MIME-Version: 1.0 Content-Location: file:///C:/EC2C4CD1/1.htm Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset="us-ascii" Course Overview and Intro to Python

Course Overview and Intro to Python

Indiana U= niversity

Com= puter Science A202 / A598

and Informatics I211

 

Why are you here?

<= ![if !supportLists]>n   How many o= f you are Informatics students?

<= ![if !supportLists]>n   Why are you studying Informatics?

<= ![if !supportLists]>n   Why is this course required?

<= ![if !supportLists]>q   Information Infrastructure: it’s almost all programmed.

 

Why learn programming?

<= ![if !supportLists]>n   Better jud= ge how computer systems will behave

<= ![if !supportLists]>q   Have a feel for what can and can’t be easily programmed

<= ![if !supportLists]>n   You’= ll probably be involved in the design of systems, and again it helps to know w= hat sort of things programmers can do easily, and not

<= ![if !supportLists]>n   Automate r= outine worik by writing small programs

<= ![if !supportLists]>q   Embedded programming environme= nts in spreadsheets databases, editors, etc.

<= ![if !supportLists]>q   Write small independent progra= ms to automate tedious work, transform data going from one system to another, and= so on

“But I’m no good at prog= ramming !    &nbs= p;  

<= ![if !supportLists]>n   Better wit= h more experience

<= ![if !supportLists]>n   Better wit= h a language that is more suitable for your probable needs than Java=

<= ![if !supportLists]>n   More likel= y to find the language Python useful.

<= ![if !supportLists]>n   Mostly you’ll probably be using languages that don’t exist now, but mo= re like Python than Java

 

 

 

 “But= I want to do applet and GUI programming.”

<= ![if !supportLists]>n   The GUI and applet programming APIs commonly used in Java today are poorly designed (ex= cept for some professional purposes)

<= ![if !supportLists]>n   Most likel= y you can do your GUI/applet programming more easily in Python than Java

<= ![if !supportLists]>n   Countless = GUI programming environments, and new ones all the time

<= ![if !supportLists]>q   You can learn them on your own= as needed

 

“But learning a new languages is to= o much trouble. I already know Java.”

<= ![if !supportLists]>n   You only l= earned a small par to Java in A202/I210

<= ![if !supportLists]>q   You’d have to learn a lo= t more in a second Java course

<= ![if !supportLists]>n   Java synta= x is difficult and aggrivating; Python’s is mu= ch simpler

<= ![if !supportLists]>n   Most diffi= culty was learning concepts that apply to Python as well

 

 

Benefits of learning a second language=

<= ![if !supportLists]>n   Broader perspective makes learning languages much easier

<= ![if !supportLists]>q   Like moving out of a small tow= n

<= ![if !supportLists]>n   Learning to learn: that’s real education

 

Java vs Python

<= ![if !supportLists]>n   More compa= rison shortly.

<= ![if !supportLists]>n   BOTTOM LIN= E:

<= ![if !supportLists]>q   both are generally well-design= ed, modern, popular, free languages

<= ![if !supportLists]>q   but Java is designed for writi= ng very large program, Python for smaller programs

<= ![if !supportLists]>n   Python is = easier to learn and more useful for non-professional programmers=

<= ![if !supportLists]>n   Take c211/= c212 if you intend to do professional programming

<= ![if !supportLists]>q   Easier to learn language in th= is course doesn't mean course will be easier, but hopefully more useful

 

 

Course overview

<= ![if !supportLists]>n   Administri= via and much other valuable information on course

<= ![if !supportLists]>n   Home page assignments and news

<= ![if !supportLists]>n   Policies

<= ![if !supportLists]>q   Are you in the right course?

<= ![if !supportLists]>q   Academic dishonesty=

<= ![if !supportLists]>n   Resources:= text, IDLE, and more

<= ![if !supportLists]>n   Bring your= text to every lab

<= ![if !supportLists]>n   Read at le= ast chapter 1 before lab this week

<= ![if !supportLists]>n   Chapter 1 sections 1-5 won’t be covered in class

 

 

Simple Python expressions and functions

<= ![if !supportLists]>n   tran1.txt

<= ![if !supportLists]>q   the link above is to an interective Python session transcript

<= ![if !supportLists]>q   start Python IDLE shell with <= b>>>> prompt

<= ![if !supportLists]>q   simple data types: integer, fl= oat, and string

<= ![if !supportLists]>n     division (/) of two integers thr= ows the remainder away!

