Handout Python Fundamentals
Handout Python Fundamentals
March 2015
Python fundamentals
Amaral Lab 0
Data Types
Type Values Operations
None None
Boolean True, False and, or, in, not, …
Number Integer, float, fraction, complex *, +, -, %, //, **
Tuple Ordered immutable sequence of Create
values of any type
List Ordered sequence of values of any append, index, pop, …
type
Set Unordered “bag” of unique add, update, union,
immutable values intersection, …
Dictionary Unordered “bag” of key-value pairs update, pop, …
String Sequences of Unicode characters Concatenation, removal, …
Byte and Byte Binary value or array of values
array (such as a PNG image file)
Amaral Lab 1
Boolean type
Beware: None, 0 from any numerical type, and empty
set/list/string/dictionary all default to False.
Beware: Not has higher priority than and and or (i.e., it is performed first).
Amaral Lab 2
Number type
Beware: Operations with mixed types convert to a more “complex” type.
Amaral Lab 3
Tuples
Tuples are immutable ordered sequences of data accessible by
index.
a_tuple[2] returns 5
a_tuple[:2] returns ( 1, 5 )
Amaral Lab 5
Lists
Lists are mutable ordered sequences of data accessible by index.
b_list.index(1) returns 0
b_list.index( “3” ) raises a ValueError exception
Amaral Lab 7
Sets
Sets are unordered “bags” of unique immutable “keys”.
a_set.add(“3”)
a_set now returns { 1, Fraction(1, 3), “3”, ( 1, 5, “hello!”, True ), “1” }
a_set.update( b_set )
a_set now returns { Fraction(1, 3), ( 1, 5, “hello!”, True ), 1, “1”,
complex(1 , 3), “3” }
a_set.pop() returns, for example, 1 randomly selected!!!
a_set now returns { “1”, Fraction(1, 3), ( 1, 5, “hello!”, True ), complex(1 ,
3) }
Amaral Lab 8
Sets
Sets are unordered “bags” of unique immutable “keys”.
a_set.union( b_set )
a_set.intersection( b_set )
a_set.difference( b_set )
a_set.symmetric_difference( b_set )
a_set.issubset( b_set )
a_set.issuperset( b_set )
Amaral Lab 9
Dictionaries
Dictionaries are unordered “bags” of pairs of key/value. Keys
must be unique immutable data, i.e., form a set.
Amaral Lab 10
Dictionaries
Dictionaries are unordered “bags” of pairs of key/value. Keys
must be unique immutable data, i.e., form a set.
Amaral Lab 11
Strings
Strings are immutable sequences of Unicode characters.
a_string = “two”
b_string = ‘hello’
Amaral Lab 12
Strings
Strings are immutable sequences of Unicode characters.
a_string = “two”
b_string = ‘hello’
Amaral Lab 13
Strings
Strings are immutable sequences of Unicode characters.
first_name = “Luis”
last_name = “Amaral”
“My name is {0} {1}.\n”.format( first_name, last_name )
Amaral Lab 14
Flow control
Command Type
for x in a_list: Loop
commands
while ( expression is True ): Conditional loop
commands
break Breaks out of lowest
enclosing loop
continue Interrupts lowest
enclosing loop and
continues with next
iteration
pass Syntax
Amaral Lab 15
Flow control
Command Type
if expression1 is True : Condition
commands
elif expression2 is True :
commands
else:
commands
try: Exception handling
commands
except exception_type:
commands
raise exception_type( a_string ) Exception raising
with command as object: Ensuring execution of
commands paired commands
Amaral Lab 16
Flow control
Command Type
def function_name( arguments ): Function
””” String with information on function””” (if something is
whitespace or return
commands is missing it returns
return something None)
lambda arguments: expression
new_list = [ expression for x in a_list for y in b_list List comprehension
if condition ]
new_list = { expression for x in a_list for y in b_list Set comprehension
if condition } (removes repeats)
Amaral Lab 17
Exception handling
Exception Type Type
SyntaxError Syntax
IndentationError Syntax
NameError Access Calling object that has not been created
AttributeError Access Attribute does not exist in object
KeyError Access Key does not exist in dictionary or set
IndexError Access Index does not exist in list or tuple
ZeroDivisionError Operation
TypeError Operation Types cannot be operated together
ValueError Operation Value cannot be operated upon
Amaral Lab 18