2. The plan!
Basics: data types (and operations & calculations)
Basics: conditionals & iteration (+ recap)
Basics: lists, tuples, dictionaries
Basics: writing functions
Reading & writing files: opening, parsing & formats
Working with numbers: numpy & scipy
Making plots: matplotlib & pylab
… if you guys want more after all of that …
Writing better code: functions, pep8, classes
Working with numbers: Numpy & Scipy (advanced)
Other modules: Pandas & Scikit-learn
Interactive notebooks: ipython & jupyter
Advanced topics: virtual environments & version control
3. recap: ints, floats, strings & booleans
ints & floats:
+ addition
- subtraction
* multiplication
/ division
** exponent
% modulus
variable names:
… no spaces
… cannot begin with number: 1, 2 …
… cannot contain ?$@# (except _)
… cannot be a “built-in” name: and, or, int,
float, str, char, exec, file, open, object, print,
quit… (approx. 80 reserved names)
comparisons:
= = equal
! = not equal
> greater than
>= greater or equal than
< less than
<= less or equal than
basic data types:
integer, float, string, boolean
converting types :
int(), float(), str(), type()
the linux command line:
ls : list files & folders
cd directory : change directory
to run a python file:
python filename.py
user input:
a = raw_input(“write stuff:”)
strings:
+ concatenate
* copy
printing:
print “literal text”, var1, var2, “more text”
4. recap: if - else …
# basic “if - else” block
# indentation matters!!!
if boolean :
the “True” code goes here
else:
the “False” code goes here
# basic “if - else” block
if chairs < lions :
print “Run away!”
else:
print “You are OK!”
if chairs < lions :
print “There are”, lions, “lions.”
print “Run away!”
else:
print “You are OK”
print “That’s all for now folks!”
Indentation IS the logic of
your code!
non-logical and uneven
indentation makes python
angry
The first line at the same level as the “if” begins
the rest of the code (or program)
the “else” goes at the
same level as the “if”
(because they belong
together)
5. # “if - elif - else”
# blocks are tested in order
# only the first True “if” or “elif” is handled
if (boolean and/or boolean…) :
the “True” code goes here
elif (boolean and/or boolean…) :
next “True” code goes here
else:
the “False” code goes here
recap: if - else …
# “if - elif - else”
if chairs < lions :
print “Run away!”
elif chairs == lions ::
print “You got lucky this time.”
print “Buy more chairs!”
else:
print “Run away!”
6. # “if - elif - else”
# blocks are tested in order
# only the first True “if” or “elif” is handled
if (boolean and/or boolean…) :
the “True” code goes here
elif (boolean and/or boolean…) :
next “True” code goes here
else:
the “False” code goes here
recap: if - else …
# “if - elif - else”
if chairs < lions :
print “Run away!”
elif chairs == lions ::
print “You got lucky this time.”
print “Buy more chairs!”
else:
print “Run away!”
# “if...”
if chairs < lions :
print “Run away!”
elif chairs == lions ::
print “Buy chairs tomorrow!”
print “Program has finished!”
the “else” is not
required. Sometimes
there is just “nothing
else” to do :)
7. recap: if - else …
# combinatorial logic
if (boolean and/or boolean…) :
the “True” code goes here
else:
the “False” code goes here
# combinatorial logic
if (traps > bears) and (chairs > lions):
print “your are OK”
else:
print “Run away!”
8. recap: if - else …
# combinatorial logic
if (boolean and/or boolean…) :
the “True” code goes here
else:
the “False” code goes here
# combinatorial logic
if (traps > bears) and (chairs > lions):
print “your are OK”
else:
print “Run away!”
# nested if
if boolean:
if boolean:
“True True” code here
else:
“True False” code here
else:
“False” code here
# nested if
if lions > 0:
if chairs < lions :
print “Run away”
else:
print “You are OK”
else:
print “There are no lions”
9. “if else” exercises
- Write a script that asks the user 3 times to insert a single digit number. These will
represent the digit for Hundreds, Tens and Unit/Single (of a three digit number).
The script should output:
- Whether the number is larger than 500
- The number reversed
- Whether the reversed number is larger than 500
- Whether the number is a ‘palindrome’
- Write a scripts that asks the user for a number (between 0 and 999), and tells the
user whether:
- The number is larger than 100
- The number is even
- Lastly: If the number is larger than 200, but ONLY if the number is even (otherwise it prints
nothing)
10. loops and iteration
… for all numbers between 100 and 500…
… for each row in a table …
… for each image in collection of files …
… for each pixel in an image …
… while the total number is less than 1200 …
… until you find the first value bigger than 500 …
NOTE: this is c++ , not python ;)
11. the almighty “for … in … range”
---- file contents ----
# iterating over a range of numbers is VERY common
for i in range(10):
print i
# less trivial: 2 ** the first 10 numbers
for i in range(10):
print “2 to the power”, i, “is:”, 2 ** i
# optional parameters to “range”
for x in range(5, 10):
print x
for x in range(5, 10, 2):
print x
# going backwards…
for x in range(20, 15, -1):
print x
# the basic “for - in - range” loop
for x in range(stop):
code to repeat goes here…
again, indentation is
everything in python!
# the basic “for - in - range” loop
# “start” and “step” are optional
for x in range(start, stop, step):
code to repeat goes here…
0 → stop - 1
12. the almighty “for … in … range”
# sum of all numbers < 100
total = 0
for i in range(100):
total = total + i
print “The total is:”, total
# show all even numbers
# between 100 and 200
for i in range(100, 200):
if i % 2 == 0:
print i, “is even.”
# another way...
for i in range(100, 200, 2):
print i, “is even.”
# the basic “for - in - range” loop
for x in range(stop):
code to repeat goes here…
again, indentation is
everything in python!
# the basic “for - in - range” loop
# “start” and “step” are optional
for x in range(start, stop, step):
code to repeat goes here…
0 → stop - 1
13. the “while loop”
---- file contents ----
# adding numbers, until we reach 100
total = 0
while total <= 100:
n = int(raw_input(“Please write a number:”))
total = total + n
print “The total exceeded 100. It is:”, total
# adding numbers, until we reach 100
user_input = “”
while user_input != “stop”:
user_input = raw_input(“Type ‘stop’ to stop:”)
# the basic “while ” loop
while boolean:
code goes here
again, indentation!
NEVER use while loops!
okay… not “never”, but a “for loop” is
almost always better!
14. okay, some more exercises
Basic practice:
- Write a script that asks the user for two numbers and calculates:
- The sum of all numbers between the two.
- Write a script that asks the user for a single number and calculates:
- The sum of all even numbers between 0 and that number.
A little more complex:
- Write a script that asks the user for a single number and calculates whether or not
the number is prime.
- Write a script that asks the user for a single number and finds all prime numbers
less than that number.
15. Let’s write the following variants of the lion/chair/hungry program. They start as
above, but:
- If the ‘user’ does not write ‘yes’ or ‘no’ when answering ‘hungry?’, keeps on asking
them for some useful information.
Back to lions and bears
# the beginning of the lion / hungry program from last week…
chairs = int(raw_input(“How many chairs do you have?:”))
lions = int(raw_input(“How many lions are there?:”))
hungry = raw_input(“Are the lions hungry? (yes/no)?:”)
… etc…