Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.

Similar presentations


Presentation on theme: "CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications."— Presentation transcript:

1 CS 127 Writing Simple Programs

2  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

3 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

4 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

5 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

6 Python Reserved Keywords

7 Expressions  Program code that produces or calculates new data values  Evaluation  The process of turning an expression into an underlying data type  e.g. answer = input(“What is your age”) age = eval(answer) # answer here is a string which is converted into a number on the second line Examples : x =5 print x print y #name error

8 Output statements  print(,, )  print()  print(,,, end=“\n”)

9 Assignment Statements  =  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

10 Assigning Input  = input( )  is the string expression that is displayed to the user >>>name = input(“What is your name”) >>>name

11 Handling number inputs = eval(input( )) 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

12 Evaluations  >>> ans = eval(input(“Enter an expression ”))  Enter an expression 3+4*5  >>> print(ans)  23  >>>

13 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

14 Simultaneous Assignment ,, =,, >>> x, y = 10, 15 >>> sum, diff = x +y, y-x >>> sum >>> diff

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

16 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 in :

17 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

18 Range  >>> list(range(10))  [0, 1, 3 …, 9]  General syntax  for in range( )

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

20 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

21 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

22 Exercise  Write a program that converts distances measured in kilometers to distances measured in miles  (1 km is 0.62 miles)

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

24 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


Download ppt "CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications."

Similar presentations


Ads by Google