Variables and controls in Python
1 Data types
1.1 Integers
The int
type can handle long integers very effectively.
x = 1
type(x); print(x)
for i in range(1,100):
x *= 10
type(x); print(x)
Integers are basically unbounded.
Only memory limits its maximum value.
1.2 Integer representations
- Decimal:
53
- Binary:
0b110101
- Octal:
0o65
- Hexadecimal:
0x35
1.3 Floating-point numbers
The float
type represents floating-point (real) numbers.
for x in (3.414, 4., .3, 1e2, 3.4e1):
type(x); print(x)
- Minimum non-zero value: Approximately
5e-324
- Maximum non-infinity value: Approximately
1.79e308
1.4 Complex numbers
Use j
instead of i
for imaginary parts.
x = 4+5j
type(x); print(x)
y = 4-5j
z = x+y
type(z); print(z)
print(z.real); print(z.imag)
1.5 Strings
The str
type stores a sequence of characters.
a = 'Hello World!'
type(a); print(a)
an_empty_string = ''
print('really'+an_empty_string+'?')
1.6 Strings: Escape sequences
Escape sequences to include delimiters.
print("What about a single quote (')?")
print('What about a single quote (\')?')
print('What about a double quote (")?')
print("What about a double quote (\")?")
print("What about a backslash itself (\\)?")
1.7 Strings: Triple-quoted strings
Use three single/double quotes for multi-line strings.
print('''No escaping needed for '!''')
print("""Even
"multiple"
lines""")
1.8 Boolean
The bool
type should be True
or False
, not true
or false
.
a = 'you'
type(a == 'you'); print(a == 'you')
if a == 'me':
print('me')
elif a == 'you':
print('you')
else:
print('someone else')
1.9 Type casting
Type casting means converting one type of a variable to another.
x = '12'
type(int(x)); print(int(x))
x = '12.34'
# Oops!
type(int(x)); print(int(x))
x = 12.34
type(str(x)); print('String: ' + str(x))
2 Variable scoping
2.1 Global variables
Global variables are available “globally.”
def f():
print(a)
a = 'Global?'
f()
Try to change a global variable.
def f():
a = 'Local!'
print(a)
a = 'Global?'
f()
print(a)
2.2 Global variables: Global and local
You cannot access the same variable globally and locally.
def f():
print(a)
a = 'Local!'
print(a)
a = 'Global?'
f()
print(a)
Any variables that are assigned or modified inside a function become local.
2.3 Global variables: Explicit declaration
You can modify global variables inside a function.
def f():
global a
print(a)
a = 'Modified global'
print(a)
a = 'Global?'
f()
print(a)
2.4 Local variables
Local variables are only accessible from where they are defined.
def f():
a = 'I am local!'
print(a)
f()
print(a)
2.5 Nonlocal variables
Nonlocal variables are similar to global variables, but you cannot modify them from a nested function.
def f():
a = 'Local'
print('Inside f: ' + a)
def g():
nonlocal a
a = 'Modified local'
print('Inside g: ' + a)
g()
print('Inside f: ' + a)
a = 'Global?'
f()
print(a)
2.6 Nonlocal vs. global variables
Global variables are modifiable anywhere as long as they are declared as global
.
def f():
a = 'Local'
print('Inside f: ' + a)
def g():
global a
a = 'Modified global'
print('Inside g: ' + a)
g()
print('Inside f: ' + a)
a = 'Global?'
f()
print(a)
3 Control statements
3.1 Conditionals
Python provides a branching control if
, else
, and elif
.
a = 1
if a == 0:
print('a is 0')
else:
print('a is not 0')
if a == 0:
print('a is 0')
elif a == 1:
print('a is 1')
else:
print('a is not 0 or 1')
3.2 Loops: for
A for
loop takes an array-like data type.
# tuple
for i in (1, 2, 3):
print(i)
# list
for i in [1, 2, 3]:
print(i)
3.3 Loops: while
A while
loop tests a condition and loops the block as long as the condition is true.
# prints 1 through 10
a = 1
while a <= 10:
print(a)
a = a+1
4 Short-circuiting
Short-circuiting means taking a shortcut in a conditional statement.
def f():
return x == 1
def g():
global x
x += 1
return x == 3
x = 1
print('Initial: ' + str(x))
if f() or g():
print('f True, g False: ' + str(x))
x = 2
if f() or g():
print('f False, g True: ' + str(x))
5 Homework: Numeral system conversion
Convert 0xa9f5
to a decimal number manually.
Show your work for full credits!