- A programming language that is compiled when you run it. (Explain JIT vs. Compiled languages like C)
- Sort files on your computer
- Do complex math
- Create websites
- Make games
- NASA - Curiosity log parsing
- bit.ly - Data analysis
- Django Framework - rapid web application development (think of it as Rails for Python)
- reddit.com - python is the backbone of one of the most visited sites in the world
- Use the interpreter ie. the python shell idle or others
- Create a text file and run that
- This is important. It helps you and others figure out what you were trying to do.
- Denote comments with
#
# This is a comment, it won't be run
-
Basic Operations
+, -, /, *
-
More Complex Operations
-
Order
(1 + 2) * 3 = 9
1 + 2 * 3 = 7
-
- Assignment is by
=
- Comparison is by
==
- Variables can be called anything, but it is best to be descriptive for you.
- Must begin with an _ or letter
- Case Sensitive
- Some words are reserved: and, del, if, not, print etc.
x = 3
x = raw_input("Enter your name")
Python 2
x = input("Enter your name")
Python 3 use just input instead
-
Strings are for you not, for the computer.
-
1 vs. '1'
x = 1
y = '1'
print x
1
print y
1
3 + y
Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type(s) for +: 'int' and 'str'
3 + x
4
-
String concatenation
-
Use it to join things
` string1 = 'Hi'
` string2 = 'there'
print string1 + string2
-
-
Analogous to variables that hold multiple values. You might recognize them as arrays if you know another language.
-
Can be indexed by their position in the.
-
Indexes start with 0 not 1.
list1 = ['a', 'b', 'c', 'd']
print list1
['a', 'b', 'c', 'd']
-
Reference items in the list by their index.
print list1[3]
d
-
Use the built-in
len
function to count how many items are in a list.len(list1)
4
-
Integers in lists
list2 = [2, 4, 6, 8]
print list2[0] + list2[1]
6
-
Another type of list.
list3 = ['apples', 'oranges', 'bananas']
list3 = [0:2]
'apples', 'oranges'
[0:2]
means a range not including the last index.
-
This is where Python's indentation is very important.
-
IF something meets our criteria, then do this.
x = 5
y = 6
if x + y == 11:
print '5 + 6 DOES equal 11'
5 + 6 does equal 11
-
Take the form
def functionname(value1, value2, …)
do something here
-
A function that adds two numbers together:
def number_adder(x, y):
print x + y
-
Utilize the function by supplying arguments:
number_adder(5, 3)
8
- Write a program that asks the user to enter their age, and name, prints it back to them.
- Expand on this and add a friend's age. Now calculate the difference between your friend and your age and tell the user.
A couple more really good resources:
http://learnpythonthehardway.org/
http://www.udacity.com/overview/Course/cs101/CourseRev/apr2012