CS 127 Writing Simple Programs. Stages involved Analyze the problem Understand as much as...

24
CS 127 Writing Simple Programs

Transcript of CS 127 Writing Simple Programs. Stages involved Analyze the problem Understand as much as...

Page 1: CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.

CS 127Writing Simple Programs

Page 2: CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.

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

Page 3: CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.

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

Page 4: CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.

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

Page 5: CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.

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

Page 6: CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.

Python Reserved Keywords

Page 7: CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.

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

Page 8: CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.

Output statements

print(<expr>, <expr>, <expr>) print()

print(<expr>, <expr>, <expr>, end=“\n”)

Page 9: CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.

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

Page 10: CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.

Assigning Input

<variable> = input(<prompt>)<prompt> is the string expression

that is displayed to the user

>>>name = input(“What is your name”)

>>>name

Page 11: CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.

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

Page 12: CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.

Evaluations

>>> ans = eval(input(“Enter an expression ”))

Enter an expression 3+4*5 >>> print(ans) 23 >>>

Page 13: CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.

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

Page 14: CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.

Simultaneous Assignment

<var>, <var>, <var> = <expr>, <expr>, <expr>>>> x, y = 10, 15>>> sum, diff = x +y, y-x>>> sum

>>> diff

Page 15: CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.

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()

Page 16: CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.

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>

Page 17: CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.

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

Page 18: CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.

Range

>>> list(range(10))

[0, 1, 3 …, 9]

General syntax for <variable> in range(<expr>)

Page 19: CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.

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()

Page 20: CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.

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

Page 21: CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.

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

Page 22: CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.

Exercise

Write a program that converts distances measured in kilometers to distances measured in miles

(1 km is 0.62 miles)

Page 23: CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.

Exercise

Write an interactive calculator in Python. The user should be able to enter an expression and the answer should be displayed.

Page 24: CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.

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