Introductory document to Python for Python for non-programmers courses
Python for Non-programmers
111 Cummington St.
This is for non-programmers.
The first half is very gentle.
The second half is more in depth.
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
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
Get ready to type this is definitely interactive.
Python is named after the BBC show
”Monty Python’s Flying Circus”
Python is named after the BBC show
”Monty Python’s Flying Circus”
We will focus on Python 2 today.
Python is named after the BBC show
”Monty Python’s Flying Circus”
We will focus on Python 2 today.
How to get Python on Katanaand on Windowsor Mac
Python is named after the BBC show
”Monty Python’s Flying Circus”
We will focus on Python 2 today.
How to get Python on Katanaand on Windowsor Mac
Python can be run interactively.
Python can be run interactively.
Code ⇒ execution is almost instant; No explicit compilation step required.
This allows for a faster development process
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:
% python
Python 2.7 (#1, Feb 28 2010, 00:02:06)
Type "help", "copyright", "credits" or "license" for more information
The >>> is a prompt asking for the next python line of code.
Sometimes you’ll see as a prompt too.
To exit, type exit() and Enter Try it!
Numbers 1
Start python (type python, then Enter), and try typing the following Addition:
2 + 2 1.5 + 2.25 |
Subtraction:
4 - 2 100 - .5 0 - 2 |
Multiplication:
2*3
... ... ...
From interactive prompt to
The Python prompt is great for quick tasks:
math, short bits of code, and testing.
For bigger projects, it’s easier to store code in a file.
One such example can be found in examples\
Script Practice: Nobel Laureates
Exit python: exit() then hit Enter The >>> is now replaced by % ; This is the Unix Prompt.
Go to examples directory with this command:
% cd examples
Run the script by typing: % python Open ’’ (command: % gedit & ) and answer these questions:
Script Practice: Nobel Laureates
Exit python: exit() then hit Enter The >>> is now replaced by % ; This is the Unix Prompt.
Go to examples directory with this command:
% cd examples
Run the script by typing: % python Open ’’ (command: % gedit & ) and answer these questions:
How do you comment code in Python?
How do you print a newline?
How do you print a multi-line string so that whitespace is preserved?
Booleans 1
A Boolean type is a type with two values: True/False. Try typing the following:
True type(True) False type(False) |
>>> True True >>> type(True) >>> False False >>> type(False) |
Booleans 2a
You can also compare values, to see if they’re equal:
0 == 0 0 == 1 |
Booleans 2b
You can also compare values, to see if they’re equal:
>>> 0 == 0 True >>> 0 == 1 False |
== (equal equal) is for equality test
= (equal) is for assignment
Be careful! This can lead to bugs!
Booleans 3
You can do other comparisons: != means not equal
"a" != "a" "a" != "A" |
Others are just like math class:
1 > 0 2 >= 3 -1 < 0 .5 <= 1 |
Booleans 3 - Output
>>> "a" != "a" False >>> "a" != "A" True >>> 1 > 0 True >>> 2 >= 3 False >>> -1 < 0 True >>> .5 <= 1 True |
Booleans 4
You can see if something is in something else:
"H" in "Hello" "X" in "Hello" |
or not:
"a" not in "abcde" "Perl" not in "Python Tutorial" |
Booleans 4 - Output
>>> "H" in "Hello" True >>> "X" in "Hello" False >>> "a" not in "abcde" False >>> "Perl" not in "Python Tutorial" True |
Flow Control 1
You can use Booleans to decide if some code should be executed:
if 6 > 5: print "Six is greater than five!" |
This is a multi-line piece of code:
if 6 > 5:
Enter
4 spaces print "Six is greater than five!" Enter
Enter again
The ” ” is a special prompt; Python realizes this is a code block.
Final enter is to signify the end of code block.
The ” ” is a special prompt; Python realizes this is a code block.
Final enter is to signify the end of code block.
>>> if 6 > 5: print "Six is greater than five!" Six is greater than five! |
What’s going on here?
if looks for a Boolean, and if it is true, the code block is executed.
The ” ” is a special prompt; Python realizes this is a code block.
Final enter is to signify the end of code block.
>>> if 6 > 5: print "Six is greater than five!" Six is greater than five! |
What’s going on here?
if looks for a Boolean, and if it is true, the code block is executed.
6 > 5 is True so the next line is executed.
Flow Control 3
Now what will happened?
if 0 > 2: print "Zero is greater than two!" if "banana" in "bananarama": print "I miss the 80s." |
Flow Control 3 - Output
>>> if 0 > 2: print "Zero is greater than two!" >>> if "banana" in "bananarama": print "I miss the 80s" I miss the 80s |
Indentation, what’s up with that?
If you’ve programmed in other languages, this indentation thing might seem weird.
Python prides itself as an easy-to-read language, and indentation makes it easy to read code blocks.
So Python requires indentation over if/end-if, begin-block/end-block organization.
Indentation - example
# this looks like other languages, # but I use a comment to organize if 1 == 1: print "Everything is going to be OK!" if 10 < 0: print "or is it?" #end if print "Inside first code block!" #end if |
Don’t use #end if, just keep it in your mind if it gets confusing
Flow Control 4
More control over choices if and else:
sister_age = 15 brother_age = 12 if sister_age > brother_age: print "sister is older" else: print "brother is older" |
else block needs to be correctly indented too. else gets executed if Boolean is False.
You don’t shouldn’t hit Enter twice between if code block and else statement.
Compound Conditionals 1
and and or allow you to combine tests. and: True only if both are True or: True if at least one is True Try these:
1 > 0 and 1 < 2 1 < 2 and "x" in "abc" "a" in "hello" or "e" in "hello" 1 <= 0 or "a" not in "abc" |
ompound Conditionals 1 - Output
>>> 1 > 0 and 1 < 2 True >>> 1 < 2 and "x" in "abc" False >>> "a" in "hello" or "e" in "hello" True >>> 1 <= 0 or "a" not in "abc" False |
Compound Conditionals 2
Try this:
temperature = 32 if temperature > 60 and temperature < 75: print "It’s nice and cozy in here!" else: print "Too extreme for me." |
Compound Conditionals 2 - Output
>>> temperature = 32 >>> if temperature > 60 and temperature < 75: print "It’s nice and cozy in here!" else: print "Too extreme for me." Too extreme for me. |
Compound Conditions 3
And try this:
hour = 11 if hour < 7 or hour > 23: print "Go away!" print "I’m sleeping!" else: print "Welcome to the cheese shop!" print "Can I interest you in some choice gouda?" |
Compound Conditions 3 - Output
>>> hour = 11 >>> if hour < 7 or hour > 23: print "Go away!" print "I’m sleeping!" else: print "Welcome to the cheese shop!" print "Can I interest you in some choice gouda?" Welcome to the cheese shop! Can I interest you in some choice gouda? |
Flow Control 5
There’s also elif:
sister_age = 15 brother_age = 12 if sister_age > brother_age: print "sister is older" elif sister_age == brother_age: print "sister and brother are the same age" else: print "brother is older" |
Flow Control 5 - Output
>>> sister_age = 15 >>> brother_age = 12 >>> if sister_age > brother_age: print "sister is older" elif sister_age == brother_age: print "sister and brother are the same age" else: print "brother is older" sister is older |
else is not required at the end, just like in the first if example.
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:
length = len("Mississippi") |
This assigns the number of characters in the string ”Mississippi” to the variable length.
Functions: Step 1
Write the function signature, how it will be called: def, Tells Python you’re defining a function.
Then a space, and the function’s name.
Then an open parenthesis.
Then a comma-separated list of parameters Then a closing parenthesis.
And finally a colon, ’:’.
def myFunction(): |
or
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
def add(x, y): result = x + y |
Return something (if you want to). return tells python to return a result.
def add(x, y): result = x + y return result |
3 or shorter .
def add(x, y): return x + y |
In Python you can return anything: strings, booleans even other functions!
Once return is called, the work in the function ends:
def absoluteValue(number): if number < 0: return number * -1 return number |
Once return is called, the work in the function ends:
def absoluteValue(number): if number < 0: return number * -1 return number |
This code have also been written like:
def absoluteValue(number): if number < 0: return number * -1 else: return number |
Functions: Step 4
Use them! Again and again and again .
def add(x, y): return x + y result = add(1234, 5678) print result result = add(-1.5, .5) print result |
Functions: Step 4
Use them! Again and again and again .
def add(x, y): return x + y result = add(1234, 5678) print result result = add(-1.5, .5) print result |
Keep in mind, functions don’t have to return something, but they usually do.
End of Part 1
Thanks!
Fill out the survey please!
Resources
Like this tutorial: Boston_Python_Workshop_6/Friday A good place to practice:
Much more detail: