📱

Get Our Mobile App

Take your business learning on the go!

Download on the App StoreGet it on Google Play

Уроки Python с нуля / #4 – Переменные и типы данных

Школа itProger / Программирование21:52

Transcription

Hello! In this lesson, we will learn how to work with variables and explore the data types that exist in the Python language.

Before we begin, I would like to recommend the website itprager.com. On this site, you will find homework assignments and a lot of other useful information. The link to this lesson will be in the description of this video.

To start, let's find out what a variable is. A variable is essentially a memory cell where we can store some information, and we can refer to this information later. We can view, modify, delete, or simply display this information on the screen, and we can perform any other actions with it.

To create a variable, we just need to specify a name for it. The name can be anything, but it's important not to use special characters like these. You can name the variable anything you like, but it's best to name variables meaningfully.

For example, I want to create a variable where I will store a number, so I will name it "number." I want to set the value of this variable to, say, the number 5. To assign a value to this variable, I need to use the equals sign and then specify the value, for example, the number 5.

After this, we understand that we have created a variable called "number," and it holds the value 5. I could also store another value here, like -5, -500, or just 500, or something similar. For now, let's just keep the value 5.

Later, we will also learn how to store not only integers but also floating-point numbers, strings, and other data types that exist in Python.

For now, let's understand how to work with this variable after its creation. The first thing you can do is display it on the screen. To do this, we can use the print function and simply pass the variable "number" inside this function.

If we run this, we will see that the number 5 is displayed here. Additionally, you can concatenate the output. For example, I can write "Result: " and then display the value of the variable. This all works correctly.

Besides displaying, you can also change the value of the variable. For instance, I initially set it to 5, and later, I might change it to 7. Then, let's display this variable again, and we will notice that initially, it displayed the value 5, and then it displays the value 7. Everything is processed correctly.

When you create variables, you can initially assign one value to them, and later, during the program's execution, you can assign a different value.

If you need to delete a variable, you can use the "del" directive, which stands for "delete," and then specify the variable you want to delete. Since we are working with the directive, you don't need to use parentheses. If you do, there won't be an error, but they are unnecessary, so why add extra symbols?

If we try to run the program now, we will encounter an error. The error occurs because I created a variable, then deleted it, so it no longer exists, and then I try to refer to this variable here and display its value. Since this variable does not exist, we get an error.

Errors are always displayed in the terminal, so don't hesitate to read them, as they can be very informative. For example, we currently have a NameError, which indicates that the variable "number" is undefined.

If we look at line four, we see that this is indeed the case. We have a variable that is undefined because we deleted it, but we are trying to refer to it.

If I write "del" here, there will be no errors because I initially created the variable, then deleted it, and then recreated it, so everything processes correctly without issues.

In addition to storing integers, we can also store other values in variables. Let's create a new variable, which I will call "digit." Again, the name can be anything, but avoid special characters.

This time, I want to set a floating-point number, for example, 4.5. This will be processed correctly. You can also specify many digits after the decimal point, and it will be handled correctly.

Besides storing such numbers, we can also store strings in variables. Let's create another variable called "word," and I want to store a string here. A string can be enclosed in either single or double quotes; there is no difference.

For example, let's write "Hello" or something more logical. Let's write "Result," and below, we will display the variable "word." We can also display "digit" to notice the difference.

If we run this now, we will see that the result displays the value from the variable "word" and the value from the variable "digit."

Thus, we can store integers, which can be both positive and negative, as well as floating-point numbers, which can also be positive or negative. All of this will be processed correctly.

Additionally, we can store strings, which can contain many characters or just one character. You can even store an entire poem; it will also be processed correctly.

Besides all these data types, there is another non-standard data type called Boolean. This data type allows us to store a variable with one of two possible values: either True or False.

True represents truth, and False represents falsehood. Such values are often used in various checks. For example, we can check if a variable has the value True, and if it does, we will execute one piece of code; if it has the value False, we will execute another piece of code.

So, let's create a variable called "isTrue." We won't use it much right now, but later, when we study conditional statements, we will use this data type.

We can assign either the value True or False to it. If I try to display it on the screen, it will be shown as if it were a regular string. You see, it displays either "False" or "True" as a regular string.

However, this is not a regular string; it is a Boolean data type, and we will use it later in various conditions. For example, we can check if this variable is True, and then execute a specific piece of code; otherwise, we will execute another piece of code.

A few words about data types in Python: there is no strict typing. This means that when creating a variable, we do not need to specify what data type it will be. However, each variable has a specific data type.

For instance, when we create an integer, we are essentially saying that this variable is of the integer type, or simply "int." The integer type is specifically for whole numbers.

When we create a floating-point number, we create a variable of the float type. When we create a string, we create a variable of the string type. And when we create a Boolean variable, we create a variable based on the Boolean type.

