Python: Experiencing IDLE, writing simple programs

Slides:



Advertisements
Similar presentations
Adapted from John Zelle’s Book Slides
Advertisements

Python Programming: An Introduction to Computer Science
CSC 110 Writing simple programs [Reading: chapter 2] CSC 110 C 1.
Vahé Karamian Python Programming CS-110 CHAPTER 2 Writing Simple Programs.
Introduction to Python
Chapter 2 Writing Simple Programs
Basic Elements of C++ Chapter 2.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
CSC1015F – Python Chapter2 Michelle Kuttel
CS190/295 Programming in Python for Life Sciences: Lecture 2 Instructor: Xiaohui Xie University of California, Irvine.
Introduction to Python
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.
Computer Science 101 Introduction to Programming.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Input, Output, and Processing
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University.
Variables and Assignment CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
Chapter 2 Writing Simple Programs
Chapter Topics The Basics of a C++ Program Data Types
Whatcha doin'? Aims: To start using Python. To understand loops.
A Python Tour: Just a Brief Introduction
Topics Designing a Program Input, Processing, and Output
Introduction to Programming
Chapter 2 Introduction to C++ Programming
Input and Output Upsorn Praphamontripong CS 1110
Topic: Python’s building blocks -> Statements
© 2016 Pearson Education, Ltd. All rights reserved.
Python Programming: An Introduction to Computer Science
Python: Control Structures
Basic Elements of C++.
Python Programming: An Introduction to Computer Science
The Selection Structure
Variables, Expressions, and IO
Basic Elements of C++ Chapter 2.
Python Primer 2: Functions and Control Flow
INPUT & OUTPUT scanf & printf.
First Python Program Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
CS111 Computer Programming
4. sequence data type Rocky K. C. Chang 16 September 2018
“If you can’t write it down in English, you can’t code it.”
CS190/295 Programming in Python for Life Sciences: Lecture 2
3. Decision Structures Rocky K. C. Chang 19 September 2018
Introduction to Programming
Rocky K. C. Chang September 18, 2018 (Based on Zelle and Dierbach)
Topics Designing a Program Input, Processing, and Output
5. Functions Rocky K. C. Chang 30 October 2018
15-110: Principles of Computing
CISC101 Reminders All assignments are now posted.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
12th Computer Science – Unit 5
Unit 3: Variables in Java
Chopin’s Nocturne.
The Last of the Mohicans (大地英豪)
General Computer Science for Engineers CISC 106 Lecture 03
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
Introduction to Programming
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

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

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

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

Integrated Development and Learning Environment (IDLE)

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

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

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?

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

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

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.

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.

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

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

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 212.0 degrees Fahrenheit. The temperature is 100.0 degrees Celsius. Python Programming, 1/e

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.

Python’s keywords

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

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

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?

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?

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.

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.

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

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

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?

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

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: "))

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

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.

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

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?

Flow chart for the definite loop

end