Chapter 2 Writing Simple Programs

Slides:



Advertisements
Similar presentations
Python Basics: Statements Expressions Loops Strings Functions.
Advertisements

Intro to Python. Python is an interpreted language Can be used interactively Identifiers are case-sensitive Operators: + - * / ** Arbitrarily large integer.
Adapted from John Zelle’s Book Slides
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.
Intro to Robots Robots are Everywhere. Intro to Robots Various robots in use today lawnmower pet baby seal for convalescents gutter cleaner home security.
Introduction to Python
Chapter 2 Writing Simple Programs
Variables, Expressions, and Statements
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.
Python Control of Flow.
Python.
INLS 560 – V ARIABLES, E XPRESSIONS, AND S TATEMENTS Instructor: Jason Carter.
Introduction to Python
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.
CS1022 Computer Programming & Principles Lecture 1.2 A brief introduction to Python.
CPTR 124 Review for Test 1. Development Tools Editor Similar to a word processor Allows programmer to compose/save/edit source code Compiler/interpreter.
Computer Science 101 Introduction to Programming.
Variables, Expressions, and Statements
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
COSC 235: Programming and Problem Solving Ch. 2: Your first programs!!! Instructor: Dr. X.
Software Development. Software Development Loop Design  Programmers need a solid foundation before they start coding anything  Understand the task.
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.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
Xiaojuan Cai Computational Thinking 1 Lecture 2 Simple Programs Xiaojuan Cai (蔡小娟) Fall, 2015.
Python Let’s get started!.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
I NTRODUCTION TO PYTHON - GETTING STARTED ( CONT )
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
Loop Structures and Booleans Zelle - Chapter 8 Charles Severance - Textbook: Python Programming: An Introduction to Computer Science,
Software Development.
Topics Designing a Program Input, Processing, and Output
CS1022 Computer Programming & Principles
Python: Experiencing IDLE, writing simple programs
Python Programming Module 3 Functions Python Programming, 2/e.
Input and Output Upsorn Praphamontripong CS 1110
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Python Loops and Iteration
Python Let’s get started!.
Introduction to Python
Python Programming: An Introduction to Computer Science
Python: Control Structures
Python Programming: An Introduction to Computer Science
Presented By S.Yamuna AP/IT
Variables, Expressions, and Statements
Python Primer 2: Functions and Control Flow
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Loop Structures and Booleans Zelle - Chapter 8
4. sequence data type Rocky K. C. Chang 16 September 2018
CS190/295 Programming in Python for Life Sciences: Lecture 2
3. Decision Structures Rocky K. C. Chang 19 September 2018
Variables, Expressions, and Statements
Rocky K. C. Chang September 18, 2018 (Based on Zelle and Dierbach)
Lecture 2 Python Programming & Data Types
Elements of a Python Program
Variables, Expressions, and Statements
CISC101 Reminders All assignments are now posted.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Introduction to Computer Science
Introduction to Computer Science
Chopin’s Nocturne.
The Last of the Mohicans (大地英豪)
Data Types and Expressions
Python Reserved Words Poster
Presentation transcript:

Chapter 2 Writing Simple Programs Charles Severance Textbook: Python Programming: An Introduction to Computer Science, John Zelle

Software Development Process Figure out the problem - for simple problems - think about how you would do the problem by hand Determine the specifications - for a first programming course - the specifications are generally in the assignment handout

Software Development Create a Design - In the beginning this is an outline of the major steps Implement the design - build your software Test and debug the program - make sure to think about different things which might go wrong Maintain the program

Input Process Output # convert.py # A program to convert Celsius temps to Fahrenheit# by: Susan Computewell def main(): celsius = input("What is the Celsius temperature? ") fahrenheit = (9.0 / 5.0) * celsius + 32 print "The temperature is", fahrenheit, "degrees Fahrenheit.“ main() Input Process Output Z-28

Running the Program... Input Process Output $ python convert.py What is the Celsius temperature? 0 The temperature is 32.0 degrees Fahrenheit. What is the Celsius temperature? 100 The temperature is 212.0 degrees Fahrenheit. Input Process Output

