Introduction to Python
Google uses Python extensively in its web search systems and even employed Guido van Rossum, the inventor of Python.
Welcome to Level 3.
Let’s start with what we will learn. In this level, we will cover arithmetic in Python.
We will also introduce a concept that lets us connect variables called “strings.” I put “strings” in double quotes here to avoid confusion with the str
data type.
This will make more sense as we go through Level 3.
Now, why do we need to talk about math in Python?
Because sometimes, Python requires us to use a double equal sign (==) instead of a single equal sign (=).
There are a few other math rules in Python that we need to understand.
Lastly, how are we going to learn about these math and string concepts?
We will do several exercises together to understand how to use Python math and strings.
Once we have a variable of a data type that is either an integer or a float, we can perform mathematical operations on it.
We will soon introduce more data types that can use math.
Python follows the typical math rules, where multiplication and division take precedence over addition and subtraction.
For example,
if we set age = 44
and then calculate age + 1 * 2
, the result is 46.
This happens because Python first multiplies 1 by 2 and then adds the result to 44.
If we want to add 1 to the age before multiplying by 2, we need to use brackets.
For example,
(age + 1) * 2
. This will first add 1 to the age and then multiply the result by 2.
Just like in Microsoft Excel, once you open a bracket, you must also close it.
Let’s talk about combining variables, or “stringing them together.” We’ll also discuss how to use a variable like name
in a sentence.
For example,
if we want to create a sentence that includes a name, we use the +
sign to combine the text.
name = "Apsock"
print("Your Name is "+name)
Make sure to include a space after “Your name is ” so that it doesn’t look like “Your name isApsock.”
When using the print
statement, remember that you cannot directly combine an int
data type with a string. You must first convert the int
to a str
to avoid errors.
This conversion temporarily changes the data type to a string, allowing you to use it in the print
statement. The rule is that when you combine variables, they must all be strings.
Ahsoka is 1.70 meters tall, Yoda is 0.66 meters tall, R2D2 is 1.09 meters tall and
C3P0 is 1.75 meters tall
Step 1: Get the average height of the 4 characters
Step 2: Print the average height
Step 3: Define 4 variables in your calculations as follows:
Ahsoka heigh, Yoda height, R2D2 height, C3P0 height
Please complete the following steps:
Step 1: Define 2 names as Jack & Rose
Step 2: Define the year as 1912
Step 3: Using the 2 name variables and the year variable,
print this:
Jack and Rose were on the Titanic in 1912
Google uses Python extensively in its web search systems and even employed Guido van Rossum, the inventor of Python.
Why do we need to know if a data type is text or a number? Because Python needs to understand if an item is a number to perform calculations with it.
Once we have a variable of a data type in python that is either an integer or a float, we can perform mathematical operations on it.
Why should we learn to use Python built-in functions? Because they can save us a lot of time when we’re programming.
We already discussed how to use several data types. Now, we’ll talk about another data type called list.