2. The plan!
Basics: data types (and operations & calculations) ← recap
Basics: conditionals & iteration
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
---- file contents ----
from __future__ import division
# the “+” operation works differently on strings and integers
print “Python”, 1 + 3, “the win!”
print “1” + “3”, “is an unlucky number.”
# integers (and floats) are great for maths
# we can use variables to store values
two = 3 - 1
print “Python is”, two “much fun.”
# strings are for text manipulation
first_numbers = “1” + “2” + “3”
print “The first few numbers are:”, first_numbers
print “Lots of nonsense:” first_numbers * 123
ints & floats
+ addition
- subtraction
* multiplication
/ division
** exponent
% modulus
strings
+ concatenate
* copy
… 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)
4. recap: ints, floats, strings & booleans
---- file contents ----
from __future__ import division
# “raw_input” always returns a string
number = raw_input(“Gimme a number:”)
print “Here is your number 3 times:” number * 3
# we can use functions to check and change the types
print “number is a:” type(number)
new_number = int(number)
print “new_number is a:” type(new_number)
print “Here is your number times 3 :” new_number * 3
# boolean values and equality testing
number = 3
print “Does number equal 3?”, number == 3
print “Is number > 10? ” number ?”, 3 > 10
int() convert to an integer
str() convert to a string
float() convert to a float
type() determine the type of a variable
= = equal
! = not equal
> greater than
>= greater or equal than
< less than
<= less or equal than
5. Some exercises...
- Write a script that asks the user for their height (in meters), and prints their
height in imperial:
- version 1: prints the result using only feet (1m = 3.28084ft)
- version 2: prints the result using feet and inches, to the nearest inch. (1ft = 12 inches)
6. Making choices
---- file good_advice.py ----
from __future__ import division
# “raw_input” always returns a string
chairs = int(raw_input(“How many chairs do you have?:”))
lions = int(raw_input(“How many lions are there?:”))
# here comes an “if else” block
if chairs >= lions:
# everything “indented” is part of the “true” block
print “You should be fine.”
# so you can write as much code as you want
print “Really, you’ll be fine!”
else:
print “Run away!!!”
# structure of an “if else” block
if boolean :
the “True” code goes here
else:
the “False” code goes here
7. Making choices: elif
---- file good_advice.py ----
from __future__ import division
# “raw_input” always returns a string
chairs = int(raw_input(“How many chairs do you have?:”))
lions = int(raw_input(“How many lions are there?:”))
# here comes an “if else” block.
if chairs > lions:
# everything “indented” is part of the “true” block
print “You should be fine.”
# so you can write as much code as you want
print “Really, you’ll be fine!”
elif chairs == lions: # “elif” is short for “else, if”
print “You’re OK.”
print “But should get some more chairs soon!”
else:
print “Run away!!!”
# structure of an “if elif else” block
# Only ONE block is executed!
if boolean :
the “True” code goes here
elif boolean :
next “True” code goes here
else:
the “False” code goes here
8. Making complex choices: nesting
---- file good_advice.py ----
from __future__ import division
# “raw_input” always returns a string
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)?:”)
# only worry about chairs if the lions are hungry
if hungry == ‘yes’:
# you can “nest” one “if” in another
if chairs > lions:
# notice the extra indentation?
print “You’ll be fine!”
else:
print “Run away!”
else:
print “The lions are not hungry right now.”
# structure of an “if else” block
if boolean :
if boolean :
… code goes here
else:
… code goes here
else:
… code goes here
9. Making complex choices: nesting
---- file good_advice.py ----
from __future__ import division
# “raw_input” always returns a string
chairs = int(raw_input(“How many chairs do you have?:”))
lions = int(raw_input(“How many lions are there?:”))
traps = int(raw_input(“How many traps do you have?:”))
bears = int(raw_input(“How many bears are there?:”))
# assume traps work against bears
# and chairs against lions
if (traps >= bears) and (chairs >= lions):
print “You’ll be fine!”
else:
print “Run away!”
# structure of an “if else” block
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
10. Making complex choices: nesting
---- file good_advice.py ----
from __future__ import division
# “raw_input” always returns a string
chairs = int(raw_input(“How many chairs do you have?:”))
lions = int(raw_input(“How many lions are there?:”))
traps = int(raw_input(“How many traps do you have?:”))
bears = int(raw_input(“How many bears are there?:”))
# assume traps work against bears
# and chairs against lions
if (traps < bears) or (chairs < lions):
print “Run away!”
else:
print “You’ll be fine!”
# structure of an “if else” block
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
11. 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?’, tells them that
their choice is not valid, and nothing else.
Exercise!
---- file good_advice.py ----
from __future__ import division
# “raw_input” always returns a string
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)?:”)
12. Moar exercises
- Write a script that asks the user 3 times to insert a number: First hours, next
minutes, and last seconds.
- The script should output the time in seconds only.
- Bonus: Write a script that does the opposite (i.e asks for seconds, and writes hr/min/sec)