Gaddis Python 4e Chapter 02 PPT - combinedForLesson2
Gaddis Python 4e Chapter 02 PPT - combinedForLesson2
Input,
Processing,
and Output
>>> print(123)
123
>>> print(98.6)
98.6
>>> print('Hello world')
Hello world
Strings and String Literals
• 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
# All done
print(bigword, bigcount)
Variables
• 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 (=)
x = 12.2 x 12.2
y = 14
y 14
Variable Naming Rules
• 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
http://en.wikipedia.org/wiki/Mnemonic
x1q3z9ocd = 35.0
x1q3z9afd = 12.50
x1q3p9afd = x1q3z9ocd * x1q3z9afd
print(x1q3p9afd)
What is this
bit of code
doing?
x1q3z9ocd = 35.0 a = 35.0
x1q3z9afd = 12.50 b = 12.50
x1q3p9afd = x1q3z9ocd * x1q3z9afd c=a*b
print(x1q3p9afd) print(c)
What are
these bits of
code doing?
x1q3z9ocd = 35.0 a = 35.0
x1q3z9afd = 12.50 b = 12.50
x1q3p9afd = x1q3z9ocd * x1q3z9afd c=a*b
print(x1q3p9afd) print(c)
x = 3.9 * x * ( 1 - x )
A variable is a memory
location used to store a x 0.6
value (0.6)
0.6 0.6
x = 3.9 * x * ( 1 - x )
0.4
0.936
The right side is an expression.
Once the expression is
evaluated, the result is placed
in (assigned to) x.
A variable is a memory location
used to store a value. The
value stored in a variable can
be updated by replacing the x 0.6 0.936
old value (0.6) with a new
value (0.936).
0.6 0.6
x = 3.9 * x * ( 1 - x )
0.4
• We can instruct
Python to pause name = input('Who are you? ')
and read data print('Welcome', name)
from the user
using the input()
function
Who are you? Cihan
• The input() Welcome Cihan
function returns a
string
Converting User Input
• If we want to read
a number from the
user, we must
convert it from a
string to a number
using a type inp = input('Europe floor?')
conversion usf = int(inp) + 1
print('US floor', usf)
function
• Later we will deal Europe floor? 0
with bad input data US floor 1
Performing Calculations
• Math expression: performs calculation and
gives a value
• Math operator: tool for performing calculation
• Operands: values surrounding operator
• Variables can be used as operands
• Resulting value typically assigned to variable
• Two types of division:
• / operator performs floating point division
• // operator performs integer division
• Positive results truncated, negative rounded away from zero
4 3
>>> yy = 440 * 12 >>> print(4 ** 3) - Subtraction
>>> print(yy) 64
5280 * Multiplication
>>> zz = yy / 1000
>>> print(zz) 4R3 / Division
5.28
5 23
20 ** Power
3
% Remainder
Order of Evaluation
• When we string operators together - Python must
know which one to do first
• This is called “operator precedence”
• Which operator “takes precedence” over the
others?
x = 1 + 2 * 3 - 4 / 5 ** 6
Operator Precedence Rules
1+2*5
Parenthesis
Power 1 + 10
Multiplication
Addition 11
Left to Right
Operator Precedence
Parenthesis
Power
Multiplication
• Remember the rules top to bottom Addition
Left to Right
• When writing code - use parentheses
• When writing code - keep mathematical
expressions simple enough that they are easy to
understand
• Break long series of mathematical operations up
to make them more clear
Mixed-Type Expressions and
Data Type Conversion
• Data type resulting from math operation
depends on data types of operands
• Two int values: result is an int
• Two float values: result is a float
• int and float: int temporarily converted to float,
result of the operation is a float
• Mixed-type expression
• Type conversion of float to int causes truncation
of fractional part
>>> print(10 / 2)
Integer division produces 5.0
a floating point result. >>> print(9 / 2)
4.5
>>> print(99 / 100)
0.99
>>> print(10.0 / 2.0)
5.0
>>> print(99.0 / 100.0)
0.99
INTEREST_RATE = 0.069
import turtle
• When the turtle's pen is up, the turtle does not draw
as it moves.