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 2.
Let’s begin with what we will learn. We will cover data types, such as text (strings), numbers (integers and floating-point), and Boolean values. We will also learn how to declare variables as either text or numbers.
Now, let’s discuss why this is important.
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.
Finally, let’s talk about how we will learn about data types and variables.
We will complete several exercises together to understand how to declare a variable as a specific data type.
This includes declaring a variable as a string (text) or as a number (integer, floating-point, or Boolean).
We will cover more advanced topics like strings and data types, such as arrays, in chapters 3, 5, 7, and 8.
Python syntax is straightforward and easy to understand, making it great for both beginners and experienced programmers.
Understanding syntax is like learning the grammar of a language. It is the base for all your future coding projects.
So, let’s start by talking about variables.
Let’s write our first line of code together. We are going to create a variable named name
and assign it the value “Apsock”.
Here’s how we do it: name = "Apsock"
. The word Apsock is in quotes because it is a string.
We have created a variable called name
and given it the value “Apsock”.
Now, let’s go to Google Colab, which we will call Colab from now on.
Click on “New” and then “New Notebook”. In the new notebook, enter the following code: name = "Apsock"
.
When you run the code by pressing the play button, it might seem like nothing happened, but something did. We told Python that our variable name
has the value “Apsock”.
You can also use single quotes instead of double quotes, and it will work the same way. For example: name = 'Apsock'
.
Python knows that what we just created is a string because we put Apsock in quotes. Strings are a type of data in Python. Think of strings as text or anything in quotes.
Strings can get more complicated, and we will build on this throughout the course. It’s important to note that Python is case-sensitive.
If we change the code to use uppercase letters, like String
, we will get an error because Python recognizes string
and String
as different.
Now, there are different types of data, or data types, in Python.
Let’s discuss another data type called int, which stands for integer. An integer is a whole number, like one, two, or three.
What if we try to define the name Apsock as an integer and run the code? We would get an error because Apsock is not a number.
Let’s add another line of code to define age as 44: age = 44. Python knows that 44 is an integer.
We could also use int to get the same result: age = int(44).
Python is smart enough to understand that 44 is an integer, whether we write it directly or use the int function.
We used the print function to show “Hello World” to the user. This is called a function.
The print function has one goal: to show a message, or information, back to the user.
There are many built-in Python functions that make our lives easier by accomplishing specific tasks for us.
We can even define our own functions, which we will discuss in Level 4. In this level, we will talk about functions in more detail.
So, let’s go back to Colab together.
I want you to add a second line of code to Colab and print the variable called name
. You can pause and try this in Colab, then press play to run the code.
Here’s how to use the print
function to print the variable called name
:
name = "Apsock"
print(name)
Notice we didn’t need to put quotes around name
. This is because we already defined the variable name
as “Apsock”.
Please remember to comment your code. This is important in case you come back to see your code in the future or if someone else needs to understand what you were trying to do.
We know that Python skips the commented line of code. You can also type comments after the code:
name = "Apsock" # This is a string variable
print(name) # This prints the name variable
type()
function.
For example:
type(name) # This will tell us that 'name' is a string
age
is:
age = 44
type(age) # This will tell us that 'age' is an integer
Now that we understand how to use string and integer data types, let’s talk about two more data types: float and Boolean.
Let’s start with floating point numbers, which are called float. An integer is a whole number like one, two, or three.
pi = 3.14
print(pi)
print(type(pi))
pi = float(3.14)
Another important data type is Boolean, which represents true or false values.
In programming, this can also be written as 0 or 1, where 1 means true and 0 means false.
Here’s an example using Boolean:
is_sunny = True
print(is_sunny)
print(type(is_sunny))
is_sunny = bool(True)
is_sunny = bool(1)
is_sunny = bool(0)
Why is this important?
In more advanced programming that we will do later in this book, we might want to use true or false logic to control the flow of our programs.
Understanding these basic data types is crucial as we build more complex applications.
name = "Apsock"
print(name)
name
from “Apsock” to “Python”?
We can add a third line of code to make the change and a fourth line to print the updated variable:
name = "Apsock"
print(name)
name = "Python"
print(name)
Now, let’s discuss how to change a variable’s data type from a string to an integer.
In the previous chapters, we used str
for strings, int
for integers, float
for floating-point numbers, and bool
for Booleans to define our variables in different data types.
Let’s make a variable called age
, and instead of setting it to 100, let’s define this number in double quotes:
age = "100"
age
as a string because we put 100 in quotes. To convert age
from a string to an integer, we can use the following code:
age = int(age)
print(age)
print(type(age))
Another important thing to keep in mind is the consequence of converting a floating-point number to an integer.
When we convert a number that has a decimal point to an integer, everything after the decimal point is discarded, which means we lose some information.
For example:
pi = 3.14
pi_as_int = int(pi)
print(pi_as_int)
No matter how close the floating-point number is to the next whole number, everything after the decimal point is discarded, and only the whole number part is kept.
This process does not round the number; it simply cuts off the decimal part. Later in this course, we will discuss how to use the rounding function to handle such cases more precisely.
Please complete the following steps below:
C3P0 is 1.75 meters tall & R2D2 is 1.09 meters tall.
Step 1: Create a variable called height as a string type
Step 2: Assign the height a value of 1.09
Step 3: Print the height of R2D2
Step 4: Change the height data type from string to floating
Step 5: Add 0.66 to 1.09
Step 6: Print the new height
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.