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

CE100 - Chapter 3 - Operators (Expressions, Data Types and Variables)

Here is a program that prompts the user for hours and rate per hour and computes the pay: print('Enter hours: ') hours = float(input()) print('Enter rate per hour: ') rate = float(input()) pay = hours * rate print('Pay: ', pay) This program: 1. Prompts the user to enter hours and stores it in a variable hours as a float 2. Prompts the user to enter rate per hour and stores it in a variable rate as a float 3. Computes pay by multiplying hours and rate 4. Prints the result
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

CE100 - Chapter 3 - Operators (Expressions, Data Types and Variables)

Here is a program that prompts the user for hours and rate per hour and computes the pay: print('Enter hours: ') hours = float(input()) print('Enter rate per hour: ') rate = float(input()) pay = hours * rate print('Pay: ', pay) This program: 1. Prompts the user to enter hours and stores it in a variable hours as a float 2. Prompts the user to enter rate per hour and stores it in a variable rate as a float 3. Computes pay by multiplying hours and rate 4. Prints the result
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Variables, Expressions, and

Statements
Chapter 3

Python for Informatics: Exploring Information


www.pythonlearn.com
Constants
• Fixed values such as numbers, letters, and strings are
called “constants” - because their value does not
change
• Numeric constants are as you expect
• String constants use single-quotes (') >>> print 123
or double-quotes (") 123
>>> print 98.6
98.6
>>> print 'Hello world'
Hello world
Variables
• A variable is a named place in the memory where a programmer
can store data and later retrieve the data using the variable
“name”

• Programmers get to choose the names of the variables

• You can change the contents of a variable in a later statement

x = 12.2 x 12.2 100


y = 14
x = 100 y 14
Python Variable Name Rules
• Must start with a letter or underscore _

• Must consist of letters and numbers and underscores

• Case Sensitive

• Good: spam eggs spam23 _speed

• Bad: 23spam #sign var.12

• Different: spam Spam SPAM


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
Sentences or Lines

x=2 Assignment Statement


x=x+2 Assignment with expression
print x Print statement

Variable Operator Constant Reserved Word


Assignment Statements
• We assign a value to a variable using the assignment statement
(=)

• An assignment statement consists of an expression on the right


hand side and a variable to store the result

x = 3.9 * x * ( 1 - x )
A variable is a memory location x 0.6
used to store a value (0.6).

0.6 0.6
x = 3.9 * x * ( 1 - x )
0.4

Right side is an expression.


Once expression is evaluated, 0.93
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 x 0.6 0.93
updated by replacing the old
value (0.6) with a new value
(0.93).

x = 3.9 * x * ( 1 - x )

Right side is an expression.


Once expression is evaluated, 0.93
the result is placed in (assigned
to) the variable on the left side
(i.e. x).
Numeric Expressions
Operator Operation
• Because of the lack of mathematical + Addition
symbols on computer keyboards -
we use “computer-speak” to express - Subtraction
the classic math operations * Multiplication

• Asterisk is multiplication / Division

• Exponentiation (raise to a power) ** Power


looks different from in math. % Remainder
Numeric Expressions
>>> xx = 2 >>> jj = 23 Operator Operation

>>> xx = xx + 2 >>> kk = jj % 5 + Addition

>>> print xx >>> print kk - Subtraction

4 3
* Multiplication
>>> yy = 440 * 12 >>> print 4 ** 3
>>> print yy 64 / Division

** Power
5280
>>> zz = yy / 1000 5 23
% Remainder

>>> print zz 20
5
3
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
• Highest precedence rule to lowest precedence rule

• Parenthesis are always respected

• Exponentiation (raise to a power) Parenthesis


Power
• Multiplication, Division, and Remainder Multiplication
Addition
• Addition and Subtraction Left to Right

• Left to right
>>> x = 1 + 2 ** 3 / 4 * 5 1 + 2 ** 3 / 4 * 5
>>> print x
11 1+8/4*5
>>>
1+2*5

Parenthesis
Power 1 + 10
Multiplication
Addition 11
Left to Right
>>> x = 1 + 2 ** 3 / 4 * 5 1 + 2 ** 3 / 4 * 5
>>> print x
11 1+8/4*5
>>> Note 8/4 goes before 4*5 1+2*5
because of the left-right
rule.
Parenthesis 1 + 10
Power
Multiplication
Addition 11
Left to Right
Operator Precedence Parenthesis
Power
Multiplication


Addition
Remember the rules top to bottom Left to Right

• When writing code - use parenthesis

• 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

Exam Question: x = 1 + 2 * 3 - 4 / 5
Python Integer Division is
Weird!
>>> print 10 / 2
5
>>> print 9 / 2
• Integer division truncates 4
>>> print 99 / 100
• Floating point division produces 0
floating point numbers >>> print 10.0 / 2.0
5.0
>>> print 99.0 / 100.0
0.99
This changes in Python 3.0
Mixing Integer and Floating
• When you perform an >>> print 99 / 100
operation where one 0
operand is an integer and >>> print 99 / 100.0
the other operand is a 0.99
floating point the result is a >>> print 99.0 / 100
floating point 0.99
>>> print 1 + 2 * 3 / 4.0 - 5
• The integer is converted to -2.5
a floating point before the >>>
operation
What does “Type” Mean?
• In Python variables, literals,
and constants have a “type”
>>> ddd = 1 + 4
• Python knows the difference >>> print ddd
between an integer number 5
and a string >>> eee = 'hello ' + 'there'
>>> print eee
• For example “+” means hello there
“addition” if something is a
number and “concatenate” if
something is a string concatenate = put together
Several Types of Numbers
>>> xx = 1
• Numbers have two main types >>> type (xx)
• Integers are whole numbers: -14, -
<type 'int'>
>>> temp = 98.6
2, 0, 1, 100, 401233 >>> type(temp)
• Floating Point Numbers have <type 'float'>
>>> type(1)
decimal parts: -2.5 , 0.0, 98.6, 14.0
<type 'int'>
• There are other number types - they >>> type(1.0)
are variations on float and integer <type 'float'>
>>>
Type Conversions >>> print float(99) / 100
0.99
>>> i = 42
>>> type(i)
• When you put an integer and <type 'int'>
floating point in an expression >>> f = float(i)
the integer is implicitly >>> print f
converted to a float 42.0
>>> type(f)
• You can control this with the <type 'float'>
built in functions int() and >>> print 1 + 2 * float(3) / 4 - 5
float() -2.5
>>>
String Operations
• Some operators apply to strings
• + implies “concatenation” >>> print 'abc' + '123’
• * implies “multiple
Abc123
>>> print 'Hi' * 5
concatenation”
HiHiHiHiHi
• Python knows when it is dealing >>>
with a string or a number and
behaves appropriately
What is this
code doing?

hours = 35.0
rate = 12.50
pay = hours * rate
print pay
Exercise

Write a program to prompt the user for hours and


rate per hour to compute gross pay.
Enter Hours: 35
Enter Rate: 2.75
Pay: 96.25

You might also like