And now for something completely different . . . - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

And now for something completely different . . .

Description:

Find the average of three numbers. 1. Tell the user to enter the ... class elif finally if lambda print while. Note: keywords cannot be used as variable names ... – PowerPoint PPT presentation

Number of Views:14
Avg rating:3.0/5.0
Slides: 22
Provided by: AnneD71
Category:

less

Transcript and Presenter's Notes

Title: And now for something completely different . . .


1
And now for something completely different . .
.
2
  • Part 2
  • Introduction to Programming
  • using Python

3
Introduction toProgramming using Python
  • 2.1 A simple programming problem
  • 2.2 Simple math statements
  • 2.3 Simple output statements
  • 2.4 Data types

4
Introduction toProgramming using Python
  • 2.1 A simple programming problem
  • 2.2 Simple math statements
  • 2.3 Simple output statements
  • 2.4 Data types

5
The ProblemFind the average of three numbers
2.1 A simple programming problem
6
Example 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

7
PseudocodeFind 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
8
Python 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)
9
Using Python
  • How to Create and Run Python Programs using IDLE

10
Python 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)
11
Python 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)
12
Introduction toProgramming using Python
  • 2.1 A simple programming problem
  • 2.2 Simple math statements
  • 2.3 Simple output statements
  • 2.4 Data types

13
result 1 1result 2 / 3result 3
2students 10books students 4x 52 y
(59)(15-7)
2.2 Simple math statements
14
print result 1 1print result 2 / 3print
"The average is " str(average)
2.3 Simple output statements
15
int (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
16
A 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
17
messageresult student1student2student_2max_
temperaturelast_namefinal_exam_score
Legal Variables names in Python
18
and 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
19
This Presentation uses the following program
files
  • http//www.annedawson.com/PythonPrograms.txt
  • 02-01.py
  • 02-02.py
  • 02-03.py

20
End of Python_Intro.ppt
21
  • Last updated Saturday 11th March 2006, 0905 PT,
    AHD
Write a Comment
User Comments (0)
About PowerShow.com