Week 4

Introducing Python

Author:Christopher Haynes
Email:chaynes@indiana.edu
Affiliation:Indiana University
Course:BL CSCI A201
Date:2008-02-21

Contents

python-logo.png

This week

Our first Python program

# notmuch.py by chaynes@indiana.edu

print "Not much of a program, but it's Python!"

Python programs: The big picture

How can I create Python programs?

Ways of running Python programs

Some cool places Python is used

Nokia_6630.jpg

Nokia 6630 smarthphone

olpc.jpg
One Laptop Per Child project $100 computer


google.jpg


What kinds of programs can I write?

What does a program do?

Sublanguages

Python syntax: comments and whitespace

Statements and expressions

Statement execution

Expression evaluation

Literals

Strings literals (simplified)

Interactive demo: Numbers

>>> 1 + 1
2
>>> 1.2 * 3.4
4.0800000000000001
>>> 2 / 3.0
0.66666666666666663
>>> 2/3
0
>>> 2 ** 8
256
>>> 2**30
1073741824
>>> 2**31
2147483648L
>>> 2**2000
1148130695274254524232833201177681984022317702088695200477642736825766261392370\
3138566594863165062699184459646389874627734471189608630553314259313561666531853\
9129989145312280000688779148240044871428926990063486244781615463646388363947317\
0260404663539709049965581623988089446296056233116495361642219703326813441689089\
8445850560237948480791405890093477650042900271670662583052200813223628129176126\
7883317206598995396418127021779858404042159853183251540889433902091920554957783\
5896720391600819572166305827553804255837260155283487864194320545089152757838826\
25175435528800822842770817965453762184851149029376L
>>> 2.**2000
Traceback (most recent call last):
  File "", line 1, in ?
OverflowError: (34, 'Result too large')
>>> 2.**1000
1.0715086071862673e+301
>>> -1 - +2
-3
>>> -(1-2)
1
>>> 1 + 2 * 3
7
>>> 1 + (2 * 3)
7
>>> (1 + 2) * 3
9
>>> 8 / 4 / 2
1
>>> 8 / (4 / 2)
4
>>> (10 + 5) % 12  # 10 o'clock plus 5 hours is 3 o'clock
3
>>> (275 + 120) % 360  # from a heading of 275 degrees, turn right 120 degrees
35
>>> ((1 + 2) / 3)) + 4
  File "", line 1
    ((1 + 2) / 3)) + 4
                 ^
SyntaxError: invalid syntax
>>> 8 / (1 / 2)
Traceback (most recent call last):
  File "", line 1, in 
ZeroDivisionError: integer division or modulo by zero
>>> l+2
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'l' is not defined
>>> None
>>>

Interacting with Python

Take advantage of the shell -- it's fun and easy :)

Kinds of errors

Syntax error messages can be misleading

The program:

print ("missing right paren on this line"

print "error reported on this line"

results in the messge:

File "", line 3
  print "error reported on this line"
      ^
SyntaxError: invalid syntax

even thought the error is on line 1.

The position of an error report is always where the error was detected, not where the actual error in the program is located.

Types

Numeric types in Python

Common mathematical operators

Remainders

>>> (10 + 5) % 12  # 10 o'clock plus 5 hours is 3 o'clock
3
>>> # from a heading of 275 degrees, turn right 120 degrees
>>> (275 + 120) % 360
35

Unary operators

Numeric type conversion

Evaluation

What operation comes first?

Operator precedence

Parenthesized expressions

clicker Using clicker numeric keys, ., and -, what is the value of the following?

Enter zero if a syntax or runtime error will result.

Use the SYM key to the right of the zero key to enter a decimal point.

Programming language expressions are NOT mathematical expressions

Interactive demo: Variables

>>> x = 3
>>> x
3
>>> x = x + 1
>>> x
4
>>> x += 1 # same as x = x + 1
5
>>> x *= 2 # same as x = x * 2
10
>>> first_name = 'Humpty'
>>> first_name
'Humpty'
>>> First_name
Traceback (most recent call last):
  File "", line 1, in ?
NameError: name 'First_Name' is not defined
>>> 1st_name = 'Alice'
  File "", line 1
    1st_name = 'Alice'
          ^
SyntaxError: invalid syntax
>>> first name = 'Alice'
  File "", line 1
    first name = 'Alice'
             ^
SyntaxError: invalid syntax
>>> firstName = 'Alice'
>>>

Python names

Name conventions for good style

Use descriptive names

Variables

Augmented assignment statements

clicker What is printed by this statement?

print "1 + 3"
  1. "1 + 3"
  2. 4
  3. 1+3

clicker What is the value of x after the following statements?

x = 3
x = x + 6
x /= 2

(Use your clicker number pad.)

Answer: 4

clicker What is the value of b after the following statements?

a = 3
b = a
a = 4

(Use your clicker number pad.)

Answer: 3

How NOT to think about variable assignment

Warning: falling out of our sublanguage can be confusing

Demo: Built-in functions

>>> float(3)
3.0
>>> int(5 + 0.7)
5
>>> x = -5.7
>>> int(x)
-5
>>> round(-5.7)
-6.0
>>> round(17.5)
18.0

Built-in functions

Reeborg the robot programming

In this week's assigned reading

What kind of function calls are possible?

Function call syntax

Notation for grammar rules

Function calls as statements

Text error

Function definition syntax

Blocks and indentation

Function definition semantics

Assignments within functions

Function call semantics

Python functions and methods

clicker What does the following program do?

def f():
    g()

def g();
    print "we eat ham and jam and spamalot"

f()
  1. prints we eat ham and jam and spamalot
  2. syntax error
  3. runtime error
  4. it depends on the implementation

Answer: A

clicker What does the following program do?

ham = spam

spam = "a lot"

print ham, spam
  1. prints a lot a lot
  2. syntax error
  3. runtime error

Answer: C

Alice practical test

Tests

Peer Tutoring reminder