Note: this is not a language course! Though I'll cover the important bits of the language (and standard library) that are relevant to class material, I expect you to master the language on your own time.
Python ...
# python can be used as a calculator
3*4
1+3
7/2
12
4
3.5
# by default, only the result of the last expression in a cell is displayed after evaluation.
# the following forces display of *all* self-standing expressions in a cell.
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
_
3.5
7/3
7//3
2.3333333333333335
2
Python has no beginning/end block markers! Blocks must be correctly indented (4 spaces is the convention) to delineate them.
if True:
print('In if-clause')
x=5
print(x)
else:
print('In else-clause')
In if-clause 5
if False:
print('In if-clause')
x=5
print(x)
else:
print('In else-clause')
In else-clause
for x in range(5): # for (int i=0; i<5; i++){}
print('In for loop body')
print(x)
In for loop body 0 In for loop body 1 In for loop body 2 In for loop body 3 In for loop body 4
def foo():
print('In function definition')
x
4
foo()
In function definition
In Python, variables do not have types. Values have types (though they are not explicitly declared). A variable can be assigned different types of values over its lifetime.
a = 2 # starts out an integer
print(type(a)) # the `type` function tells us the type of a value
a = 1.5
print(type(a))
a = 'hello'
print(type(a))
<class 'int'> <class 'float'> <class 'str'>
type(3.33)
type(4)
float
int
dir(5)
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'as_integer_ratio', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']
Note that all the types reported are classes. I.e., even types we are accustomed to thinking of as "primitives" (e.g., integers in Java) are actually instances of classes. All values in Python are objects!
There is no dichotomy between "primitive" and "reference" types in Python. All variables in Python store references to objects.
# int: integers, unlimited precision
1
500
-123456789
6598293784982739874982734
1
500
-123456789
6598293784982739874982734
# basic operations
1 + 2
1 - 2
2 * 3
2 * 3 + 2 * 4
2 / 5
2 ** 3 # exponentiation
abs(-25)
2//5
3
-1
6
14
0.4
8
25
0
# modulus (remainder) and integer division
10 % 3 # remainder function
10 // 3
1
3
# floating point is based on the IEEE double-precision standard (limit to precision!)
2.5
-3.14159265358924352345
1.000000000000000000000001
2.5
-3.1415926535892433
1.0
# mixed arithmetic "widens" ints to floats
3 * 2.5
1 / 0.3
7.5
3.3333333333333335
True
False
True
False
not True
False
True and True
False and True
True and False
False and False
True
False
False
False
True or True
False or True
True or False
False or False
True
True
True
False
# relational operators
1 == 1
1 != 2 # not (1==2)
1 < 2
1 <= 1
1 > 0
1 >= 1
1.0 == 1
1.0000000000000000001 == 1
type(1) == type(1.0)
True
True
True
True
True
True
True
True
False
# object identity (reference) testing
x = 1000
y = 1000
x == x
x is x # checks object address
x is not x
True
True
False
x == y # compares values
x is y #compares object references (address)
x is not y
True
False
True
x=1000
y=x # does not create a new object for y, instead y points to the x object
x==y #compare values, should be true
x is y # compares object addresses, will it be the same?
True
True
x=5 # create a new int object to store 5, make x point to it, don't change
# what y points to
x
y
5
1000
y=5
x
y
x is y
5
5
True
x=1000
y=x
y=y+1
x
y
1000
1001
# but Python caches small integers! so ...
x = 5
y = 5
x == y
x is y
True
True
# whatever strings you want
'hello world!'
"hello world!"
'hello world!'
'hello world!'
# convenient for strings with quotes:
print('she said, "how are you?"')
print("that's right!")
she said, "how are you?" that's right!
'hello' + ' ' + 'world'
'thinking... ' * 3
'*' * 80
'hello world'
'thinking... thinking... thinking... '
'********************************************************************************'
Strings are an example of a sequence type; https://docs.python.org/3.5/library/stdtypes.html#typesseq
Other sequence types are: ranges, tuples (both also immutable), and lists (mutable).
All immutable sequences support the common sequence operations, and mutable sequences additionally support the mutable sequence operations
# indexing
greeting = 'hello there'
# 01234567890
greeting[0]
greeting[6]
len(greeting)
greeting[len(greeting)-1]
'h'
't'
11
'e'
# negative indexes greeting = 'hello there'
# 10987654321
greeting[-1]
greeting[-2]
greeting[-len(greeting)]
_
'e'
'r'
'h'
'h'
greeting = 'hello there'
# 01234567890
#greeting[len(greeting)]
#greeting[11]
greeting[-12]
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-7-e5da3f695454> in <module> 3 #greeting[len(greeting)] 4 #greeting[11] ----> 5 greeting[-12] IndexError: string index out of range
# "slices" [from:to] index "from" up to index "to"-1
# create/return a new string
greeting = 'hello there'
# 01234567890
greeting[0:11] # [0:len(greeting)]
greeting[0:5]
greeting[6:11]
greeting
'hello there'
'hello'
'there'
'hello there'
# default slice ranges [from:to]
greeting[:11]
greeting[6:]
greeting[:]
'hello there'
'there'
'hello there'
#greeting = 'hello there'
# 01234567890
# slice "steps" [from:to:increment]
greeting[::2]
greeting[::3]
greeting[6:11:2]
'hlotee'
'hltr'
'tee'
# negative steps
greeting[::-1]
'ereht olleh'
# other sequence ops 'hello there'
# 01234567890
greeting
greeting.count('e')
greeting.index('e')
greeting.index('e', 2)
'e' in greeting
'z' not in greeting
min(greeting)
max(greeting)
'hello there'
3
1
8
True
True
' '
't'
Strings also support a large number of type-specific methods.
Constructors for most built-in types exist that create values of those types from other types:
# making ints
int('123')
int(12.5)
int(True)
# floats
float('123.123')
# strings
str(123)
123
12
1
123.123
'123'
5 + 6
(5).__add__(6)
# calling method __add__ on the object (5) sending it argument (6)
(6).__add__(5)
11
11
11
class MyInt(int): # MyInt inherits from int
# instance methods in a class must have "self" as the first argument
def __add__(self, other): #self is the object that __add__ was called on
return self * other
a = MyInt(5)
b = MyInt(6)
a + b
a-b
30
-1
abs(-2.8)
(-2.8).__abs__()
2.8
2.8
'hello' + ' ' + 'world'
'hello'.__add__(' ').__add__('world')
'hello world'
'hello world'
None
¶None
is like "null" in other languages
# often use as a default, initial, or "sentinel" value
x = None
x
note: notebooks do not display the result of expressions that evaluate to None
None
a = None
b = 100
c = None
a
b
c
100
some functions return None
, so when we call them, there is no "Out" cell
print('Hello')
Hello
All objects in Python can be evaluated in a Boolean context (e.g., as the condition for an if
statement). Values for most types act as True
, but some act (conveniently, usually) as False
.
if True: # try numbers, strings, other values here
print('tests as True')
else:
print('tests as False')
tests as True
if False: # try numbers, strings, other values here
print('tests as True')
else:
print('tests as False')
if None: # try numbers, strings, other values here
print('tests as True')
else:
print('tests as False')
# any empty sequence type is False
if '': # try numbers, strings, other values here
print('tests as True')
else:
print('tests as False')
if 0: # try numbers, strings, other values here
print('tests as True')
else:
print('tests as False')
tests as False tests as False tests as False tests as False
What tests as False
?
# simple, single target assignment RETURNS None
a = 0
b = 'hello'
a
b
0
'hello'
# can also assign to target "lists"
# in all assignment statements the right hand side
# is evaulated completed first, then assigned element by elemnt to
# the left hand side
a, b, c = 0, 'hello', True
a
b
c
0
'hello'
True
# note: expression on right is fully evaluated, then are assigned to
# elements in the "target" list, from left to right
x, y, z = 1, 2, 3
x
y
z
x, y, z = x+y, y+z, x+y+z # 3 5 6
x
y
z
1
2
3
3
5
6
# easy python "swap"
a, b = 'apples', 'bananas'
a, b = b, a # 'bananas' 'apples'
a
b
'bananas'
'apples'
# note: order matters!
a, b, a = 1, 2, 3
a
b
3
2
# can also have multiple assignments in a row -- consistent with
# above: expression is evaluated first, then assigned to all targets
# from left to right (note: order matters!)
x = y = z = None
a = 0
a += 2 #increment a by 2
a
a *= 3 # multiply a by 3
a
a -=4
a
2
6
2
pass
¶pass
is the "do nothing" statement
pass
def foo():
pass