Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python: Experiencing IDLE, writing simple programs

Similar presentations


Presentation on theme: "Python: Experiencing IDLE, writing simple programs"— Presentation transcript:

1 Python: Experiencing IDLE, writing simple programs
Dennis Y. W. Liu (Adapted from John Zelle’s slides)

2 Objectives To be able to use Integrated Development and Learning Environment (IDLE) to program in Python To be able to understand and write Python statements to output information to the screen assign values to variables get numeric information entered from the keyboard perform a counted loop

3 Python Create by Guido van Rossum, National Research Institute for Mathematics and Computer Science in the Netherlands in the late 1980s A scripting language based on ABC Supports structured programming and object-oriented programming Easy to extend The latest version is 3.6.2

4 Integrated Development and Learning Environment (IDLE)

5 "Hello World!" Type the followings: What do you see?
print("Hello World!") print("Hello World!\n") print("Hello\nWorld!\n") What do you see?

6 Exercise 1 Display your name and student ID on the screen. Skip one line before printing the student ID. Python Programming, 1/e

7 Input, Process, Output (IPO)
Type the followings: x = eval(input("Input an integer: ")) 123 y = eval(input("Input another integer: ")) 1001 sum = x + y print(sum) What do you see?

8 Example Program: Temperature Converter
Problem: The temperature is given in Celsius, user wants it expressed in degrees Fahrenheit. Specification Input – temperature in Celsius Output – temperature in Fahrenheit Output = 9/5 x (input) + 32

9 Example Program: Temperature Converter
Design Input, Process, Output (IPO) Prompt the user for input (Celsius temperature) Process it to convert it to Fahrenheit using F = 9/5 x (C) + 32 Output the result by displaying it on the screen

10 Example Program: Temperature Converter
Before we start coding, let’s write a rough draft of the program in pseudocode. Pseudocode is precise English that describes what a program does, step by step. Using pseudocode, we can concentrate on the algorithm rather than the programming language.

11 Example Program: Temperature Converter
Pseudocode: I - Input the temperature in degrees Celsius (call it celsius) P - Calculate fahrenheit as (9/5) x celsius + 32 O - Output fahrenheit Now we need to convert this to Python! Besides entering each statement in IDLE, we can create a source file with an extension .py that contains the Python code.

12 Example Program: Temperature Converter
# convert.py # A program to convert Celsius temps to Fahrenheit # by: Susan Computewell def main(): celsius = eval(input("What is the Celsius temperature? ")) fahrenheit = 9/5 * celsius + 32 print("The temperature is", fahrenheit, "degrees Fahrenheit.") main() Python Programming, 1/e

13 Example Program: Temperature Converter
Once we write a program, we should test it! >>> What is the Celsius temperature? 0 The temperature is degrees Fahrenheit. >>> main() What is the Celsius temperature? 100 The temperature is degrees Fahrenheit. What is the Celsius temperature? -40 The temperature is degrees Fahrenheit.

14 Exercise 2 Modify convert.py so that it will take the degree in Fahrenheit and change it back to Celsius. A sample output: >>> What is the Celsius temperature? 100 The temperature is degrees Fahrenheit. The temperature is degrees Celsius. Python Programming, 1/e

15 Naming the identifiers
Names are given to variables (celsius, fahrenheit), modules (convert), functions (main), etc. These names are called identifiers. Every identifier must begin with a letter or underscore (“_”), followed by any sequence of letters, digits, or underscores. Note that “-”, “.” and many other symbols are not allowed. Identifiers are case sensitive. Identifiers cannot be Python’s keywords.

16 Python’s keywords

17 Exercise 3 Say, if you suspect that a word might be a Python keyword, how would you find out without looking up the table of the keywords? Python Programming, 1/e

18 Expressions The fragments of code that produce or calculate new data values are called expressions. A (numeric/string) literal, which is the simplest kind of expression, is used to represent a specific value, e.g. 10 or "Dennis". A simple identifier can also be an expression. Simpler expressions can be combined using operators +, -, *, /, and **. The normal mathematical precedence applies. Only round parentheses can be used to change the precedence, e.g., ((x1 – x2) / 2*n) + (spam / k**3). Try "I" + "love" + "you". Python Programming, 1/e

19 Exercise 4 If you assign 5 to x and then type x, the shell will return 5 to you. What if you type y but without assigning any value to it before?

20 Output statements Recall from your previous print exercises: Formally,
Each print statement will print their content on a different line. The items are separated by a single space Formally, print(*objects, sep=' ', end='\n', file=sys.stdout) To make it simple, print(<expr>, <expr>, …, <expr>, sep=' ', end='\n', file=sys.stdout) sep: how are the items separated? end: how is the printed content ended? file: where is the content printed?

21 Exercise 5 By setting sep appropriately, print out 1*2*3*4.
Use a print statement to print 1 and 2, and a second print statement to print 3 and 4. By setting end appropriately, 1, 2, 3, and 4 are printed on the same line.

22 Assignment statements
Simple assignment: <variable> = <expr> <variable> is an identifier, <expr> is an expression The expression on the RHS is evaluated to produce a value which is then associated with the variable named on the LHS.

23 Behind the scene Generally speaking, each variable occupies a memory location in computer systems. Python does not recycle these memory locations. Assigning a variable is more like putting a “sticky note” on a value and saying, “this is x”. The memory for the old value will be “released” automatically (i.e., garbage collection).

24 Assigning Input The purpose of an input statement is to get input from the user and store it into a variable. <variable> = input(<prompt>) E.g., x = eval(input("Enter a temperature in Celsius: ")) Print the prompt. Wait for the user to enter a value and press <enter>. The expression that was entered is evaluated and assigned to the input variable. Two kinds of input: character string or number

25 Exercise 6 Prompt user for a number and then print it out using just input(<prompt>). Prompt user for a number and then print it out using eval(input(<prompt>)). What is the difference between the two? Remove eval() from convert.py and does it still run?

26 Exercise 7 Ask users to input two numbers and print out the two numbers in a reversed order.

27 Simultaneous Assignment
Several values can be calculated at the same time. <var>, <var>, … = <expr>, <expr>, … Evaluate the expressions in the RHS and assign them to the variables on the LHS. E.g., sum, diff = x + y, x - y E.g., x, y = eval(input("Input the first and second numbers separated by a comma: "))

28 Exercise 8 Simplify your codes in exercise 7 using simultaneous assignment statements.

29 Definite Loops A definite loop executes a definite number of times, i.e., at the time Python starts the loop it knows exactly how many iterations to do. for <var> in <sequence>: <body> The beginning and end of the body are indicated by indentation. The variable after for is called the loop index. It takes on each successive value in sequence.

30 Definite Loops E.g., i counts from 0 to 9 for i in range(10):
x = 3.9 * x * (1 - x) print(x) i counts from 0 to 9 10 is the stop value, which is not used for calculation in above example

31 Exercise 9 Replace range(10) by [0,1,2,3,4,5,6,7,8,9]. Any difference?
Replace [0,1,2,3,4,5,6,7,8,9] with [0,1,3,5,7,9]. Any difference?

32 Flow chart for the definite loop

33 end


Download ppt "Python: Experiencing IDLE, writing simple programs"

Similar presentations


Ads by Google