Title: And now for something completely different . . .
1And now for something completely different . .
.
2- Part 2
- Introduction to Programming
- using Python
3Introduction toProgramming using Python
- 2.1 A simple programming problem
- 2.2 Simple math statements
- 2.3 Simple output statements
- 2.4 Data types
4Introduction toProgramming using Python
- 2.1 A simple programming problem
- 2.2 Simple math statements
- 2.3 Simple output statements
- 2.4 Data types
5The ProblemFind the average of three numbers
2.1 A simple programming problem
6Example AlgorithmFind the average of three
numbers
- 1. Tell the user to enter the first number
- 2. Add to a running total
- 3. Tell the user to enter the second number
- 4. Add to a running total
- 5. Tell the user to enter the third number
- 6. Add to a running total
- 7. Divide the total by 3
- 8. Print out the result
7PseudocodeFind the average of three numbers
set total to 0 until there are no more numbers to
process, do this write "Enter a number" read
number set total to total number set average
to total / 3 write average
8Python Program (02-01.py)Find the average of
three numbers
total 0.0 number1float(raw_input("Enter the
first number ")) total total
number1 number2float(raw_input("Enter the second
number ")) total total number2 number3float(
raw_input("Enter the third number ")) total
total number3 average total / 3 print "The
average is " str(average)
9Using Python
- How to Create and Run Python Programs using IDLE
10Python Program (02-02.py)Find the average of
three numbers
number1float(raw_input("Enter the first number
")) number2float(raw_input("Enter the second
number ")) number3float(raw_input("Enter the
third number ")) total number1 number2
number3 average total / 3 print "The average is
" str(average)
11Python Program (02-03.py)Find the average of
three numbers
total 0.0 count 0 while count lt 3
numberfloat(raw_input("Enter a number "))
count count 1 total total
number average total / 3 print "The average is
" str(average)
12Introduction toProgramming using Python
- 2.1 A simple programming problem
- 2.2 Simple math statements
- 2.3 Simple output statements
- 2.4 Data types
13result 1 1result 2 / 3result 3
2students 10books students 4x 52 y
(59)(15-7)
2.2 Simple math statements
14print result 1 1print result 2 / 3print
"The average is " str(average)
2.3 Simple output statements
15int (integer, e.g. 12, 14, 101)string (text,
e.g. "Anne", 'Anne', "Hello World!")
float (floating point number, e.g. 3.142, 98.6)
2.4 Data types
16A variable is a name that refers to a value.
The assignment statement creates new variables
and gives them values message "Hello
World!"print message
Variables in Python
17messageresult student1student2student_2max_
temperaturelast_namefinal_exam_score
Legal Variables names in Python
18and continue else for import not raise
assert def except from in or
return break del exec global is
pass try class elif finally if lambda
print while Note keywords cannot be used as
variable names
Python's 28 keywords
19This Presentation uses the following program
files
- http//www.annedawson.com/PythonPrograms.txt
- 02-01.py
- 02-02.py
- 02-03.py
20End of Python_Intro.ppt
21- Last updated Saturday 11th March 2006, 0905 PT,
AHD