Variable Names / Identifiers Must start with a letter or underscore _ Must consist of letters and numbers Case Sensitive Good: spam eggs spam23 Bad: 23spam #sign var.12 Different: spam Spam SPAM Z-2.3.1

and del for is raise assert elif from lambda return break else global Reserved Words You can not use reserved words as variable names / identifiers and del for is raise assert elif from lambda return break else global not try class except if or while continue exec import pass yield def finally in print Z-2.3.1

Expressions Programming languages have lots of expressions Expressions are things that can be evaluated to a value Can be a string, number or virtually anything Can be a single value or computed from several values using operators Z-2.3.2

Expressions Everywhere celsius = input( "What is the Celsius temperature? " ) fahrenheit = (9.0 / 5.0) * celsius + 32 print "The temperature is" , fahrenheit , "degrees Fahrenheit." Z-2.3.2

Expressions with Numbers 0.6 Look up variables Do math operations in order left to right ( ) * / + - x 0.6 0.6 3.9 * x * ( 1 - x ) 0.4 0.93 Z-2.3.2

Expressions With Strings “Bob” abc For strings the + operator means “concatenate” “Bob” “Hello “ + “there “ + abc “Hello there Bob” Z-4.1

Output Statements x = 6 print 2 print 2 + 3 2 5 print “Hello”, 4+5 The print statement takes one or more expressions separated by commas and prints the expressions on the output separated by spaces x = 6 print 2 print 2 + 3 print “Hello”, 4+5 2 5 Hello 9 Z-2.4

Assignment Statements variable = expression Evaluate the expression to a value and then put that value into the variable x = 1 spam = 2 + 3 spam = x + 1 x = x + 1

Slow Motion Assignment We can use the same variable on the left and right side of an assignment statement Remember that the right side is evaluated *before* the variable is updated Z-34

Input Statements input(“Prompt”) - displays the prompt and waits for us to input an expression - this works for numbers In Chapter 4 we will see how to read strings >>> x = input("Enter ") Enter 123 >>> print x 123 Z-34

Simultaneous Assignment variable, variable = expression, expression Both expressions on right hand side are evaluated before the right hand side variables are updated >>> x = 1 >>> y = 2 >>> x , y = y , x >>> print x, y 2 1 >>> x , spam = 2 + 3, “hello” Z-36

Definite Loops

Definite Loops Loops that run a fixed (aka definite) number of times Loops that “iterate” through an ordered set Loops that run “for” a number of times Hi 1 2 3 4 for abc in range(5) : print “Hi” print abc Z-39

Definite Loops Loops that run a fixed (aka definite) number of times Loops that “iterate” through an ordered set Loops that run “for” a number of times The iteration variable change for each iteration of the loop Hi 1 2 3 4 for abc in range(5) : print “Hi” print abc Colon (:) defines the start of a block. Indenting determines which lines belong to the block. Z-39

Five-element sequence Looking at In... The iteration variable “iterates” though the sequence (ordered set) The block (body) of code is executed once for each value in the sequence The iteration variable moves through all of the values in the sequence Five-element sequence [ 0, 1, 2, 3, 4] Iteration variable for abc in range(5) : ... block of code ...

In a FlowChart The iteration variable “iterates” though the sequence (ordered set) The block (body) of code is executed once for each value in the sequence The iteration variable moves through all of the values in the sequence

Loop body is run repeatedly print i Program: for i in range(4) : print i i = 1 print i i = 2 print i i = 3 Loop body is run repeatedly print i

What is range(10) ? >>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> for i in [0, 1, 2] : ... print I ... 1 2 >>> for i in [0, "abc", 9, 2, 3.6] : ... print I abc 9 3.6 range(10) is a built in function that returns a sequence of numbers The for statement can iterate through any sequence A sequence can have values of different types Z-40

Summary Assignment Statements Input Statements Software Development Simultaneous Assignments Definite Loops Sequences Software Development Input Processing Output Pattern Variable Names / Identifiers What are legal identifiers Which identifiers are unique Reserved Words Expressions Output Statements