Python 3

# fill in details ...
def modify_tuple(t, i, x):
modify_tuple((1,2,3), 1, 'foo') # should return (1, 'yo!', 3)
Python code :
___________________________________
def modify_tuple(t , i , x) : # function to change value of ith
element of tuple to x
tmp_tuple = list(t) # declare a
temporaty tuple and assign t to it
tmp_tuple[i] = x #
change value at ith index of tmp_tuple
t = tuple(tmp_tuple) # assign tmp_tuple to
t
return
t
# return the tuple t
t = ('1', '2', '3') # input
example
myTuple = modify_tuple(t , 1 , 'foo' )
print(myTuple)
___________________________________
Explanation : As per question requirement a function modify_tuple() is declared which takes three arguments : a tuple(t) , and index(i) and a value(x) and finally changes the value of element at ith index to x.Since tuples are immutable(not mutable),you cannot directly change its value.To change the value at ith index , I've used a list tmp_tuple and assigned t to it using typecasting.After changing the value I've assigned tmp_tuple to t by type casting it to a tuple and finally returned the modified tuple.
___________________________________
Code screenshot :

___________________________________
Output :

_________________________________
Please comment in case you have any doubt or anything is unclear in code/explanation.
Python 3 # fill in details ... def modify_tuple(t, i, x): modify_tuple((1,2,3), 1, 'foo') # should...
ON PYTHON: ''' Design the functions described below. RECALL: With functions that do not return a value and print a result to the console, to test you must call the function, run it and visually inspect the result for correctness. With functions that return a value, use the print_test function to provide feedback of the test results at the command line. The print_test function is implemented for you at the bottom of this file. RECALL: floating point arithmetic can lose...
PYTHON FILL IN THE BLANK CODE REMEMBER TO UTILIZE DICTIONARIES # Implement this function: def store_checkout(inventory_tuple_list, item_purchase_list): """ inventory_tuple_list: A list of tuples. Each tuple has a string representing an item name, an int representing a price, and a string representing a description. Example: [("A", 5, "shiny new A"), ("B", 10, "big heavy B")] item_purchase_list: A list of strings. Each string represents an item name. Example: ["A", "A", "B", "C"] Return the total price of the items in item_purchase_list by...
Please help on my homework Q i got wrong: PYTHON Question 1 def higherOrderFilter(data, p) : res = [] for d in data : if p(d) : res.append(d) return res def FilterR(data, p): if data == []: return [] return [data[0]] + FilterR(data[1:], p) if p(data[0]) else FilterR(data[1:], p) Both of these functions produce the same output given the same data and same filtering condition p. True False Question 2...
IN PYTHON 3 LANGUAGE, please help with function, USE RECURSION ONLY def im(l: 'an int, str,list,tuple,set,or dict') -> 'an int, str, tuple, or frozenset' pass SAMPLE OUTPUT: The following call (with many mutable data structures) imm(1) returns 1 imm('a') returns 'a' imm( (1, 2, 3)) returns (1, 2, 3) imm( frozenset([1, 2, 3])) returns frozenset({1, 2, 3}) imm( [1, 2, 3, 4, 5, 6]) returns (1, 2, 3, 4, 5, 6) imm( [1, 2, [3, [4], 5], 6]) ...
I need to rewrite this code without using lambda function using python. def three_x_y_at_one(x): result = (3 * x *1) return result three_x_y_at_one(3) # 9 zero_to_four = list(range(0, 5)) def y_values_for_at_one(x_values): return list(map(lambda x : three_x_y_at_one(x), x_values)) -------> this is the function that I need to rewrite without using a lambda function . y_values_for_at_one(zero_to_four) # [0, 3, 6, 9, 12] Thanks
Python 3 Modify the sentence-generator program of Case Study so that it inputs its vocabulary from a set of text files at startup. The filenames are nouns.txt, verbs. txt, articles.txt, and prepositions.txt. (HINT: Define a single new function, getWords. This function should expect a filename as an argument. The function should open an input file with this name, define a temporary list, read words from the file, and add them to the list. The function should then convert the list...
PYTHON 3: An n x n matrix forms a magic square if the following conditions are met: 1. The elements of the matrix are numbers 1,2,3, ..., n2 2. The sum of the elements in each row, in each column and in the two diagonals is the same value. Question: Complete the function that tets if the given matrix m forms a magic square. def is_square(m): '''2d-list => bool Return True if m is a square matrix, otherwise return False...
in python 3 1. A program contains the following function definition: def cube(num): return num * num * num Write a statement that passes the value 4 to this function and assign its return value to the variable result. 2. Write a function named get_first_name that asks the user to enter his or her first name and returns it.
1. In Python, 'mutable' objects such as lists are passed to functions 'by reference'. This means: a.) Changes made to the object within the function persist after the function completes b.) The function receives a copy of the object c.) Python keeps a history of where then object was used d.) Statements in the function can refer to the object by nickname 2. Which of the following is NOT a requirement for a Python function: a.) The function must have...
Hello can anyone help solve this please in Python the way it's
asked?
Question 22 40 pts List Comprehensions Write the solution to a file named p2.py and upload it here. Comprehensions will show up at the midterm exam, so it's important to practice. a) Write a list comprehension that returns a list [0, 1,4, 9, 16, .. 625] starting from range(0, 26). (lots of list elements were omitted in ...) b) Write a list comprehension that returns the list...