<= ![if !supportLists]>n&nb= sp;    float f= unction converts integer to float

<= ![if !supportLists]>n     single and doubly quoted string= s

<= ![if !supportLists]>n     + concatenation operator

<= ![if !supportLists]>q   standard operator precedence in expressions

Transcript points continued

 

<= ![if !supportLists]>q&nb= sp;  module import statement example: import math

<= ![if !supportLists]>q   module variable reference exam= ple: math.pi

<= ![if !supportLists]>q&nb= sp;  variable declaration and assignment operator: =3D

<= ![if !supportLists]>n     no types in declarations

<= ![if !supportLists]>q   functions

<= ![if !supportLists]>n     definition: def <func= tion name> ( <parameters> ) : <body>

<= ![if !supportLists]>n     body is indented: each line by = the same amount

<= ![if !supportLists]>n     function call: <function>= ( <arguments> )

<= ![if !supportLists]>n     end with return statement unless nothing is returned

<= ![if !supportLists]>n&nb= sp;    syntax: return <return value expresssion><= b>

<= ![if !supportLists]>q   print statement

 

Python names

<= ![if !supportLists]>n   Names (of variables, functions, modules, etc.)

<= ![if !supportLists]>q   may contain upper or lower case letters, underscore “_”, or digits (0-9), and cannot start with= a digit

<= ![if !supportLists]>q   are case sensitive<= /span>

<= ![if !supportLists]>q   may not be keywords=

<= ![if !supportLists]>n&nb= sp;    the Python keywords are: and, assert, break, class, continue, def, del, elif, else, except, exec, finally, for, from, global, if, import, in, is, lambda, not, or, pass, print, raise, return, try, while, yield

<= ![if !supportLists]>n     the IDLE colors keywords orange=

<= ![if !supportLists]>n   Compound n= ame conventions

<= ![if !supportLists]>q&nb= sp;  underscore-separated, as in get_value=

<= ![if !supportLists]>q   Java-style camel case, as in <= span class=3DSpellE>getValue

<= ![if !supportLists]>q&nb= sp;  run-together lower case, as in getvalue=

<= ![if !supportLists]>q   Java-style all-caps for consta= nts, as in F_OK

 

 

First handin

<= ![if !supportLists]>n    As always for hand-ins, at top= of full blank page write

<= ![if !supportLists]>q   your username, in clear block l= etters

<= ![if !supportLists]>q   your signature

<= ![if !supportLists]>n    What is the value of the expre= ssion at the end of the transcript below?

>>> x =3D 9

>>> 1 + x / 2 / 2

???

<= ![if !supportLists]>n    Answer: 3

<= ![if !supportLists]>q   please don’t change your handin: your answers aren’t graded

<= ![if !supportLists]>n    What is the main reason you= 217;re taking this course?

<= ![if !supportLists]>n    Anything specific you’d = like to learn or do in the course?

<= ![if !supportLists]>n    Unless asked to do otherwise, = always hand in handins on the front table as you leave.

 

A first Python program

<= ![if !supportLists]>n    circleArea4.py

<= ![if !supportLists]>q&nb= sp;  Python programs (modules) are files generally ending in .p= y

<= ![if !supportLists]>q   modules typically contain impo= rt statements, function and variable declarations

<= ![if !supportLists]>q   applications end with a mai= n function call (with no arguments)

<= ![if !supportLists]>q&nb= sp;  important: modules should not perform I/O or launch a GUI when loaded (executed) or imported, except possibly after an application has invoked main() in its last = line

<= ![if !supportLists]>q   execute module in IDLE shell w= ith F5 key

<= ![if !supportLists]>q   value returned area function is thr= own away !

<= ![if !supportLists]>q   comments start with a hash mar= k: #

<= ![if !supportLists]>n   printCircleArea4.py

<= ![if !supportLists]>q   that’s better: it prints= the answer

 

Circle area calculation program

<= ![if !supportLists]>n   circleArea.= py

<= ![if !supportLists]>q   input function takes a prompt strin= g, waits for the user to enter a line, evaluates it, and returns the result

<= ![if !supportLists]>q   string as first element of mod= ule or function body is automatically saved and interpreted as documentation<= /o:p>

<= ![if !supportLists]>n     the function help(<module= >) or help(<function>) prints documentation

Circle area table program

<= ![if !supportLists]>n   circle= AreaTable.py

<= ![if !supportLists]>q   one possible for loop syntax:<= o:p>

    =     for <variable> in <list expression>

