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 week1
[benson@hrn23501 labs]$ ls
week1
[benson@hrn23501 labs]$ cd week1
[benson@hrn23501 week1]$ ls
[benson@hrn23501 week1]$ 

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

  • 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"
  • 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

ċ
average.py
(0k)
Greg Benson,
Aug 30, 2012, 4:26 PM
ċ
funcs.py
(0k)
Greg Benson,
Aug 30, 2012, 4:26 PM
Comments