Python Simple Assignment Flashcards

 

card 1 of 25

 

>>> x = 1.3
>>> (type(x),x)

 

evaluate expression by substituting values for variables

(<type 'float'>, 1.3)

card 2 of 25

 

>>> MyVar = False, 'Q'
>>> type(MyVar)
>>> MyVar*2

 

remember that comma is used to form tuples

<type 'tuple'>
(False,'Q',False,'Q')

card 3 of 25

 

>>> emblem = "xaraktor"
>>> type(emblem)

 

this one is easy

<type 'str'>

card 4 of 25

 

>>> E = else = range(3)
>>> E[1]

 

multiple assignments evaluate from the same expression

SyntaxError: cannot assign to reserved word in Python language

card 5 of 25

 

>>> E = Else = range(3)
>>> E[2],Else[1]

 

remember that comma is used to form tuples

(2,1)

card 6 of 25

 

>>> (x1,x2) = (2,50)
>>> x1*x2

 

tuple assignment is from corresponding terms on right of '='

100

card 7 of 25

 

>>> a,b = True,False
>>> a or b

 

tuple assignment is from corresponding terms on right of '='

True

card 8 of 25

 

>>> (x1,x2) = range(3)
>>> x1*x2

 

tuple assignment is from corresponding terms on right of '='

ValueError: too many values to unpack/unbox

card 9 of 25

 

>>> (x1,x2) = 298
>>> x1-x2

 

tuple assignment is from corresponding terms on right of '='

TypeError: integer does not match tuple assignment

card 10 of 25

 

>>> Home, Home, Home = range(3)
>>> Home

 

confusing, but evaluate term-by-term assignment, left to right

2

card 11 of 25

 

>>> T, v = "Shirt", "beach"
>>> T, v = v, T
>>> T + v

 

in this case, do evaluations before assignments

'beachShirt'

card 12 of 25

 

Python 2 Question
>>> 
>>> A = 4
>>> B = A*B
>>> C = B+B
>>> print C
>>> D = print C

 

keep track of variable values line by line to evaluate

NameError: name 'B' is not defined in 'A*B'

card 13 of 25

 

Python 2 Question
>>> 
>>> A = 4
>>> B = A*A
>>> C = B+B
>>> print C
>>> D = print C

 

keep track of variable values line by line to evaluate

32
SyntaxError: 'D = print C' invalid syntax

card 14 of 25

 

Python 3 Question
>>> 
>>> A = 4
>>> B = A*A
>>> C = B+B
>>> D = print(C)
>>> print(type(D))

 

keep track of variable values line by line to evaluate

<class 'NoneType'>

card 15 of 25

 

>>> x, y = a, b = True, False
>>> x, a, y, b

 

multiple assignment works even with tuple assignment

(True,True,False,False)

card 16 of 25

 

>>> x, y = a, b = True, False
>>> del b
>>> x, a, y, b

 

'del' is a command to remove/undefine a variable

NameError: name 'b' is not defined

card 17 of 25

 

>>> S = [20,30,2,5,-4]
>>> [ 2*x for x in S ]

 

evaluate the list comprehension using the value for S

[40,60,4,10,-8]

card 18 of 25

 

>>> S = [20,30,2,5,-4]
>>> S[-1]*S[2]

 

this one is simple

-8

card 19 of 25

 

>>> S = [20,30,2,5,-4]
>>> del S[1]
>>> S[-1]*S[2]

 

the 'del S[1]' changes S by removing just the specified item

-20

card 20 of 25

 

>>> S = "cases of vinegar".split()
>>> S[1][0]+S[2][3]

 

evaluate the assignment to S first

'oe'

card 21 of 25

 

>>> t = ["ab",1.5]
>>> u = [t,22]
>>> u
>>> u[0]
>>> u[0][0]
>>> u[0][0][0]
>>> u[0][0][0][0]

 

evaluate values before assigning

[['ab',1.5],22]
['ab',1.5]
'a'
'a'

card 22 of 25

 

>>> one = [True,True]
>>> two = [one,"second"]
>>> three = [one,two,"third"]
>>> len(three)
>>> len(three[1])

 

evaluate values before assigning

3
2

card 23 of 25

 

>>> control = [9,2,2,7]
>>> DeTail = control + 25
>>> DeTail[-1]

 

evaluate values before assigning

TypeError: can only concatenate list to list

card 24 of 25

 

>>> control = [9,2,2,7]
>>> DeTail = control + [25]
>>> Detail[-1]

 

evaluate values before assigning

NameError: name 'Detail' is not defined

card 25 of 25

 

>>> control = [9,2,2,7]
>>> DeTail = control + [25]
>>> DeTail[-1]

 

evaluate values before assigning

25

 

End of Problem Set

(use browser reload to restart this problem set)