Python For Non-Programmers: A Gentle Introduction 1
Python For Non-Programmers: A Gentle Introduction 1
A Gentle Introduction 1
Yann Tambouret
Scientific Computing and Visualization
Information Services & Technology
Boston University
111 Cummington St.
[email protected]
Winter 2013
This Tutorial
This is for non-programmers.
The first half is very gentle.
The second half is more in depth.
This Tutorial
This is for non-programmers.
The first half is very gentle.
The second half is more in depth.
If you have any programming experience, feel free to
entertain yourself at codingbat.com/python
This Tutorial
This is for non-programmers.
The first half is very gentle.
The second half is more in depth.
If you have any programming experience, feel free to
entertain yourself at codingbat.com/python
Get ready to type... this is definitely interactive.
Python Overview
Python is named after the BBC show
”Monty Python’s Flying Circus”
Python Overview
Python is named after the BBC show
”Monty Python’s Flying Circus”
We will focus on Python 2 today.
Python Overview
Python is named after the BBC show
”Monty Python’s Flying Circus”
We will focus on Python 2 today.
How to get Python on Katana and on Windows or
Mac
Python Overview
Python is named after the BBC show
”Monty Python’s Flying Circus”
We will focus on Python 2 today.
How to get Python on Katana and on Windows or
Mac
This tutorial borrows largely from a tutorial by the
Boston Python Group
Python is Interpreted
Python can be run interactively.
Python is Interpreted
Python can be run interactively.
Code ⇒ execution is almost instant;
No explicit compilation step required.
This allows for a faster development process
Python is Interpreted
Python can be run interactively.
Code ⇒ execution is almost instant;
No explicit compilation step required.
This allows for a faster development process
The final product is usually more resource intensive,
and as a side effect slower then comparable
C/Fortran code.
Python is Interactive
Practice running python, type python in the terminal,
hit Enter:
1 % python
2 Python 2.7 ( # 1 , Feb 28 2010 , 00:02:06)
3 Type " help " , " copyright " , " credits " or " license " for more information .
4 >>>
Numbers 1
Start python (type python, then Enter), and try typing
the following Addition:
1 2 + 2
2 1.5 + 2.25
Subtraction:
1 4 - 2
2 100 - .5
3 0 - 2
Multiplication:
1 2 * 3
Numbers 1 - Output
1 >>> 2 + 2
2 4
3 >>> 1.5 + 2.25
4 3.75
5 >>> 4 - 2
6 2
7 >>> 100 - .5
8 99.5
9 >>> 0 - 2
10 -2
11 >>> 2 * 3
12 6
Numbers 2
Division:
1 4 / 2
2 1 / 2
3 1.0 / 2
4 3/4 + 1/4
5 3.0/4 + 1.0/4
6 3.0/4.0 + 1.0/4.0
Numbers 2 - Output
1 >>> 4/2
2 2
3 >>> 1/2
4 0
5 >>> 1.0/2
6 0.5
7 >>> 3/4 + 1/4
8 0
9 >>> 3.0/4 + 1.0/4
10 1.0
11 >>> 3.0/4.0 + 1.0/4.0
12 1.0
type()
type() is a function that tells you what data type
Python thinks something is. Try:
1 type (1)
2 type (1.0)
type()
type() is a function that tells you what data type
Python thinks something is. Try:
1 type (1)
2 type (1.0)
results in:
1 >>> type (1)
2 < type ’ int ’ >
3 >>> type (1.0)
4 < type ’ float ’ >
type()
type() is a function that tells you what data type
Python thinks something is. Try:
1 type (1)
2 type (1.0)
results in:
1 >>> type (1)
2 < type ’ int ’ >
3 >>> type (1.0)
4 < type ’ float ’ >
Tip
Press the up arrow a few times in the terminal.
The Python Interpreter saves a history of what you
type.
Pressing up allows you to access previous lines.
Hit return and you re-run a command.
Variables 1
Python variables can be made of any data type.
Giving a name to some value is called assignment.
Variable names cannot have spaces, and they need
to start with a letter.
Try typing:
1 type (4)
2 x = 4
3 x
4 type ( x )
5 2 * x
Variables 1 - output
and we get:
1 >>> type (4)
2 < type ’ int ’ >
3 >>> x = 4
4 >>> x
5 4
6 >>> type ( x )
7 < type ’ int ’ >
8 >>> 2 * x
9 8
Note on Output
Just typing a value and the interpreter spits it back out
at you. If you assign 4 to a variable, nothing is printed.
1 >>> 4
2 4
3 >>> x = 4
Variables 2
Reassignment is possible:
1 >>> x = 4
2 >>> x
3 4
4 >>> x = 5
5 >>> x
6 5
Variables 2
Reassignment is possible:
1 >>> x = 4
2 >>> x
3 4
4 >>> x = 5
5 >>> x
6 5
Strings 1
Strings are surrounded by quotes:
1 " Hello "
2 " Python , I ’m your #1 fan ! "
Strings 1 - Output
1 >>> " Hello "
2 ’ Hello ’
3 >>> " Python , I ’m your #1 fan ! "
4 " Python , I ’m your #1 fan ! "
5 >>> type ( " Hello " )
6 < type ’ str ’ >
7 >>> type (1)
8 < type ’ int ’ >
9 >>> type ( " 1 " )
10 < type ’ str ’ >
Strings 2
Strings can be combined (concatenated):
1 " Hello " + " World "
Strings 2
Strings can be combined (concatenated):
1 " Hello " + " World "
Strings 2 - Output
1 >>> " Hello " + " World "
2 ’ HelloWorld ’
3 >>> print " Hello " + " World "
4 HelloWorld
A traceback occurs:
TypeError is the error that occurs
cannot concatenate ’str’ and ’int’ objects is the
‘helpful’ message
and every thing from Traceback to the error tells
you where it happened
Yann - [email protected] (SCV) Python Winter 2013 21 / 56
Python as a Calculator
Strings 3
Single or Double quotes are OK:
1 print ’ Hello ’
2 print " Hello "
Strings 3 - Output
1 >>> print ’ Hello ’
2 Hello
3 >>> print " Hello "
4 Hello
5 >>> print ’I ’m a happy camper ’
6 File " < console >" , line 1
7 print ’I ’m a happy camper ’
8 ^
9 SyntaxError : invalid syntax
10 >>> print ’I \ ’ m a happy camper ’
11 I ’m a happy camper
12 >>> print " I ’m a happy camper "
13 I ’m a happy camper
14 >>> h = " Happy "
15 >>> b = " Birthday "
16 >>> print ( h + b ) * 10
17 HappyBirthdayHappyBirthdayHappyBirthdayHappyBirthdayH
Yann - [email protected] (SCV) Python Winter 2013 23 / 56
Python is a Scripting Language
Booleans 1
A Boolean type is a type with two values: True/False.
Try typing the following:
1 True
2 type ( True )
3 False
4 type ( False )
1 >>> True
2 True
3 >>> type ( True )
4 < type ’ bool ’ >
5 >>> False
6 False
7 >>> type ( False )
8 < type ’ bool ’ >
Booleans 2a
You can also compare values, to see if they’re equal:
1 0 == 0
2 0 == 1
Booleans 2b
You can also compare values, to see if they’re equal:
1 >>> 0 == 0
2 True
3 >>> 0 == 1
4 False
Booleans 3
You can do other comparisons: != means not equal
1 " a " != " a "
2 " a " != " A "
Booleans 3 - Output
1 >>> " a " != " a "
2 False
3 >>> " a " != " A "
4 True
5 >>> 1 > 0
6 True
7 >>> 2 >= 3
8 False
9 >>> -1 < 0
10 True
11 >>> .5 <= 1
12 True
Booleans 4
You can see if something is in something else:
1 " H " in " Hello "
2 " X " in " Hello "
or not:
1 " a " not in " abcde "
2 " Perl " not in " Python Tutorial "
Booleans 4 - Output
1 >>> " H " in " Hello "
2 True
3 >>> " X " in " Hello "
4 False
5 >>> " a " not in " abcde "
6 False
7 >>> " Perl " not in " Python Tutorial "
8 True
Flow Control 1
You can use Booleans to decide if some code should be
executed:
1 if 6 > 5:
2 print " Six is greater than five ! "
Flow Control 2
The ”...” is a special prompt; Python realizes this is a
code block.
Final enter is to signify the end of code block.
Flow Control 2
The ”...” is a special prompt; Python realizes this is a
code block.
Final enter is to signify the end of code block.
1 >>> if 6 > 5:
2 ... print " Six is greater than five ! "
3 ...
4 Six is greater than five !
Flow Control 2
The ”...” is a special prompt; Python realizes this is a
code block.
Final enter is to signify the end of code block.
1 >>> if 6 > 5:
2 ... print " Six is greater than five ! "
3 ...
4 Six is greater than five !
Flow Control 3
Now what will happened?
1 if 0 > 2:
2 print " Zero is greater than two ! "
3 if " banana " in " bananarama " :
4 print " I miss the 80 s . "
Indentation - example
1 # this looks like other languages ,
2 # but I use a comment to organize
3 if 1 == 1:
4 print " Everything is going to be OK ! "
5 if 10 < 0:
6 print " or is it ? "
7 # end if
8 print " Inside first code block ! "
9 # end if
Flow Control 4
More control over choices if and else:
1 sister_age = 15
2 brother_age = 12
3 if sister_age > brother_age :
4 print " sister is older "
5 else :
6 print " brother is older "
Compound Conditionals 1
and and allow you to combine tests.
or
and: True only if both are True
Try these:
1 1 > 0 and 1 < 2
2 1 < 2 and " x " in " abc "
3 " a " in " hello " or " e " in " hello "
4 1 <= 0 or " a " not in " abc "
Compound Conditionals 2
Try this:
1 temperature = 32
2 if temperature > 60 and temperature < 75:
3 print " It ’s nice and cozy in here ! "
4 else :
5 print " Too extreme for me . "
Compound Conditions 3
And try this:
1 hour = 11
2 if hour < 7 or hour > 23:
3 print " Go away ! "
4 print " I ’m sleeping ! "
5 else :
6 print " Welcome to the cheese shop ! "
7 print " Can I interest you in some choice gouda ? "
Flow Control 5
There’s also elif:
1 sister_age = 15
2 brother_age = 12
3 if sister_age > brother_age :
4 print " sister is older "
5 elif sister_age == brother_age :
6 print " sister and brother are the same age "
7 else :
8 print " brother is older "
Functions
Remember type()? Functions ...
do some useful work,
let us re-use code without having to retype it,
can take some input, and optionally return a value.
Functions
Remember type()? Functions ...
do some useful work,
let us re-use code without having to retype it,
can take some input, and optionally return a value.
You call a function by using its name, followed by its
arguments in parenthesis:
1 length = len ( " Mississippi " )
Functions: Step 1
Write the function signature, how it will be called:
1 def, Tells Python you’re defining a function.
2 Then a space, and the function’s name.
3 Then an open parenthesis.
4 Then a comma-separated list of parameters
5 Then a closing parenthesis.
6 And finally a colon, ’:’.
1 def myFunction ():
or
1 def myFunction ( myList , myInteger ):
Functions: Step 2
Do something (useful):
Underneath the function signature you do some
work.
This code must be indented, just like if/else blocks.
This tells python that it’s part of the function.
You can use variables passed as parameters just
like you used variables before
1 def add (x , y ):
2 result = x + y
Functions: Step 3
Return something (if you want to).
return tells python to return a result.
1 def add (x , y ):
2 result = x + y
3 return result
or shorter....
1 def add (x , y ):
2 return x + y
Functions: Step 3
Once return is called, the work in the function ends:
1 def absoluteValue ( number ):
2 if number < 0:
3 return number * -1
4 return number
Functions: Step 3
Once return is called, the work in the function ends:
1 def absoluteValue ( number ):
2 if number < 0:
3 return number * -1
4 return number
Functions: Step 4
Use them! Again and again and again....
1 def add (x , y ):
2 return x + y
3
4 result = add (1234 , 5678)
5 print result
6 result = add ( -1.5 , .5)
7 print result
Functions: Step 4
Use them! Again and again and again....
1 def add (x , y ):
2 return x + y
3
4 result = add (1234 , 5678)
5 print result
6 result = add ( -1.5 , .5)
7 print result
End of Part 1
Thanks!
Fill out the survey please!
Resources
Like this tutorial: https://openhatch.org/wiki/
Boston_Python_Workshop_6/Friday
A good place to practice:
http://codingbat.com/python
Much more detail:
http://docs.python.org/tutorial/