welcome (back) to python!
Basics: data types (and operations & calculations)
… back in 2015 …
---- file contents ----
from __future__ import division
# the first line fixes the “division” problem in python 2
# this is a comment. Comments start with “#”, and are not interpreted!
name = “Marc” # comments can also go after stuff
print “Hello”, name # print multiple things in one line with “,”
# let's get some user input
a = int(raw_input(“Write a number, any number:”))
# some basic math
b = 10 * a
# and finally, printing the result
print a, “multiplied by 10 equals:”, b
The plan!
Basics: data types (and operations & calculations)
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
The notion of “data type” is very important:
- is 1.0 the same as “1.0” ?
- is “1.0” the same as ‘1.0000’ ?
- is 1 the same as 1.0 ?
integers, floats, strings & booleans
---- file contents ----
from __future__ import division
a = 12
b = 2.5 # “decimal numbers” are called “floating point” in python
c = a + b
a = 3 # we can re-assign variables
d = c / a # remember the first line makes division work as expected
e = (d + c) % a # The modulus ‘%’ is the remainder of division
print a, b, c, d, e
# variable names don’t need to be letters
one_result = (a + d) ** c
print “The first result is:”, result_one
# but there are limits to variable names
2_result = (a - b) ** c
print “The next result is:”, 2_result
oh no = (a + c) / (b +d)
print “The next result is:”, oh no
Numbers: integers & floats
… no spaces
… cannot begin with number: 1, 2, 3 …
… 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)
+ addition
- subtraction
* multiplication
/ division
** exponent
% modulus
---- file contents ----
word = “Hello”
letter = ‘X’ # single quotes are OK too!
sentence = “Welcome to strings!”
# notice spaces in the print below?
print word, letter, sentence
# The ‘+’ and ‘*’ operators also have functions on strings
cat = word + letter
print cat
dog = word * 5
print dog
Characters, words & sentences: Strings
+ concatenate
* copy
---- file contents ----
first = raw_input(“What is your first name?”)
last = raw_input(“What is your last name?”)
full = first + last
print full
a = raw_input(“Please enter a number:”)
b = raw_input(“Please enter another one:”)
c = a + b
print c
# converting a string to an integer
a = raw_input(“Please enter a number:”)
b = raw_input(“Please enter another one:”)
c = int(a) + int(b)
print c
Converting between types
int() convert to an integer
str() convert to a string
float() convert to a float
Converting between types
---- file contents ----
a = raw_input(“Number: ”)
b = raw_input(“Another one number: ”)
c = int(a) + int(b)
print “Result:”, c
a = int(raw_input(“Number: ”))
b = int(raw_input(“Another number: ”))
c = a + b
print “Result:”, c
a = raw_input(“Number: ”)
b = raw_input(“Another one number: ”)
c = int(a) + int(b)
print int(“Result:”, c)
a = raw_input(“Number: ”)
b = raw_input(“Another number: ”)
print “Result:”, int(a) + int(b)
a = raw_input(“Number: ”)
b = raw_input(“Another number: ”)
c = int(a + b)
print “Result:”, c
a = raw_input(“Number: ”)
b = raw_input(“Another number: ”)
c = int(a) + int(b)
print “Result:”, str(c)
a = raw_input(“Number: ”)
b = raw_input(“Another number: ”)
c = str(int(a) + int(b))
print “Result:”, c
---- file contents ----
a = raw_input(“Please enter a number”)
b = raw_input(“Please enter another one”)
print “a is type:”, type(a)
print “b is type:”, type(b)
c = int(a) + int(b)
print “c is type:”, type(c)
print c
Converting between types
int() convert to an integer
str() convert to a string
float() convert to a float
type() determine the type of a variable
---- file contents ----
a = 5
b = 10
c = 5
# testing for equality
print “Does a equal b?”, a == b
print “Does a equal c?”, a == c
# tests result in boolean variables
x = (a == b) # the parenthesis are not really needed
print “Does a equal b?”, x
print “The type of x is:”, type(x)
print “Is a larger than 5?”, x > 5
# tests are (almost all) type specific!
print 1.0 == “1.0”
print “1.0” == “1.0000”
print 1 == 1.0
True or False: Boolean
= = equal
! = not equal
> greater than
>= greater or equal than
< less than
<= less or equal than
… python is “smart” enough to realize
that 1 and 1.0 (as integers and float) are
the same number.
- is 1.0 the same as “1.0” ?
- is “1.0” the same as ‘1.0000’ ?
- is 1 the same as 1.0 ?
Some quick tests…
type('3.14')
<type ‘str’>
type(3.14)
<type ‘float’>
type(3)
<type ‘int’>
type(True)
<type ‘bool’>
type(3>1)
<type ‘bool’>
float('3.14')
3.14 (float)
int('3')
3 (int)
int('Python')
err
float('Python')
err
int(3.9)
3 (int)
int(‘3.9’)
err
float(3)
3.0 (float)
float(-3.14)
-3.14 (float)
str(-3.14)
“-3.14” (str)
str(Python)
err
str(x = 0)
err
_x = 0
_x, int value 0
1x = 0
err
-x = 0
err
x + y = 0
err
x = 0.0
x, float 0.0
x = ## 0.0
err
0 == 0.0
True (bool)
'0' == '0.0'
False (bool)
x = '0'
x, str value ‘0’
x = '0' + '0'
x, str value ‘00’
x = 2 * '0'
x, str value ‘00’
y = 'x' + 'x'
y, str value ‘xx’
y = '2' + '1'
y, str value ‘21’
y = '2' - '1'
err
Mashing it all together… exercices!
- Write a script that prompts a user for 3 numbers, and prints the following values
to the screen:
- The sum of the three
- The average of the three
- The maximum of the three (google some if you need to)
cheatsheet = = equal
! = not equal
> greater than
>= greater or equal than
< less than
<= less or equal than
int() convert to an integer
str() convert to a string
float() convert to a float
type() determine the type of a variable
+ addition
- subtraction
* multiplication
/ division
** exponent
% modulus
+ concatenate
* copy
raw_input(“text”) prompt user input (string)
print “some”, 1, 2, “values” raw_input(“text”) prints (any) values to the screen
strings
type conversion
conditions / equality
numbers
(user) input and output

