Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
www.umbc.edu
www.umbc.edu
www.umbc.edu
Todays Objectives
To learn more about expressions
To learn Pythons operators
Including mod and integer division
www.umbc.edu
Expressions
Expressions are code that produces or
calculates new data and data values
Allow us to program interesting things
Always on the right hand side of the
assignment operator
www.umbc.edu
Pop Quiz!
Which of the following examples are correct?
1.
2.
3.
4.
5.
6.
7.
6
500 = numStudents
numStudents = 500
numCookies * cookiePrice = total
mpg = miles_driven / gallons_used
"Hello World!" = message
_CMSC201_doge_ = "Very learning"
60 * hours = days * 24 * 60
www.umbc.edu
Pythons Operators
www.umbc.edu
operator
www.umbc.edu
focus of
todays lecture
Arithmetic Operators
Comparison (Relational) Operators
Assignment Operators
Logical Operators
Bitwise Operators
Membership Operators
Identity Operators
www.umbc.edu
Operators in Python
Operator
+
*
/
//
%
**
10
Meaning
Addition
Subtraction
Multiplication
Division
Integer division
Modulo (remainder)
Exponentiation
www.umbc.edu
www.umbc.edu
www.umbc.edu
www.umbc.edu
7
7
2
2
4
/
//
/
//
//
5
= 1.4
5
= 1
8
= 0.25
8
= 0
17 // 5 = 0
evaluate from left to right
14
www.umbc.edu
Operators Modulo
Also called modulo, modulus, or mod
Example: 17 % 5 = 2
What do you think mod does?
www.umbc.edu
Examples: Mod
Mod uses the percent sign (%)
Examples:
1.
2.
3.
4.
5.
16
7 % 5
=
5 % 9
=
17 % 6
=
22 % 4
=
48692451673
2
5
5
2
% 2 = 1
www.umbc.edu
Modulo Answers
Result of a modulo operation will always be:
Positive
No less than 0
No more than the divisor minus 1
Examples:
1.
2.
3.
17
8 % 3
21 % 3
13 % 3
= 2
= 0
= 1
Operators Exponentiation
Exponentiation is just another word for
raising one number to the power of another
Examples:
1.
2.
3.
4.
18
binary8
cubeArea
cubeVolume
squareRoot
=
=
=
=
2 ** 8
length ** 2
length ** 3
num ** (0.5)
www.umbc.edu
Order of Operations
in what
Expressions are evaluated from
leftdirection?
to right
Operator(s)
**
/ * // %
+
-
Priority
highest
lowest
www.umbc.edu
Types in Python
www.umbc.edu
Variable Types
There are many different kinds of variables!
Numbers
Whole numbers (Integers)
Decimals
(Floats)
Booleans (True and False)
Strings (collections of characters)
21
www.umbc.edu
22
>>> b = "moo"
>>> type(b)
<class 'str'>
www.umbc.edu
23
>>> print("Hello")
Hello
>>> 4 + 7
lines without a >>>
are Pythons response
11
>>>
www.umbc.edu
www.umbc.edu
Division Examples
What do the following expressions evaluate to?
1.
2.
3.
4.
5.
6.
7.
25
4
4
4
8
8
5
5
/
//
//
/
/
/
//
3
3
3.0
3
2
7
7
=
=
=
=
=
=
=
1.3333333333333333
1
1.0
2.6666666666666667
4.0
0.7142857142857143
0
www.umbc.edu
www.umbc.edu
Casting to a Type
We can change a variable from one type to
another using casting
Example:
>>> e = 2.718
>>> int(e)
2
>>> str(e)
'2.718'
27
www.umbc.edu
www.umbc.edu
www.umbc.edu
Constants
www.umbc.edu
www.umbc.edu
Using Constants
Calculating the total for a shopping order
MD_TAX
= 0.06
32
Magic Numbers
Magic numbers are numbers used directly in
the code should be replaced with constants
Examples:
Mathematical numbers (pi, e, etc.)
Program properties (window size, min and max)
Important values (tax rate, maximum number of
students, credits required to graduate, etc.)
33
www.umbc.edu
www.umbc.edu
Magic Everything
Can also have magic characters or strings
Use constants to prevent any magic values
HIT):
if (userChoice == "H"):
if (userChoice ==
www.umbc.edu
www.umbc.edu
www.umbc.edu
Version of Python
After typing python -v
38
www.umbc.edu
Announcements
Your Lab 1 is happening this week!
First graded lab; attend your assigned section
www.umbc.edu
Practice Problems
Write a program that gets a price from the user,
and uses arithmetic operators to calculate the
dollars and pennies (e.g., 7.55 = $7, 55)
Update the program to check if the value is
negative, and print out an error message if it is
www.umbc.edu