Lectures‎ > ‎

Week02

Preliminaries

  • Check website regularly.
  • Class Google Group subscriptions - all Dons accounts.

Review

  • Accounts and Portfolios
  • Google Group
  • Linux
  • Editing programs
    • cat > filename (quick and dirty)
    • gedit
  • Running Python
    • Interactively
    • Script mode

The Linux File System and Directories

  • When you open a terminal you are situated in your home directory
  • You home directory lives in the Linux file system
  • You "own" everything in your home directory
  • You can create files in you home directory
  • You can also create sub-directories in you home directory:
[benson@hrn23501 ~]$ mkdir cs110
[benson@hrn23501 ~]$ cd cs110
[benson@hrn23501 cs110]$ ls
[benson@hrn23501 cs110]$ 
[benson@hrn23501 cs110]$ mkdir labs
[benson@hrn23501 cs110]$ ls
labs
[benson@hrn23501 cs110]$ cd labs
[benson@hrn23501 labs]$ ls
[benson@hrn23501 labs]$ mkdir week01
[benson@hrn23501 labs]$ ls
week01
[benson@hrn23501 labs]$ cd week01
[benson@hrn23501 week01]$ ls
[benson@hrn23501 week01]$ 

  • Now we can create a file in our new cs110/labs/week01 directory:
[benson@hrn23501 week01]$ cat > hello.py
print 'Hello World'
[benson@hrn23501 week01]$ python hello.py 
Hello World
[benson@hrn23501 week01]$ 

  • Linux commands:
    • ls : list the contents of the "current" directory
    • mkdir <name> : create a new directory called "name"
    • cd <name>: change directory
    • cd .. : move up one directory level
    • cd <name1>/<name2> : move into directory name1/name2
    • rm <name> : remove file called "name"
    • rmdir <name> : remove directory called "name"
    • mv <name> <destination> : move or rename a file
  • Tricks: tab completion and command-line editing.
  • Can also use GUI (but try not too :-)

Values and types in Python

  • Things like 3, "hello", and 7.4 are all values in Python
  • A value has a type:
>>> type(3)
<type 'int'>

>>> type('hello')
<type 'str'>

>>> type(7.4)
<type 'float'>
>>> 

  • It will be important to use the right type for the right computation

Variables

  • variable is used to hold a value in a computer program:
>>> x = 3
>>> print x
3
>>> type(x)
<type 'int'>

>>> myname = 'Greg'
>>> print myname
Greg
>>> type(myname)
<type 'str'>

>>> score = 85.6
>>> print score
85.6
>>> type(score)
<type 'float'>
>>> 

Expressions

  • Expressions are used to derive new values from existing values and variables:

>>> 1 + 2
3
>>> x = 1
>>> y = 2
>>> x + y
3
>>> z = x + y
>>> z
3
>>> print z
3
>>> 

  • + is an operator
  • = is the assignment operator

Getting input from the User

  • Two ways to get input from the user into a variable:
    • raw_input(<prompt_string>) (for strings)
    • input(<promt_string>) (for numbers)
  • Examples:
>>> name = raw_input('Your name: ')
Your name: Greg
>>> print name
Greg
>>> age = input('Your age: ')
Your age: 21
>>> print age
21
>>> 
  • Compute average:
    • Ask for number 1, n1
    • Ask for number 2, n2
    • sum n1 and n2 and divide by two
$ cat average.py
n1 = raw_input('Enter number 1: ')
n2 = raw_input('Enter number 2: ')

avg = (float(n1) + float(n2)) / 2.0

print 'The average is: ', avg

Gedit and Spacing

  • Set spacing to "4 spaces"
  • Do not use "tabs"
  • Turn on line numbers

Printing Variables

  • Using commas with print
  • Using string concatenation
  • Give examples

Functions

  • Used to package code statements so that they can be re-used
  • First form of abstraction.
    • Specifically, functional abstraction.
  • Basic parts:
    • Header: begins with a keyword and ends with a colon ":"
    • Body: one or more Python statements, each indented the same amout
  • Function definition
  • Parameters
  • Return Value
  • Function call
  • Arguments
  • Functions can call other functions.

$ cat average2.py

def average(n1, n2):
    return (n1 + n2) / 2.0

n1 = input('Enter number 1: ')
n2 = input('Enter number 2: ')

avg = average(n1, n2)

print 'The average is: ' + str(avg)

Comments

  • You often need to describe what a variable is used for or what a function does in plain English
  • Almost all programming languages allow comments.
  • In Python, you start a comment with the "#" symbol:

    # This is a comment

  • We will start to use more comments in our code.
  • Comments are useful for you and for other people looking at your code.

Function Examples

# Exmple of a simple function with no parameters and no return value
def newline():
    print
    
# Example of a function with parameters
def cat_n_times(s, n):
    print s * n

# Example of a function with parameters and a return value
def avg(n1, n2):
    avg = (n1 + n2) / 2.0
    return avg
    

# Print 3 newlines:
newline()
newline()
newline()

# Print _USF 4 times
cat_n_times('_USF', 4)

# Compute averages
a1 = avg(20, 21)
print 'a1 = ', a1

a2 = avg(100, 200)
print 'a2 = ' + str(a2)

Flow of Execution

  • Execution of Python programs start on the first line and continue one by one
  • However, when a function is called, the flow jumps to the function
    • The arguments are assigned to the parameters
    • The body is executed (statement by statement)
    • Then the function returns to the caller
  • Functions can call functions.
  • This is little bit like web-browsing and using the "back" button.

Big Picture: Understand Program Execution

  • Your goal is to be able to "predict" how a program is going to behave.
  • You are development a "mental model" for program execution

Comments