<= ![if !supportLists]>q   range(1, 10) returns the list [1, 2, ̷= 0;, 9]

<= ![if !supportLists]>n     more about lists soon

<= ![if !supportLists]>n     more information on range in th= e text and documentation

<= ![if !supportLists]>q   press F1 key in IDLE wi= ndow for documentation

<= ![if !supportLists]>n     documentation also available fr= om course web contents panel link

<= ![if !supportLists]>q&nb= sp;  print command commas introduce a space, or prevent newline if at end of line

Handin exercise

<= ![if !supportLists]>n    Write an application main meth= od that prints a tip table; for example

>>> main()

Enter first amount: 3

Enter last amount: 10

amount tip

3 0.45

4 0.6

5 0.75

6 0.9

7 1.05

8 1.2

9 1.35

10 1.5

>>>

Handin exercise continued

<= ![if !supportLists]>n        Solution

def main():=

  &nbs= p; first =3D input("Enter first amount:= ")

  &nbs= p; last =3D input("Enter last amount: &= quot;)

  &nbs= p; print "amount", "tip"=

  &nbs= p; for amount in range(first, last + 1):

        print amount, 0.15 * amount

<= ![if !supportLists]>n        Add call to main, preferably in if statement, and you have a complete applicatio= n

Differences between Python and Java

<= ![if !supportLists]>n   Bottom = line: Python is for writing small programs

<= ![if !supportLists]>q   Java is primarily for “programming in the large”)

<= ![if !supportLists]>q   programming professionals gene= rally work on large programs, others on much smaller programs

<= ![if !supportLists]>n   Python is dynamically typed

<= ![if !supportLists]>q   values, not variables, have ty= pes

<= ![if !supportLists]>q   no type checker rules to learn= and obey

<= ![if !supportLists]>q   type errors not caught until r= un time, and then only if thoroughly tested

<= ![if !supportLists]>q&nb= sp;  Java is statically typed (must declare variable types and follow type che= cker rules)

 

Differences continued

<= ![if !supportLists]>n   Python has= clean syntax

<= ![if !supportLists]>q   no braces for grouping like Ja= va

<= ![if !supportLists]>q   but= indentation is critical !

<= ![if !supportLists]>n   Python has excellent syntactic support for compound data structures<= /p>

<= ![if !supportLists]>q   haven’t seen this yet, b= ut very nice

<= ![if !supportLists]>q   very little such support in Ja= va

<= ![if !supportLists]>n   Java makes simples things simple

<= ![if !supportLists]>q   better for small programs and = for learning

<= ![if !supportLists]>q   less emphasis on systematic structure than Java

 

Similarities between Python and Java

 

<= ![if !supportLists]>n    Both are strongly typed (unlike C and C++)

<= ![if !supportLists]>q   type errors don’t cause programs to crash or give meaningless results without warning

<= ![if !supportLists]>n    Automatic storage management (= unlike C, C++)

<= ![if !supportLists]>q   a huge advantage

<= ![if !supportLists]>n    Object-oriented (but Python doesn’t have to be used that way)

<= ![if !supportLists]>n    Popular, operate in most environments, portable, fairly new

<= ![if !supportLists]>n    Usually compiled to byte code<= o:p>

<= ![if !supportLists]>q   Jython implementation uses Java byte = codes

<= ![if !supportLists]>q   can be used for applets and any= thing else you can do with Java

<= ![if !supportLists]>n    Open, free implementations, wi= dely used

 

 

 

circleArea.py

'Prompt for diameter of circle and print area'

import math

def area(diameter):
    'Return area of circle of given diameter'
    return math.pi * (float(diameter) / 2) ** 2

def main():
    d = input('Enter diameter: ')
    print area(d)

main()

circleArea4.py

import math

def area(diameter):
    return math.pi * (float(diameter) / 2) ** 2

d = 4

def main():
    area(d) # value is thrown away

main()

circleAreaTable.py

'Print table of circle areas for diameters 1, 2, ..., 9'

import math, circleArea

def main():
    for d in range(1, 10):
        print d, circleArea.area(d)

main()

printCircleArea4.py

import math

def printArea(diameter):
    return math.pi * (float(diameter) / 2) ** 2

d = 4

def main():
    print printArea(d)

main()

tran1.txt

Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

    ****************************************************************
    Personal firewall software may warn about the connection IDLE
    makes to its subprocess using this computer's internal loopback
    interface.  This connection is not visible on any external
    interface and no data is sent to or received from the Internet.
    ****************************************************************

