2. Overview for Today
1. Setting up Python
2. Background on Python
3. Basics of Python
4. Data Structures in Python
5. Control Structures in Python
6. Functions in Python
3. Things you should do
Ask questions!
If you are confused, there is a 80% chance that
someone else is even more confused.
Participate!
We're all friends here. Tell us if we're going too slowly.
Tell us if we're boring. Tell us if have an idea for
something.
Follow along!
Open up Python and try stuff!
7. What is Python?
● A language
● An interpreter
● A reference to a British sketch comedy:
"Monty Python's Flying Circus"
(The documentation is full of jokes referencing the
show)
8. Why Python?
● Traditional languages (C++, C, Java)
evolved for large-scale programming
● Scripting language (Perl, Python) evolved
for simplicity and flexibility
9. The Python Language
● Free!
● Elegant and Powerful!
● Cross-Platform!
● Tons of useful modules!
● Chock full of technical buzzwords!
○ Object-oriented, dynamic-typing
10. The "Zen" of Python
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
...
Readability Counts
... (It's actually a lot longer)
12. Printing in Python
>>> print "Hello World."
Hello World.
Meanwhile, in Java land:
public class MyFirstProgram {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
13. Input in Python
>>> input("Tell me something: ")
Tell me something:
Type something in and press enter
14. Comments in Python
>>> # This is a comment
...
The following will NOT work:
/*
Multiline comment in Java
*/
// Single line comment in Java
15. Scripts in Python
1. In your IDE/Text Editor, write
# My first program!
print "Hello", "World"
2. Save the file as helloworld.py
3. From the terminal,
> python helloworld.py
Hello World.
18. Values in Python
-5 # Integer type
23.1 # Float type
"Some words" # String type
True # Boolean type
Variables in Python
age = 23
miles_from_home = 301.36
name = "Cory Bart"
has_dog = False
23. If statements
if has_dogs:
print "Has dogs"
elif age >= 25:
print "Can rent a car"
elif name == "Ellie" or name == "Becky"
print "Is awesome!"
else:
print "Not a great fellow"
*Note the white-space!*
25. Exercise
Write a function that can compute tips for a
meal, given the price and how generous the
user is.
# price: float
# generous: string, either "stingy"
# or "generous"
# returns a float indicating tip
def calculate_tip(price, generosity):
...
27. Sequences
Lists are Mutable and Ordered
a_list = [1, 3, 2]
Tuples are Immutable and Ordered
a_tuple = (1, 2, 3)
Sets are Mutable, Unordered, and Unique
a_set = set([1, 2, 3])
Dictionaries are Key-Value pairs
a_dictionary = {'A': 1, 'B': 2, 'C': 3}
Strings are immutable sequences of one-character Strings!
a_string = "Also a sequence!"
28. Working with a list
colors = ['red', 'blue', 'yellow', 'purple']
Add to a list
colors.append('green') or colors.insert
(index)
Remove from a list
colors.pop() or colors.remove(index)
32. Working with sequences
Get an element of a sequence:
"Puzzle"[2]
Get the length of a sequence
len(set([1, 2, 3, 4, 5, 6]))
Is something in a sequence?
5 in (1, 2, 3, 4, 5)
Get a sublist from a list
['red', 'blue', 'yellow', 'green'][1:2]
There are many built-in functions for sequences
sum((1, 2, 3, 4))
min((5, 10, 15))
33. Strings have a ton of methods!
"1, 2, 3, 4".split(",")
",".join([1,2,3,4])
" whitespace ".strip()
"I am 22".replace("am", "was")
"HeLlO wOrLd".swapcase()
36. Looping
The for-each loop is best!
List
for item in [1, 2, 3]: Set
print item Tuple
String
File handler
You can also use a while loop.
while True:
# do something
40. Exercise
Write a program that will "translate" a sentence
into Pyglatin:
"We only speak Pyglatin in Pyrome"
->
"Eway onlyay peaksay Yglatinpay inay yromeay"
41. List comprehensions
You can make a list by sticking a for loop inside!
[value*2 for value in range(10)]
[int(number) for number in "123456789"]
42. File I/O
file = open('word_lists.txt', 'r')
for line in file:
print line
file = open('numbers.txt', 'w')
for number in range(100):
file.write(number)
44. Command line arguments
>python file.py this is a test
Inside of file.py
import sys
print sys.args
Outputs
['this', 'is', 'a', 'test']
45. What should I do next?
The Python Challenge
http://www.pythonchallenge.com/
Online Books:
http://tinyurl.com/interactivepython
46. Feedback
● Facebook - http://tinyurl.com/letscode
● Would you be interested in this formulating
into a meetup group?
● How often?
● What would you like to learn next?
● Does the location matter?
● Any other feedback?
● How large/small class size would you
prefer?