Math, String Operations and Comparisons
In the last lesson we have learned how to output data to our screen, how to declare a variable and what different core data types exists in Python. In this lesson we will learn how math works in Python.
Lesson Overview
At the end of the following lesson you will be confident at:
- Doing math with numbers in Python
- Doing math with strings in Python
- Comparing results to see if they are the same or one is bigger than the other
Performing Operations on Numbers
Number data types, int and float are very useful when we must do some math.
Addition
Adding two numbers together can be done with + operator.
Subtraction
Subtraction of two numbers is done with - operator.
Multiplication
Multiplication of two numbers is done with * operator.
Division
Division of two numbers is done with / operator.
Division always returns float
You must be careful when dividing two numbers as the second number must not be equal to 0. If the divisor (second number) is 0, Python will raise ZeroDivisionError.
Floor Division
Sometimes we may need to only get whole number from division and we dont care about the remainder. In that case we can use something called floor (integer) division.
Floor division is done with // operator.
Modulo Operator
Sometimes we may need to know remainder of division but we do not care about the whole number, only the remainder. In that case we use modulo operator.
Modulo in done with % operator.
Result of this operation is 1 because 3 goes into 10 three times and whats left is 1.
Exponentiation
Raises the left value to the power of the right value.
Exponentiation is done with ** operator.
Performing Operations on Strings
We can perform various operations on strings in Python. For example, we can:
Add Strings Together
Multiply Strings
Get a Single Letter
Python always starts counting at 0. This is called an index.
Convert Case
Strings Cannot Be Modified in Place
Strings in Python are immutable. This means that they cannot be modified in place and every operation on strings results in a new string.
We demonstrate this in the following example
String Formatting
Modern Python recommends usage of f-string for string formatting. Only difference is that we use letter f in front of the string to mark it as f-string.
Then we can use { } syntax to inject variables directly into strings.
Comparisons
We can compare values
In Python we use these comparison operators:
- Equal to (
==) - Not equal to (
!=) - Greater than (
>) - Less than (
<) - Greater than or equal to (
>=) - Less than or equal to (
<=)
All of these return a boolean value. True or False
Logical Operators
In Python we have 3 logical operators. These are:
andornot
and Logical Operator
and operator is used to check if both conditions (on left and right side) are True. If so the whole expression is evaluated to True.
or Logical Operator
or operator is used when we need to check if any of the conditions are True. If so, whole expression is evaluated to True
not Logical Operator
not operator is used for inverting logic. If something is True it will invert it to False
Excercise
Try to solve excercise-3 for better understanding.
Assignment
In the last assignment we have declared our shop variables and printed the inventory. In this one you are tasked with:
- Open your code editor in your last project directory
simple-python-shop - Open
main.pyfile we have been working on - Standardize your strings:
- Convert
shop_nameto uppercase (e.g.,MERLIN SHOP) - Convert item name to titlecase (e.g.,
Excalibur)
- Convert
- Calculate the checkout price:
- Create variable
totalby multiplyingitem_priceanditem_quantity
- Create variable
- Comparison checks:
- Create a boolean
is_large_orderthat evaluates whetheritem_quantityis greater then or equal to3.
- Create a boolean
- Update your output to use f-string :
Welcome to MERLIN SHOP
--------------------------------
Item: Excalibur
Purchased: 3 x 67.20
Total: $201.60
Large Order: True
--------------------------------
Thank you for your purchase ! - Commit your changes with
gitand push to Github
Deepen Your Knowledge
- Learn more about Basic math in Python from article in Stanford University, covering all the topics in this lesson but in a different style and a bit more.
- Broaden your knowledge about Strings from this Google article and read Common String Operations from official Python documentation.
- Read about Comparisons and Boolean Operators - and, or and not in official Python documentation.
What's Next
Now that we have learned how to do calculations and comparisons, its time for our program to change behavior based on those calculations or comparisons. Lets dive in conditionals.