Class 2: Welcome part 2

  • 1.
    welcome (back) topython! Basics: data types (and operations & calculations)
  • 2.
    … back in2015 … ---- file contents ---- from __future__ import division # the first line fixes the “division” problem in python 2 # this is a comment. Comments start with “#”, and are not interpreted! name = “Marc” # comments can also go after stuff print “Hello”, name # print multiple things in one line with “,” # let's get some user input a = int(raw_input(“Write a number, any number:”)) # some basic math b = 10 * a # and finally, printing the result print a, “multiplied by 10 equals:”, b
  • 3.
    The plan! Basics: datatypes (and operations & calculations) 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
  • 4.
    The notion of“data type” is very important: - is 1.0 the same as “1.0” ? - is “1.0” the same as ‘1.0000’ ? - is 1 the same as 1.0 ? integers, floats, strings & booleans
  • 5.
    ---- file contents---- from __future__ import division a = 12 b = 2.5 # “decimal numbers” are called “floating point” in python c = a + b a = 3 # we can re-assign variables d = c / a # remember the first line makes division work as expected e = (d + c) % a # The modulus ‘%’ is the remainder of division print a, b, c, d, e # variable names don’t need to be letters one_result = (a + d) ** c print “The first result is:”, result_one # but there are limits to variable names 2_result = (a - b) ** c print “The next result is:”, 2_result oh no = (a + c) / (b +d) print “The next result is:”, oh no Numbers: integers & floats … no spaces … cannot begin with number: 1, 2, 3 … … 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) + addition - subtraction * multiplication / division ** exponent % modulus
  • 6.
    ---- file contents---- word = “Hello” letter = ‘X’ # single quotes are OK too! sentence = “Welcome to strings!” # notice spaces in the print below? print word, letter, sentence # The ‘+’ and ‘*’ operators also have functions on strings cat = word + letter print cat dog = word * 5 print dog Characters, words & sentences: Strings + concatenate * copy
  • 7.
    ---- file contents---- first = raw_input(“What is your first name?”) last = raw_input(“What is your last name?”) full = first + last print full a = raw_input(“Please enter a number:”) b = raw_input(“Please enter another one:”) c = a + b print c # converting a string to an integer a = raw_input(“Please enter a number:”) b = raw_input(“Please enter another one:”) c = int(a) + int(b) print c Converting between types int() convert to an integer str() convert to a string float() convert to a float
  • 8.
    Converting between types ----file contents ---- a = raw_input(“Number: ”) b = raw_input(“Another one number: ”) c = int(a) + int(b) print “Result:”, c a = int(raw_input(“Number: ”)) b = int(raw_input(“Another number: ”)) c = a + b print “Result:”, c a = raw_input(“Number: ”) b = raw_input(“Another one number: ”) c = int(a) + int(b) print int(“Result:”, c) a = raw_input(“Number: ”) b = raw_input(“Another number: ”) print “Result:”, int(a) + int(b) a = raw_input(“Number: ”) b = raw_input(“Another number: ”) c = int(a + b) print “Result:”, c a = raw_input(“Number: ”) b = raw_input(“Another number: ”) c = int(a) + int(b) print “Result:”, str(c) a = raw_input(“Number: ”) b = raw_input(“Another number: ”) c = str(int(a) + int(b)) print “Result:”, c
  • 9.
    ---- file contents---- a = raw_input(“Please enter a number”) b = raw_input(“Please enter another one”) print “a is type:”, type(a) print “b is type:”, type(b) c = int(a) + int(b) print “c is type:”, type(c) print c Converting between types int() convert to an integer str() convert to a string float() convert to a float type() determine the type of a variable
  • 10.
    ---- file contents---- a = 5 b = 10 c = 5 # testing for equality print “Does a equal b?”, a == b print “Does a equal c?”, a == c # tests result in boolean variables x = (a == b) # the parenthesis are not really needed print “Does a equal b?”, x print “The type of x is:”, type(x) print “Is a larger than 5?”, x > 5 # tests are (almost all) type specific! print 1.0 == “1.0” print “1.0” == “1.0000” print 1 == 1.0 True or False: Boolean = = equal ! = not equal > greater than >= greater or equal than < less than <= less or equal than … python is “smart” enough to realize that 1 and 1.0 (as integers and float) are the same number. - is 1.0 the same as “1.0” ? - is “1.0” the same as ‘1.0000’ ? - is 1 the same as 1.0 ?
  • 11.
    Some quick tests… type('3.14') <type‘str’> type(3.14) <type ‘float’> type(3) <type ‘int’> type(True) <type ‘bool’> type(3>1) <type ‘bool’> float('3.14') 3.14 (float) int('3') 3 (int) int('Python') err float('Python') err int(3.9) 3 (int) int(‘3.9’) err float(3) 3.0 (float) float(-3.14) -3.14 (float) str(-3.14) “-3.14” (str) str(Python) err str(x = 0) err _x = 0 _x, int value 0 1x = 0 err -x = 0 err x + y = 0 err x = 0.0 x, float 0.0 x = ## 0.0 err 0 == 0.0 True (bool) '0' == '0.0' False (bool) x = '0' x, str value ‘0’ x = '0' + '0' x, str value ‘00’ x = 2 * '0' x, str value ‘00’ y = 'x' + 'x' y, str value ‘xx’ y = '2' + '1' y, str value ‘21’ y = '2' - '1' err
  • 12.
    Mashing it alltogether… exercices! - Write a script that prompts a user for 3 numbers, and prints the following values to the screen: - The sum of the three - The average of the three - The maximum of the three (google some if you need to)
  • 13.
    cheatsheet = =equal ! = not equal > greater than >= greater or equal than < less than <= less or equal than int() convert to an integer str() convert to a string float() convert to a float type() determine the type of a variable + addition - subtraction * multiplication / division ** exponent % modulus + concatenate * copy raw_input(“text”) prompt user input (string) print “some”, 1, 2, “values” raw_input(“text”) prints (any) values to the screen strings type conversion conditions / equality numbers (user) input and output