What is the output?
What is the output? var = 2 def mult_by_var(x): return x * var print(mult_by_var(7))
14
What is the output? my_list = [17, 3, 11, 5, 1, 9, 7, 15, 13] largest = my_list[0] for i in my_list[1:]: if i > largest: largest = i print(largest)
17
What is the output? var = 17 var_right = var >> 1 var_left = var << 2 print(var, var_left, var_right)
17 68 8 17 >> 1 → 17 // 2 (17 floor-divided by 2 to the power of 1) → 8 (shifting to the right by one bit is the same as integer division by two) 17 << 2 → 17 * 4 (17 multiplied by 2 to the power of 2) → 68 (shifting to the left by two bits is the same as integer multiplication by four)
What is the output? def is_int(data): if type(data) == int: return True elif type(data) == float: return False print(is_int(5)) print(is_int(5.0)) print(is_int("5"))
True False None
What is the output? my_list = [10, 8, 6, 4, 2] del my_list print(my_list)
Variable has been deleted
What is the output? text = "pyxpyxpyx" for letter in text: if letter == "x": continue print(letter, end="")
pypypy
What is the output? dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"} dictionary['swan'] = 'cygne' print(dictionary)
{'cat': 'chat', 'dog': 'chien', 'horse': 'cheval', 'swan': 'cygne'}
What is the output? dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"} phone_numbers = {'boss': 5551234567, 'Suzy': 22657854310} empty_dictionary = {} print(dictionary) print(phone_numbers) print(empty_dictionary)
{'cat': 'chat', 'dog': 'chien', 'horse': 'cheval'} {'boss': 5551234567, 'Suzy': 22657854310} {}
What is the output? dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"} dictionary.popitem() print(dictionary)
{'cat': 'chat', 'dog': 'chien'}
What is the output? def strange_list_fun(n): strange_list = [] for i in range(0, n): strange_list.insert(0, i) return strange_list print(strange_list_fun(5))
[4, 3, 2, 1, 0]
What is the output of the following snippet? lst = [1, 2, 3, 4, 5] lst.insert(1, 6) del lst[0] lst.append(1) print(lst)
[6, 2, 3, 4, 5, 1]
What is the output? my_list = [10, 8, 6, 4, 2] new_list = my_list[1:-1] print(new_list)
[8, 6, 4]
What is the output? my_list = [10, 8, 6, 4, 2] new_list = my_list[1:3] print(new_list)
[8, 6]
What is the output? my_list = [10, 8, 6, 4, 2] del my_list[:] print(my_list)
[]
What is the output? my_input = input("Enter something: ") # Example input: hello print(my_input * 3)
hellohellohello
What is the output? dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"} for key in dictionary.keys(): print(key, "->", dictionary[key])
horse -> cheval dog -> chien cat -> chat
What is the output? var = 2 print(var) def return_var(): global var var = 5 return var print(return_var()) print(var)
5 5
What is the output? counter = 5 while counter > 2: print(counter) counter -= 1
5 4 3
What is the output? x = int(input("Enter a number: ")) # The user enters 2 print(x * "5")
55
What is the output? def fun(a): if a > 30: return 3 else: return a + fun(a + 3) print(fun(25))
56
What is the output? tup = 1, 2, 3 a, b, c = tup print(a * b * c) -> Multiplied
6
What is the output? for i in range(6, 1, -2): print(i, end=" ")
6 4 2
What is the output? text = "OpenEDG Python Institute" for letter in text: if letter == "P": break print(letter, end="")
OpenEDG
What is the output? word = "Python" for letter in word: print(letter, end="*")
P*y*t*h*o*n*
What is the output? x = 1 y = 0 z = ((x == y) and (x == y)) or not(x == y) print(not(z))
False
What is the output? my_list = [0, 3, 12, 8, 2] print(5 in my_list) print(5 not in my_list) print(12 in my_list)
False True True
What is the output? def hi(): return print("Hi!") hi()
Hi!
What is the output? print((5 x ((25 % 13) + 100) / (2 x 13)) // 2)
10.0
What is the output? a = '1' b = "1" print(a + b)
11
What is the output? list_1 = [1] list_2 = list_1 list_1[0] = 2 print(list_2)
2
What is the output? def create_list(n): my_list = [] for i in range(n): my_list.append(i) return my_list print(create_list(5))
[0, 1, 2, 3, 4]
What is the output? dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"} for english, french in dictionary.items(): print(english, "->", french)
cat -> chat dog -> chien horse -> cheval
What is the output? dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"} for key in sorted(dictionary.keys()): print(key, "->", dictionary[key])
cat -> chat dog -> chien horse -> cheval
What is the output? dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"} phone_numbers = {'boss' : 5551234567, 'Suzy' : 22657854310} empty_dictionary = {} print(dictionary['cat']) print(phone_numbers['Suzy'])
chat 5557654321
What is the output? dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"} for french in dictionary.values(): print(french)
cheval chien chat
What is the output? i = 111 for i in range(2, 1): print(i) else: print("else:", i)
else: 111
What is the output? i = 5 while i < 5: print(i) i += 1 else: print("else:", i)
else: 5
What is the output? print((-2 / 4), (2 / 4), (2 // 4), (-2 // 4))
-0.5 0.5 0 -1
What is the output? print((2 % -4), (2 % 4), (2 xx 3 xx 2))
-2 2 512
What is the output? for i in range(0, 3): print(i) else: print(i, "else")
0 1 2 2 else
What is the output? n = 0 while n != 3: print(n) n += 1 else: print(n, "else")
0 1 2 3 else
What is the output? for i in range(3): print(i, end=" ")
0 1 2
What is the output? x = 4 y = 1 a = x & y b = x | y c = ~x # tricky! d = x ^ 5 e = x >> 2 f = x << 2 print(a, b, c, d, e, f)
0 5 -5 1 1 16 https://www.youtube.com/watch?v=PyfKCvHALj8
What is the output? list_1 = [1] list_2 = list_1[:] list_1[0] = 2 print(list_2)
1
What is the output? def factorial_function(n): if n < 0: return None if n < 2: return 1 product = 1 for i in range(2, n + 1): product *= i return product for n in range(1, 6): # testing print(n, factorial_function(n))
1 1 2 2 3 6 4 24 5 120
What is the output? def mult(x): var = 7 return x * var var = 3 print(mult(7))
49
What is the output? def adding(x): global var var = 7 return x + var print(adding(4)) print(var)
11 7
What is the output? If you pass a list to a function, the function has to handle it like a list. def list_sum(list): s = 0 for elem in list: s += elem return s print(list_sum([5, 4, 3]))
12
What is the output? num_1 = input("Enter the first number: ") # Enter 12 num_2 = input("Enter the second number: ") # Enter 21 print(num_1 + num_2)
1221
What is the output? for i in range(1, 10): if i % 2 == 0: print(i)
2 4 6 8
What is the output? a = 1 def fun(): a = 2 print(a) fun() print(a)
2 1
What is the output? a = 1 def fun(): global a a = 2 print(a) a = 3 fun() print(a)
2 2
What is the output? a = 1 def fun(): global a a = 2 print(a) fun() a = 3 print(a)
2 3
What is the output? a = 6 b = 3 a /= 2 * b print(a)
2 * b = 6a = 6 → 6 / 6 = 1.0
What is the output? def my_function(): var = 2 print("Do I know that variable?", var) var = 1 my_function() print(var)
Do I know that variable? 2 1
What is the output? def my_function(): global var var = 2 print("Do I know that variable?", var) var = 1 my_function() print(var)
Do I know that variable? 2 2
What is the output? def message(what, number): print("Enter", what, "number", number) message("telephone", 11) message("price", 5) message("number", "number")
Enter telephone number 11 Enter price number 5 Enter number number number
What is the output? def wishes(): return "Happy Birthday!" w = wishes() print(w)
Happy Birthday!
What is the output? def introduction(first_name, last_name): print("Hello, my name is", first_name, last_name) introduction("Luke", "Skywalker") introduction("Jesse", "Quick") introduction("Clark", "Kent")
Hello, my name is Luke Skywalker Hello, my name is Jesse Quick Hello, my name is Clark Kent
What is the output? def hi_everybody(my_list): for name in my_list: print("Hi,", name) hi_everybody(["Adam", "John", "Lucy"])
Hi, Adam Hi, John Hi, Lucy
What is the output? def my_function(n): print("I got", n) n += 1 print("I have", n) var = 1 my_function(var) print(var)
I got 1 I have 2 1
What is the output? def intro(a="James Bond", b="Bond"): print("My name is", b + ".", a + ".") intro()
My name is Bond. James Bond.
What is the output? def intro(a, b="Bond"): print("My name is", b + ".", a + ".") intro("Susan")
My name is Bond. Susan.
What is the output? def intro(a="James Bond", b="Bond"): print("My name is", b + ".", a + ".") intro(b="Sean Connery")
My name is Sean Connery. James Bond.
What is the output? for i in range(2, 1): print("The value of i is currently", i)
Nothing because the second argument is not bigger than the first argument
What is the output? def scope_test(): x = 123 scope_test() print(x)
NameError: name 'x' is not defined
What is the output? def multiply(a, b): return a * b print(multiply(3, 4)) def multiply(a, b): return print(multiply(3, 4))
None
def message(): alt = 1 print("Hello, World!") print(alt)
The NameError exception will be thrown (NameError: name 'alt' is not defined)
def factorial(n): return n * factorial(n - 1) print(factorial(4))
The factorial function has no termination condition (no base case) so Python will raise an exception (RecursionError: maximum recursion depth exceeded)
What is the output? my_dictionary = {"A": 1, "B": 2} copy_my_dictionary = my_dictionary.copy() my_dictionary.clear() print(copy_my_dictionary) print(my_dictionary)
The program will print {'A': 1, 'B': 2} to the screen.
What is the output? for i in range(2, 8, 3): print("The value of i is currently", i)
The value of i is currently 2 The value of i is currently 5 starting stop step 2 (starting number) → 5 (2 increment by 3 equals 5 - the number is within the range from 2 to 8) → 8 (5 increment by 3 equals 8 - the number is not within the range from 2 to 8, because the stop parameter is not included in the sequence of numbers generated by the function.)
What is the output? def boring_function(): print("'Boredom Mode' ON.") return 123 print("This lesson is interesting!") boring_function() print("This lesson is boring...")
This lesson is interesting! 'Boredom Mode' ON. This lesson is boring...
What is the output? def strange_function(n): if(n % 2 == 0): return True print(strange_function(2)) print(strange_function(1))
True None
What is the output? Here's an example of a list comprehension - the code creates a five-element list filled with the first five natural numbers raised to the power of 3: cubed = [num ** 3 for num in range(5)] print(cubed)
[0, 1, 8, 27, 64]
What is the output? my_list = [10, 8, 6, 4, 2] new_list = my_list[:] print(new_list)
[10, 8, 6, 4, 2] As we've said before, omitting both start and end makes a copy of the whole list:
What is the output? my_list = [10, 8, 6, 4, 2] new_list = my_list[:3] print(new_list)
[10, 8, 6] If you omit the start in your slice, it is assumed that you want to get a slice beginning at the element with index 0.
What is the output? my_list = [10, 8, 6, 4, 2] new_list = my_list[3:] print(new_list)
[4, 2]
What is the output? colors = { "white": (255, 255, 255), "grey": (128, 128, 128), "red": (255, 0, 0), "green": (0, 128, 0) } for col, rgb in colors.items(): print(col, ":", rgb)
white : (255, 255, 255) grey : (128, 128, 128) red : (255, 0, 0) green : (0, 128, 0)
What is the output? dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"} dictionary.update({"duck": "canard"}) print(dictionary)
{'cat': 'chat', 'dog': 'chien', 'horse': 'cheval', 'duck': 'canard'}
What is the output? dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"} del dictionary['dog'] print(dictionary)
{'cat': 'chat', 'horse': 'cheval'}
What is the output? dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"} dictionary['cat'] = 'minou' print(dictionary)
{'cat': 'minou', 'dog': 'chien', 'horse': 'cheval'}