0% found this document useful (0 votes)
10 views

2 Computer Programming (Lecture 2)

Uploaded by

simonadel898
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

2 Computer Programming (Lecture 2)

Uploaded by

simonadel898
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

04/04/1446 Dr. Hoda M.

Waguih
Python

CHAPTER 2

Input,
Processing,
and Output

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

 Designing a Program
 Input, Processing, and Output
 Displaying Output with print Function
 Comments
 Variables
 Reading Input from the Keyboard
 Performing Calculations
 More About Data Output

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

1
04/04/1446 Dr. Hoda M. Waguih
Python

 Programs must be designed before they are written


 Program development cycle:
◦ Design the program
◦ Write the code
◦ Correct syntax errors
◦ Test the program
◦ Correct logic errors

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

 Design is the most important part of the program


development cycle
 Understand the task that the program is to perform
◦ Work with customer to get a sense what the program
is supposed to do
◦ Ask questions about program details
◦ Create one or more software requirements

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

2
04/04/1446 Dr. Hoda M. Waguih
Python

 Determine the steps that must be taken to perform the task


◦ Break down required task into a series of steps
◦ Create an algorithm, listing logical steps that must be taken

 Algorithm: set of well-defined logical steps that must be


taken to perform a task

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

 Pseudocode: fake code


◦ Informal language that has no syntax rule
◦ Not meant to be compiled or executed
◦ Used to create model program
 No need to worry about syntax errors, can focus on
program’s design
 Can be translated directly into actual code in any
programming language

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

3
04/04/1446 Dr. Hoda M. Waguih
Python

 Flowchart: diagram that graphically depicts the steps in


a program
◦ Ovals are terminal symbols
◦ Parallelograms are input and output symbols

◦ Rectangles are processing symbols

◦ Symbols are connected by arrows that


represent the flow of the program

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Flowcharts are a great way to visually represent the


flow of a program before actually writing the code.

Example 1: Calculate Sum of Two Numbers


Example 2: Calculate Average of Two Numbers
Example 3: Calculate Area of a Rectangle
Example 4: Calculate Employee Gross Pay
Example 5: Calculate Employee Net Pay

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

4
04/04/1446 Dr. Hoda M. Waguih
Python

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

 Designing a Program
 Input, Processing, and Output
 Displaying Output with print Function
 Comments
 Variables
 Reading Input from the Keyboard
 Performing Calculations
 More About Data Output

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

5
04/04/1446 Dr. Hoda M. Waguih
Python

 Typically, computer performs three-step process


◦ Receive input

 Input: any data that the program receives while it is running

◦ Perform some process on the input

 Example: mathematical calculation

◦ Produce output

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

 Designing a Program
 Input, Processing, and Output
 Displaying Output with print Function
 Comments
 Variables
 Reading Input from the Keyboard
 Performing Calculations
 More About Data Output

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

6
04/04/1446 Dr. Hoda M. Waguih
Python

 Function: piece of prewritten code that performs an


operation
 print function: displays output on the screen
 Argument: data given to a function
◦ Example: data that is printed to screen

 Statements in a program execute in the order that they


appear
◦ From top to bottom

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

In Python, the print function is used displays output on the screen

Syntax
print (object(s), sep=separator, end=end, file=file, flush=flush)

Parameter Description
object(s) Any object, and as many as you like. Will be converted to string before
printed
sep='separator' Optional. Specify how to separate the objects, if there is more than one.
Default is ' '
end='end' Optional. Specify what to print at the end. Default is '\n' (line feed)

file Optional. An object with a write method. Default is sys.stdout

flush Optional. A Boolean, specifying if the output is flushed (True) or


buffered (False). Default is False

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

7
04/04/1446 Dr. Hoda M. Waguih
Python

 String: sequence of characters that is used as data

 String literal: string that appears in actual code of


a program

◦ Must be enclosed in single (‘) or double (“) quote marks

◦ String literal can be enclosed in triple quotes (''' or """)

 Enclosed string can contain both single and double quotes


and can have multiple lines

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

8
04/04/1446 Dr. Hoda M. Waguih
Python

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

 Designing a Program
 Input, Processing, and Output
 Displaying Output with print Function
 Comments
 Variables
 Reading Input from the Keyboard
 Performing Calculations
 More About Data Output

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

9
04/04/1446 Dr. Hoda M. Waguih
Python

 Comments: notes of explanation within a program


◦ Ignored by Python interpreter
 Intended for a person reading the program’s code
◦ Begin with a # character

 End-line comment: appears at the end of a line of code


◦ Typically explains the purpose of that line

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

10
04/04/1446 Dr. Hoda M. Waguih
Python

 Designing a Program
 Input, Processing, and Output
 Displaying Output with print Function
 Comments
 Variables
 Reading Input from the Keyboard
 Performing Calculations
 More About Data Output

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

 Variable: name that represents a value stored in the


computer memory
◦ Used to access and manipulate data stored in memory
◦ A variable references the value it represents
 Assignment statement: used to create a variable and
make it reference data
◦ General format is variable = expression
 Example: age = 29

 Assignment operator: the equal sign (=)

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

11
04/04/1446 Dr. Hoda M. Waguih
Python

 In assignment statement, variable receiving value must


be on left side
 A variable can be passed as an argument to a function
◦ Variable name should not be enclosed in quote marks

 You can only use a variable if a value is assigned to it

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

What if you enclose the variable you pass as an


argument to the print function in quotes

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

12
04/04/1446 Dr. Hoda M. Waguih
Python

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

13
04/04/1446 Dr. Hoda M. Waguih
Python

 Rules for naming variables in Python:


˗ Variable name cannot be a Python key word
˗ Variable name cannot contain spaces
˗ First character must be a letter or an underscore
˗ After first character may use letters, digits, or underscores
˗ Variable names are case sensitive
 Variable name should reflect its use

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

14
04/04/1446 Dr. Hoda M. Waguih
Python

 Python allows one to display multiple items with a single


call to print
◦ Items are separated by commas when passed as arguments
◦ Arguments displayed in the order they are passed to the function
◦ Items are automatically separated by a space when displayed on
screen

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

15
04/04/1446 Dr. Hoda M. Waguih
Python

 Variables can reference different values while program


is running
 Garbage collection: removal of values that are no
longer referenced by variables
◦ Carried out by Python interpreter

 A variable can refer to item of any type


◦ Variable that has been assigned to one type can be reassigned
to another type

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

16
04/04/1446 Dr. Hoda M. Waguih
Python

 Data types: categorize value in memory


◦ e.g., int for integer, float for real number, str used for storing
strings in memory

 Numeric literal: number written in a program


◦ No decimal point considered int, otherwise, considered float

 Some operations behave differently depending on data


type

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

17
04/04/1446 Dr. Hoda M. Waguih
Python

 A variable in Python can refer to items of any type

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

 Designing a Program
 Input, Processing, and Output
 Displaying Output with print Function
 Comments
 Variables
 Reading Input from the Keyboard
 Performing Calculations
 More About Data Output

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

18

You might also like