Lists, tuples, etc
What will be placed in a?
a = 2,3
2
3
(2,3)
How to assign a tuple of length 1 to a?
a = 1
a = (1,)
a = (1)
a = tuple(1)
What is the result of this code?
a = {'a':1,'b':2,'c':3}
a['a','b']
KeyError
[1,2]
{'a':1,'b':2}
What is the result of this code?
a = {(1,2):1,(2,3):2}
a[1,2]
KeyError
1
What will be placed in a?
a = {'a': 1,'b':2, 'a':3}
It causes SyntaxError
{'a': 1,'b': 2, 'a': 3}
The behavior is undefined
{'a': 1,'b': 2}
{'a': 3,'b': 2}
What will be placed in a?
a = [1,2,3]
a[-3:-1] = 10,20,30,40
IndexError
TypeError
[10, 20, 30, 40, 3]
[10, 20, 30, 40, 2, 3]
[10, 20, 30, 40]
[[10, 20, 30, 40],3]
[(10, 20, 30, 40),3]
What is the result of this code?
a=[1,2,3,4,5,6,7,8,9]
a[::2]
[1,2]
[1,3]
[8,9]
[1,3,5,7,9]
SyntaxError
What is the result of this code?
a=[1,2,3,4,5,6,7,8,9]
a[::2]=10,20,30,40,50,60
SyntaxError
ValueError
[10, 2, 20, 4, 30, 6, 40, 8, 50, 9, 60]
What is the result of this code?
a=[1,2,3,4,5]
a[3:1:-1]
SyntaxError
ValueError
[3, 2]
[4, 3]
[4, 3, 2]
Types
What is the type of b?
a = "bay"
b = a[0]
chr
ord
int
str
What is the type of b?
a = -1
b = a ** 0.5
float
complex
int
str
What is the type of a?
a = 10/5
float
int
What is the type of a?
a = 10.0//5
float
int
What is the type of a?
a = {1,2:3}
dict
set
It causes SyntaxError
What is the type of a?
a = {}
dict
set
What is the type of a?
a = (b for b in [1,2])
tuple
generator
Order of operations
What the will be in a?
a = -1 ** 2
-1
1
The operations |, ^ and & ..
.. have same priority
.. not have same priority
Calculations
a = ?
a = -4 % 1.5
0.5
1.0
-0.5
-1.0
a = ?
a = -4 // 1.5
2.0
3.0
-2.0
-3.0
Comprehensions
Is this syntax valid?
a = { i for i in range(0,10,2) }
Yes
No
Is this syntax valid?
a = { i,j for i in range(0,10,2) for j in range(1,10,2)}
Yes
No
a = ?
a = { b : b+10 for b in range(10) }
It causes a SyntaxError
{0: 9}
{0: 10, 1: 11, 2: 12, 3: 13, 4: 14, 5: 15, 6: 16, 7: 17, 8: 18, 9: 19}
Functions
What will be printed:
def f(a, b):
print(a, b)
f(b=1, *(2,))
2 1
1 2
This code causes TypeError
Is this syntax valid?
def f(a):
pass
f(a for a in [1,2])
Yes
No, it causes SyntaxError: Generator expression must be parenthesized
What does this function return?
def f(a,b):
return a,b
f(**{'b':2,'a':1})
(1, 2)
(2, 1)
It causes TypeError: f() got an unexpected keyword argument
a = ?
def f(a=[]):
a.append(1)
return a
a=(f(),f())
([1], [1])
([1], [1,1])
([1,1], [1,1])
Logical operations
a = ?
a = 3 < 5 < 7
True
False
It causes SyntaxError
a = ?
a = 1 < 2 == 2 > 1 in [1,2,3] < [2,3,4,5] != 1
True
False
It causes SyntaxError
How many times f() will be printed?
def f():
print("f()")
return 1
a = 0<f()<2
a = 0<f() and f()<2
One
Two
Three
Four
a = ?
a = [1,2,3]>(2,3)
True
False
It causes TypeError
a = ?
a = {1,2,3}<{2,3,4,5}
True
False
It causes TypeError
a = ?
a = {1:2,2:3,3:4}<{2:5,3:6,4:7,5:8}
True
False
It causes TypeError
a = ?
a = {1:'a',2:'b'}=={2:'b',1:'a'}
True
False
It causes TypeError
a = ?
a = ( 'bay' and 'cat' )
True
False
'bay'
'cat'
Generators
Will "f()" be printed?
def f():
print("f()")
yield 1
f()
Yes
No
What will be printed?
def f(value):
while True:
value = (yield value)
a=f(10)
print(next(a))
print(next(a))
print(a.send(20))
10,10
10,10,20
10,None,20
10,None,None
Classes
How to get access to __b__ in outside of class a
class a:
__b__="hello"
It is impossible
a.__b__
a._a__b__
How to get access to __b_ in outside of class a
class a:
__b_="hello"
It is impossible
a.__b_
a._a__b_
Misc
a = ?
a = [[1] * 2] * 2
a[0][0]=2
[[2, 1], [1, 1]]
[[2, 1], [2, 1]]
[[2, 2], [2, 2]]
What is this:
exec((lambda x:x).__code__.__class__(0,0,0,0,0,b'd\x00S\x00',(),(),(),"","",0,b""))
Some random invalid code
Valid code in Python 3
A way to execute Python bytecode