Simple types, variables#
Education objectives
int,bool,float,complex,NoneTypeTrue,False,NoneKeyword
is,and,orandnotNotions of objects and attributes (with
complex)
int (integers)#
a = 4
c = -10
# binary notation (base 2)
b = 0b010
# octal notation (base 8)
o = 0o011
# hexadecimal (base 16)
h = 0x1CF0
a = int("1") # base 10
a = int("111", 2) # base 2
a = int("70", 8) # base 8
a = int("16", 16) # base 16
Note
int in Python 3 are impressive! No limit! See
https://docs.python.org/3.1/whatsnew/3.0.html#integers
Arithmetic operations#
print(10 + 3)
print(10 - 3)
print(10 * 3)
print(10 / 3) # float division
print(10 // 3) # integer division
print(10 % 3)
13
7
30
3.3333333333333335
3
1
bool (booleans)#
b = bool("1")
b = False
b = True
None, NoneType#
None is defined globally. The type of None is NoneType. This variable is typically
used to represent something that is irrelevant, not initialized, ….
None evaluates to False.
# define a variable nb_students that is not yet defined
nb_students = None
# code that update nb_students
Comparison operations (bool)#
==equal!=différent<inferior<=inferior or equal>superior>=superior or equal
Keyword is: check identity#
a = None
print(a is None)
print(a is not None)
True
False
Keywords and and or#
True and True
True
True and False
False
False and False
False
True or True
True
True or False
True
False or False
False
float (real, double precision) and complex#
# float
a = float("1")
a = 1.234
a = 1e2
a = -1e-2
a = 0.2
# complex (2 floats)
c = complex("1")
c = 1 + 2j
print(c, c.real, c.imag, c.conjugate())
(1+2j) 1.0 2.0 (1-2j)
Note
Notation var_name.attr_name to access to an attribute of an object.
Warning about floating-point arithmetic and numerical errors!
b = 1e16
c = 1.2 + b
d = c - b
print(d)
gives 2.0. It’s a very general issue (not Python): see
https://en.wikipedia.org/wiki/Floating-point_arithmetic.