from IPython.display import HTML
HTML(open("notes.css", "r").read())
in C,
a = 45;
is an expression, so
if (a = 45) { ... }
a = 45
if a = 45: print("hello")
Syntax:
target [=
target]*
= object
namespaces
the is
operator (pointer equality -- usually)
a = 42
b = a
a is b
id(a)
id(b)
x = 1000
y = 1000
print(x is y, x == y, id(x), id(y))
i = 0
while True:
x = i+1
y = i+1
if x is not y:
break
i += 1
print(i)
i = 0
while True:
x = i-1
y = i-1
if x is not y:
break
i -= 1
print(i)
a = 3
print(id(a))
a = a + 1
print(id(a))
x = 0.0
y = 0.0
print(x is y, id(x), id(y))
a = b = 42.0
print(a is b)
a = 43.0
print(a is b)
a, b = "doug", "dinsdale"
print(a, b)
a = "doug"
b = "dinsdale"
print(a,b)
This assignment is "parallel" and is equivalent to:
tmpB = b
tmpA = a
b = tmpA
a = tmpB
print(a,b)
Swapping does what you expect.
a, b = b, a
print(a, b)
If you want only part of an iterable, this won't work:
letters = [ 'a', 'b', 'c', 'd' ]
(first, second) = letters
(first, second) = letters[:2] # probably more efficient
print(first,second)
(first, second, *discarded) = letters
print(first,second)
print(discarded)
(*beginningStuff, secondToLast, last) = letters
print(secondToLast)
print(last)
(first, second, *_) = letters # more pythonic
print(first,second)
print(_)
So you can do this...
(first, second, *middle, last) = range(10)
print(first, second, last)
print(middle)
(a, b, c, *_) = (1, 2, 2, 2, 2, 3, 4, 5, 6, 7, 8, 9)
print(a, b, c, _)
x += y
works for +
, -
, *
, /
, //
, |
, %
, &
, ^
, >>
, <<
, **
++
, etc. were considered, but dropped (expression vs. statement)
x = 30
x %= 7
x
going beyond name assignment
name
target.attributeName
target[
intExpr]
target[
key]
target[
sliceSpec]
the latter four can be overridden with operator overloading (later), but the first cannot
None = True
a = [1,2,3]
print(id(a))
a[2] = 32
a
print(id(a))
r = range(10)
list(r[2:4])
RED = "RED"
GREEN = "GREEN"
color = RED
...
if color is RED: # slightly faster than ==
print("red!")
You can put multiple simple statements on a line, separated by ;
s.
Assignments are simple.
Most everything else is compound.
x = 19
y = 3 if x < 10 else 4
y
x = 19; y = 3 if x < 10 else 4; y
x = 19; y = 33
print(x,y)
if x == 19: print("x is 19")
else: print("hello, world")
if
Statement¶x = 13
y = 19
if x < y:
print('x is less than y') # this is a block (or "suite")
print("no, really!")
elif x == y:
print('x is equal to y')
print((
1, 2,
3, 4,
5, 6))
print('hello'); print('world')
else:
print('x is greater than y')
The Case of the Missing Case
dict
y = "red"
x = { "green": 2*y,
"blue": y[-2],
"red": 3**4
}[y]
print(x)
but this is not recommended, especially since all of the expression values will get evaluated.
for
Statement¶C programmers are used to loops with integer indices:
for (i = 0; i < 23; i++)
printf("%d",i);
But Python loops are over sequences, which can be integers:
for i in (1, 2, 3):
print(i)
for i in range(10):
print(i)
... or they can be more general.
While this works:
colors = [ 'red', 'green', 'blue' ]
for i in range(len(colors)):
print(colors[i])
This is more pythonic (and indicates your intent better):
for color in colors:
print(color)
Using the plural for sequences is recommended, but you can also take a mathematical approach which is still pythonic by putting an index in the element name:
greeks = ['alpha','beta','gamma']
list(zip(greeks,colors))
for greek_i,color_i in zip(greeks,colors):
print(greek_i, color_i)
Use enumerate()
to get an index if you need it...
list(enumerate(colors))
for i, color in enumerate(colors):
print(i,color)
This should be familiar...
x = 1
while x < 100: # test being performed
x = 2 * x
print(x)
but Python has an "else" clause...
x = 1
while x < 100: # test being performed
x = 2 * x
print(x)
if x == 1000:
break
else:
print(f'loop finished without hitting break, x = {x}')
x
{ 2*i for i in range(20) }
Note: These elements appear in order because that's the way they were created. In general, you cannot count on sets to have any order in Python.
[ 2*i for i in range(20) ]
{ i:hex(i) for i in range(20) }
Parentheses around a comprehension-like expression creates a generator.
gen = ( i**2 for i in range(20) )
gen
gen.__next__()
gen.__next__()
gen.__next__()
print(list(gen))
Comprehension evalations aren't lazy. (We'll explain.)
Applied to a name, it removes it from the namespace.
x = 42
y = x
print(x)
del x
print(y)
if 1:
print(x)
Applied to a list or dictionary, can remove an element:
l = [ 1, 3, 5, 7 ]
print(l)
del l[1]
print(l)
d = { 'red':"rojo", 'green':'verde', 'blue':'azul' }
print(d)
del d['blue']
print(d)
pass
x = 94
if x == 3:
pass
else:
print("hello")
def aFunc():
pass
aFunc()