All these data types exist implicitly, and it's very easy to demonstrate this. For example, let's try to add something to our variable "word."

If I try to run this, we will encounter an error because we are trying to add a Boolean type to a string. Different data types cannot be added together, so this will result in an error.

The same will happen if we try to add "word" to "digit." It may sound logical to add a number to a string, but it won't work because we are trying to add a float to a string.

To handle this error, we can use a couple of methods. First, we can output them using a comma, which is the first option. The second option is type conversion.

For example, I can convert any number or string to another data type. Let's say I want to display the number as a string. I can do this by using the str function, which converts a number, whether it's a float or an integer, to a string.

If I run this, we will see that there are no changes; everything processes correctly without errors.

Now, let's create another variable called "strNum," which will contain a number, but this number will be in string format.

Again, the data type of this variable is string because we have double quotes. For interest, I will use single quotes here to remember that both exist.

Now, let's try to add "number" to "strNum." If I run this, we will get an error because "number" is an integer and "strNum" is a string.

To handle this, we can use the int function, which converts a string to a number. However, if we try to use this function on a variable like "word," we will get an error because it does not contain a number; it contains text.

If we try to run this in this context, it will process correctly. We can see that I added 5 to the number 5, and we got 10.

Now, let's also display "word." I can output it using a comma, and everything will process correctly. But if I want to use the plus sign to display it, I need to convert it to a string.

To do this, I will call the str function and place the addition of these two numbers inside it. After the addition is performed, the str function will convert it to a string, and if I run this, everything will work correctly.

Thus, in Python, there is no strict typing, meaning we do not need to explicitly specify the data type when creating a variable. However, each variable has its own specific data type.

The main data types are integer, float, string, and the Boolean types we just discussed. It's important to remember that if you try to add a string to an integer, you will always encounter an error because they are different data types.

If you need to add or subtract values, always perform type conversion using functions like str, int, or float.

Now, let's use the knowledge we've gained to create something interesting. I want to develop a program where we will get two numbers from the user and then display all possible mathematical operations related to these two numbers.

To get the values, I will use the input function. In the previous lesson, we already discussed this. I will write "Enter the first number," and then I will write "Enter the second number."

If I run the program now, I can enter the first number and the second number, and the program will end there without anything interesting happening.

So, we need to store the result we get from the user in a separate variable. Let's create a variable called "num1" to store the first value from the user, and then I will create "num2" for the second value.

Below, we will display various results using print statements. I will write some text, like "Result," and then display the result itself.

First, let's add the first variable to the second variable. Below, we will also display subtraction, division, and multiplication of the numbers.

If we run this now and enter some numbers, we will get an error. The error occurs because every time we get a value from the user, it is a string.

So, when I try to subtract one string from another, it results in an error because that is not allowed. To avoid this error, we need to convert the input to an integer right when we get it from the user.

This way, there will be no errors. If we run the program now, everything will process correctly. For example, if I enter the number 5 and then the number 2, we will get all the results: addition, subtraction, division, and multiplication.

Everything processes correctly. You can also perform exponentiation or integer division if you want.

If we run this and test it, we will see that 5 raised to the power of 2 equals 25, and the integer division shows only the whole part.

You can perform various mathematical operations easily.

Additionally, if you need to perform a mathematical operation on the same variable, you can do it in several ways. For example, if I want to add 5 to "num1," I can write it like this, and it will work correctly.

You can also use a more concise format by using "+=" to add a specific number. If we test this, we will see that the first result shows 8 because we added 5 to "num1," which was initially 3.

The same applies to subtraction, multiplication, and division.

Another point I forgot to mention is that you can multiply not only numbers but also strings. For example, if I have a string "Hi" and multiply it by 2, we will see that the string is displayed twice.

If I multiply it by 20, the string will be displayed 20 times.

One more nuance regarding data types: when you assign a value to a variable in Python, that variable immediately gets a data type. For example, if I assign a string to a variable, it will have the string type.

However, you can also assign a new value of a different data type to the same variable. If I assign the number 45 or a float or a Boolean, it will still work correctly.

This happens because when we try to assign a new data type to a variable, the old variable is deleted, and a new one is created.

Thus, you can create a variable with one value and later assign a different value of a different data type to it.

However, when it comes to mathematical operations or displaying multiple data types, you will need to perform type conversions as we did earlier.

Well, that concludes our lesson. In this lesson, we studied how to work with variables and even got a bit practical with them.

In future lessons, we will constantly work with variables, so if something was unclear now, don't worry, as you will encounter them many more times and will learn them thoroughly.

That's all from me. I hope you enjoyed the lesson. If so, don't forget to subscribe to the channel and join our social media. All the links will be in the description of this video.

That's all for now. See you soon! Goodbye!