Question: a. Execute the following code and comment on the output obtained. def absolute_value(num): if num >= 0: return num else: return -num print(absolute_value(2)) print(absolute_value(-4)) b.

a. Execute the following code and comment on the output obtained.

def absolute_value(num): if num >= 0: return num else: return -num

print(absolute_value(2)) print(absolute_value(-4))

b. The script below finds the summation of numbers from lower to upper. Try out with different input values and comment on the output.

def summation (lower, upper): """Arguments: A lower bound and an upper bound Returns: the sum of the numbers from lower through upper """ result = 0 while lower <= upper: result += lower lower += 1 return result lower = int(input('lower Bound : ' )) upper = int(input(' Upper bound: ')) print(summation(lower,upper) )

c. Create a dictionary of the following lists. my_list1=[ 1 2 3] my_list2=['a', 'b'] a. Create a dictionary called my_dictionary_of_lists and use the keys and values mentioned above. b. Append 4 into key 'my_list1' such that it's values are [1,2,3,4].

d. Create a dictionary student. Fill it with three student's names and their ID number. Choose your variables and dictionary keys wisely so that they can be understood from their names. a. Output the second student's name b. Print the second student's ID number c. Add a new student element in it. d. Check if a student with ID 123 exists in the dictionary you've created.

e. Comment on the chunk of code below.

class Person: # Define the class parameter "name" name = "Person"

def __init__(self, name = None): # self.name is the instance parameter self.name = name

jeffrey = Person("Jeffrey") print("%s name is %s" % (Person.name, jeffrey.name))

nico = Person() nico.name = "Nico" print("%s name is %s" % (Person.name, nico.name))

f. Define a class named Rectangle which can be constructed by a length and width. The Rectangle class has a method which can compute the area.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!