



































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




































interpreted
code that cannot be understood. It is found before the program is ever run by the interpreter.
: Indentation
is a comment / " " " This is a comment " " " / " " " This is a comment " " ": All of them
statement?: x + 1 = 3
What is the result of x // y // z?: 0
What is the result of a / b / c?: 0.
statements are executed? size = 18 size = size + 12 size = size * 2 size = size / 4: 15
print: print("Here ", end="") print("There "
correct?: distance = math.sqrt( (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) )
age = 10 pet = "dog" pet_name = "Gerald" To generate the output "Gerald is 10 years old dog", which print statement is NOT correct?: print( f"pet_name, ' is ', age, ' years old ', pet " )
str = 'abcd'
['a', 'c', 2] ['b', 1, 3] ['b', 'c', 1]
mylist = [-12, -43, 55, 99, -32, 12, -2] mylist.insert(2, 67): [-12, -43, 67, 55, 99, -32, 12, - 2]
list1 = [1,2,3,4] list2 = [2,4,5,6] result1 = list1 + list result2 = list1 * 2 print( result1 ) print( result2 ): 1 2 3 4 2 4 5 6 1 2 3 4 1 2 3 4
list2 = [4, 2, 5, 3] print([item for item in list1 if item not in list2]): [1 ,7]
my_tuple = (5, 10, 15, 25, 30) Suppose you want to update the value of this tuple at 3rd index to 20. Which of the following option will you choose?: The above actions are invalid for updating tuple (Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called.)
my_tuple = (5, 10, 15, 25, 30) And update the tuple as (5, 10, 15, 20, 25, 30) Which of the following code can help to realize the purpose?: my_tuple = (5, 10, 15, 25, 30) mylist = list(my_tuple) mylist.insert(3, 20) my_tuple = tuple(mylist)
= {('Albert': 23), ('Mary': 23), ('Simon': 43)}
d1 = {"Susan":42, "John":40, "Peter":45} to delete the entry for "john" what command do we use?: del d1["John"]
d1 = {"Susan":42, "John":40, "Peter":45} To get Susan's age, what command do we use?: d1.get("Susan")
Programming
student= ( 'Jack', 19, 3.6) result= ", ".join( student ) print( student ): Error
student= ( 'Jack', 19, 3.6) result= ", ".join( str(d) for d in student ) print( student ): Jack, 19, 3.
'xyyabcdxyy'.strip('xyy') 'xyyabcdxyy'.lstrip('xyy') 'xyyabcdxyy'.rstrip('xyy'): abcd abcdxyy xyyabcd
: "cd" in str
= "abcd1234" print( str.isalnum( ) ) print( str.isalpha( ) ) print( str.isdigit( ) ) print( str.islower( ) ) print( str.capitalize( ) ): True False False True Abcd
statement requires an associated if statement.: False
to 400, "Hello World" will be printed.
is between 75 and 100"?: if 75 < grade and grade < 100 :
(interchangeably).: False
The expression not done or x > y is true.: True
The expression len( str ) < y is true: True
statement if a >= b : a = a+ 1 else : b = b - 1 will do the same thing as the statement if a < b : b = b- else :
else : grade = 'F': This code will work correctly only if grade < 70
fragment given the assumptions below? if num1 < num2 : print( 'red', end=' ') if (num1 +5) < num2 : print( 'white', end=' ' )else : print( 'blue', end=' ') print( 'yellow'): red blue yellow
limit =100; num1 = 15; num2 = 40 if limit <= 100 : if ( num1 == num2) : print( "lemon") print("orange") print( "grape"): orange grape
x > 10: print("Large") elif x > 6 and x <= 10: print("Medium") elif x > 3 and x <= 6:
print("Small") else : print("Very small"): There is no logical error, but there is no need to have (x <= 10) in the second conditional or (x <= 6) in the third conditional
y: print( x + 1 ) else : print( y - 1 ): print(x + 1) if x > y else print(y-1)
y: print( x + 1 ) else : print( y - 1 ): print(x + 1) if x > y else print(y-1)
executed 100 times?: for x in range (0, 100)
for i in range(1, 5): x += i print(x): 10
= 'abcdef' for x in str[ : -1 ] : print( x, end=' '): it prints str out forwards:
x *= 2 if x == 64: break print(x, end=' '): 2 4 8 16 32
while x < 100: x *= 2 if x == 64: continue print(x, end=' '): 2 4 8 16 32 128
while x < 100: if x == 64: continue x *= 2 print(x, end=' ': Error. Go to infinite loop
while x < 100: x *= 2 print(x, end=' ') else: print(x): 2 4 8 16 32 64 128 128
while x < 100: x *= 2 if x == 64: break print(x, end=' ') else: print(x): 2 4 8 16 32
str = "abcdef"
i += 1
calc_val( ):: calc_val (x, y) :
which are valid arguments in the call calc_val( )?: calc_val( k, i+ j, 99 )
outside any function.: True
can be called.: True
def printMax(a, b):if a > b:print(a, 'is maximum')elif a == b:print(a, 'is equal to', b)else:print(b, 'is maximum') x = 3 y = 4 printMax( x+1, y): 4 is equal to 4
def func(x): print('x is', x) x = 2 print('Changed local x to', x) func(x) print('x is now', x): x is 50 Changed local x to 2 x is now 50
def func( ): global x print('x is', x) x = 2 print('Changed global x to', x) func( ) print('x is now', x): x is 50 Changed global x to 2 Value of x is 2
def say(message, times = 1):
def change(x = 0, y = 1): x = x + y y = y + 1 print(x, y) change(x = 0 , y = 1) change(x = 1 , y = 0) change(y = 1 , x = 0) change(y = 0 , x = 1): 1 2 1 1 1 2 1 1
def power(x, y=2): result = 1 for i in range(y): result = result * x return result print( power(3) ) print( power(3, 3) ) print( power(4) )
print( power(4, 3) ): 9 27 16 64
def foo( *args ): result = 0 for i in args: result += i return result print( foo(1, 2, 3) ) print( foo(1, 2, 3, 4, 5) ): 6 15