IDLE 1.0.3
>>> 3
3
>>> 3.14159
3.1415899999999999
>>> 'this is a string
SyntaxError: EOL while scanning single-quoted string
>>> 'this is a string'
'this is a string'
>>> 'this is a string with "double quotes"'
'this is a string with "double quotes"'
>>> pi = 3.14159
>>> pi * 4
12.56636
>>> d = 4
>>> pi * d
12.56636
>>> def circumference(diameter):
	return pi * d

>>> circumference(4)
12.56636
>>> import math
>>> math.pi
3.1415926535897931
>>> def circumference(diameter):
	return math.pi * d

>>> def area(diameter):
	return math.pi * (diameter / 2) ** 2

>>> area(2)
3.1415926535897931
>>> area(1)
0.0
>>> def area(diameter):
	return math.pi * (float(diameter) / 2.0) ** 2

>>> area(1)
0.78539816339744828
>>> def area(diameter):
	print math.pi (diameter / 2.0) ** 2


>>> area(1)

Traceback (most recent call last):
  File "", line 1, in -toplevel-
    area(1)
  File "", line 2, in area
    print math.pi (diameter / 2.0) ** 2
TypeError: 'float' object is not callable
>>> def area(diameter):
	print math.pi * (diameter / 2.0) ** 2


>>> area(1)
0.785398163397
>>> area(1) * 2
0.785398163397

Traceback (most recent call last):
  File "", line 1, in -toplevel-
    area(1) * 2
TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'
>>> 'a ' + 'string'
'a string'
>>> print 'a ' + 'string'
a string
>>> '1' + 2

Traceback (most recent call last):
  File "", line 1, in -toplevel-
    '1' + 2
TypeError: cannot concatenate 'str' and 'int' objects
>>> '1' + str(2)
'12'
>>>

circleArea.py

'Prompt for diameter of circle and print area'

import math

def area(diameter):
    'Return area of circle of given diameter'
    return math.pi * (float(diameter) / 2) ** 2

def main():
    d = input('Enter diameter: ')
    print area(d)

main()

circleArea4.py

import math

def area(diameter):
    return math.pi * (float(diameter) / 2) ** 2

d = 4

def main():
    area(d) # value is thrown away

main()

circleAreaTable.py

'Print table of circle areas for diameters 1, 2, ..., 9'

import math

def area(diameter):
    'Return area of circle of given diameter'
    return math.pi * (float(diameter) / 2) ** 2

def main():
    print 'diameter', 'area' # table column header
    for d in range(1, 10):
        print d, area(d)

main()

printCircleArea4.py

import math

def area(diameter):
    return math.pi * (float(diameter) / 2) ** 2

d = 4

def main():
    print area(d)

main()

t1.txt

Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

    ****************************************************************
    Personal firewall software may warn about the connection IDLE
    makes to its subprocess using this computer's internal loopback
    interface.  This connection is not visible on any external
    interface and no data is sent to or received from the Internet.
    ****************************************************************

IDLE 1.0.3
>>> 3
3
>>> 3.14159
3.1415899999999999
>>> 'this is a string
SyntaxError: EOL while scanning single-quoted string
>>> 'this is a string'
'this is a string'
>>> 'this is a string with "double quotes"'
'this is a string with "double quotes"'
>>> pi = 3.14159
>>> pi * 4
12.56636
>>> d = 4
>>> pi * d
12.56636
>>> def circumference(diameter):
	return pi * d

>>> circumference(4)
12.56636
>>> import math
>>> math.pi
3.1415926535897931
>>> def circumference(diameter):
	return math.pi * d

>>> def area(diameter):
	return math.pi * (diameter / 2) ** 2

>>> area(2)
3.1415926535897931
>>> area(1)
0.0
>>> def area(diameter):
	return math.pi * (float(diameter) / 2.0) ** 2

>>> area(1)
0.78539816339744828

>>> def area(diameter):
	print math.pi * (diameter / 2.0) ** 2


>>> area(1)
0.785398163397
>>> area(1) * 2
0.785398163397

Traceback (most recent call last):
  File "", line 1, in -toplevel-
    area(1) * 2
TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'
>>> 'a ' + 'string'
'a string'
>>> print 'a ' + 'string'
a string
>>> '1' + 2

Traceback (most recent call last):
  File "", line 1, in -toplevel-
    '1' + 2
TypeError: cannot concatenate 'str' and 'int' objects
>>> '1' + str(2)
'12'
>>>