View
213
Download
0
Category
Preview:
Citation preview
CS 127Writing Simple Programs
Writing Simple Programs
Stages involved Analyze the problem
Understand as much as possible what is trying to be solved
Determine Specifications Describe what your program will do Define Inputs and Outputs
Create your design Design algorithms How will your program work
Writing Simple Programs
Implement the Design Translate algorithm into computer language
Test / Debug your work Try out the program Does it work as expected Bugs (errors) should be fixed Try to “break” the program
Maintain the program Continue making changes in response to
needs
Ways to run a python program
1. From the command line python3 myProgram.py
2. From IDLE window (IDE)3. Drag and drop programs onto the
Python Launcher
Python variable names
Must start with letter or the underscore (“_” character) which may be followed by any sequence of letters, digits or underscores
x x12 _name __names first_last_name
Python Reserved Keywords
Expressions
Program code that produces or calculates new data values
EvaluationThe process of turning an expression into an underlying data typee.g.answer = input(“What is your age”)age = eval(answer)# answer here is a string which is converted into a number on the second lineExamples : x =5print xprint y #name error
Output statements
print(<expr>, <expr>, <expr>) print()
print(<expr>, <expr>, <expr>, end=“\n”)
Assignment Statements
<variable> = <expr> x = 3.9 * x You assign the value of an expression to a
variable >>> myVar = 0 >>>myVar 0 >>>myVar = 7 >>> myVar 7 >>>myVar = myVar +1 >>myVar 8
Assigning Input
<variable> = input(<prompt>)<prompt> is the string expression
that is displayed to the user
>>>name = input(“What is your name”)
>>>name
Handling number inputs
<variable> = eval(input(<prompt>))
eval() takes its argument and evaluates the numeric representation of the string and stores it in the variable
>>>x = eval(input(“What is the temperature in Farenheit?”))>>>x
Evaluations
>>> ans = eval(input(“Enter an expression ”))
Enter an expression 3+4*5 >>> print(ans) 23 >>>
Exercise : Temperature Converter
Problem : We need to convert temperature from
degrees Celsius to Degrees Farenheit
Write an algorithm to solve this problem
What are the inputs How are you going to compute the result What are the outputs
Conversion : farenheit = 9/5*celsius + 32
Simultaneous Assignment
<var>, <var>, <var> = <expr>, <expr>, <expr>>>> x, y = 10, 15>>> sum, diff = x +y, y-x>>> sum
>>> diff
Average Example
#average.py -- using simulataneous assignment
def main() :
print ("This program computes the average of two numbers")
num1, num2 = eval(input("Enter two numbers separated by commas"))
average = (num1+num2)/2
print ("The average of the two numbers is: ",average)
main()
Definite loops
Looping is an important part of programming
A statement has to be executed a specified number of times
For definite loops, Python knows how many times we will go around (iterate) over a loop
Syntax:for <var> in <sequence> :<body>
Definite Loop Example
>>> for i in [0,1,2,3]print (i)
>>> for odd in [1, 3, 5, 7, 9]print (odd*odd)
>>>for i in range(10):print(i)
range() is a built-in function in Python for generating a sequence of numbers on the fly
Range
>>> list(range(10))
[0, 1, 3 …, 9]
General syntax for <variable> in range(<expr>)
Future Value Example
#future.py def main(): print("This program calculates the future
value") print("of a 10-year investment") principal = eval(input("Enter the initial
principal: ")) apr = eval(input("Enter the annual interest
rate: ")) for i in range(10): principal = principal *(1+apr) print("The value in 10 years is: ",principal) main()
Exercise
Modify the previous example to allow the user to also specify the number of years so that calculation is based on the number of years the user entered
Exercise
Modify the temperature converter program so that it prints a table of Celcius temperatures and the Farenheit equivalents every 10 degress from 0C to 100 C
Exercise
Write a program that converts distances measured in kilometers to distances measured in miles
(1 km is 0.62 miles)
Exercise
Write an interactive calculator in Python. The user should be able to enter an expression and the answer should be displayed.
Exercise
Write a python program which asks a user to specify two numbers. Compute and display the average of the numbers in the range
Eg: If user enters 2 and 4
Average = (2+3+4)/3 = 3
Recommended