filmov
tv
How to Create a Variable in Python | Python for Beginners
Показать описание
In this Python video tutorial for beginners, we are going to learn how to create a variable in Python.
00:00 Welcome
00:05 Learning Objectives
00:28 What are Variables in Python?
00:38 How to Create a Variable in Python
01:16 Demo: Creating and Accessing a Variable in the Interactive Shell
03:16 Demo: Creating Variables in a Python File and Printing Their Value
04:16 Variable Names are Author-defined
04:27 Variable Naming Rules and Restrictions
06:38 Tip: Use Descriptive Variable Names
07:28 Keywords / Reserved Words in Python
08:10 Changing / Updating a Variable's Value
12:14 Benefits of Using Variables
15:37 Resetting the Interactive Shell in Thonny
16:19 Summary / Recap
In this video, we are going to learn about variables. By the end of this lesson, students should be able to:
Initialize variables
Access variables
Change a variable's value
And enumerate the benefits of using variables
In programming, a variable is a tool that allows a programmer to access a value using a name. To create a variable, specify a name, and then assign a value to it using the equals sign.
Another term that you might see that is used to refer to variables is the term identifier. Now this one is a term that has a wider scope. It can also be used to refer to other types of names. For example, the name of a function, such as print, can also be referred to as an identifier, because it is a name that identifies a specific function.
Ok, so let's go ahead and create a variable: x = 5. This is a variable named x, with a value of 5. Here, we have the name of the variable which is x, and the value which is 5. We assign the value to the name, using the equals sign. This is why this type of statement is called an assignment statement, because we are assigning a value to a variable. And then I will press enter to execute this assignment statement.
Creating this variable will now allow us to access the value 5, using the name x.
Here in the Shell, to access a variable after creating it, you simply need to type its name and press enter. So if I type x and press enter, we see 5. After creating a variable, you can access it as many times as you want. I'll type x again and press enter, and it still gives me 5.
But remember that if we were running this from a python file instead, then you would have to use the print function.
When accessing a variable, you do not put quotation marks around the name, because that would make it a string. If I place x in quotation marks, then this is not referring to the variable x that we created. Rather, this is referring to a literal string value that contains the letter x. So these 2 are completely different entities. If I press enter after typing this string, then the interpreter just echos the same string literal back to me.
Ok so let's go ahead and practice creating a few more variables. Let's do this in the code editor. In the first line I will type x equals 1, in the next line i will type y equals 3.14, and in the next line, I will type z is equal to the string python. So now we have 3 variables. The first one has an integer. The second one has a float. And the third one has a string. Let's go ahead and print out the values. Print x, print y, print z. And then let's save this, and then if we run this, these print statements will output the values assigned to these variables, and NOT the variable names themselves.
Variable names are author-defined. This means that you, as the author of your own program, will be the one to decide what to name your variables. However, there are some rules that need to be followed.
In python, variable names can contain only the following characters:
- the uppercase and lowercase letters A to Z,
- the underscore _
- a variable name can also contain the numerical characters 0 to 9, except that they cannot be the first character in a variable name
Here are some examples of valid variable names:
x1, x2, x3, _x, xYZ, x_y_z
And here are some examples of invalid variable names:
1x and xyz!
1x is invalid because variable names are not allowed to start with a digit. They can contain digits as long as it is not the first character. xyz! is invalid because exclamation points are not allowed in variable names. All special characters, except for the underscore, are not allowed in Python. Take note that in other programming languages, other special characters are also allowed. In Java for example, the dollar sign is allowed.
Variable names are also case sensitive. If you have a variable name that is a lower case x and another one that is an uppercase X, then those two are separate entities. The python interpreter will know that these are 2 separate and distinct variables. It will not be confused by them. Although it might confuse anyone else reading your code, including yourself, so it might not be such a good idea to have these 2 variables in the same program.
#Python #PythonTutorial #LearnToCode
00:00 Welcome
00:05 Learning Objectives
00:28 What are Variables in Python?
00:38 How to Create a Variable in Python
01:16 Demo: Creating and Accessing a Variable in the Interactive Shell
03:16 Demo: Creating Variables in a Python File and Printing Their Value
04:16 Variable Names are Author-defined
04:27 Variable Naming Rules and Restrictions
06:38 Tip: Use Descriptive Variable Names
07:28 Keywords / Reserved Words in Python
08:10 Changing / Updating a Variable's Value
12:14 Benefits of Using Variables
15:37 Resetting the Interactive Shell in Thonny
16:19 Summary / Recap
In this video, we are going to learn about variables. By the end of this lesson, students should be able to:
Initialize variables
Access variables
Change a variable's value
And enumerate the benefits of using variables
In programming, a variable is a tool that allows a programmer to access a value using a name. To create a variable, specify a name, and then assign a value to it using the equals sign.
Another term that you might see that is used to refer to variables is the term identifier. Now this one is a term that has a wider scope. It can also be used to refer to other types of names. For example, the name of a function, such as print, can also be referred to as an identifier, because it is a name that identifies a specific function.
Ok, so let's go ahead and create a variable: x = 5. This is a variable named x, with a value of 5. Here, we have the name of the variable which is x, and the value which is 5. We assign the value to the name, using the equals sign. This is why this type of statement is called an assignment statement, because we are assigning a value to a variable. And then I will press enter to execute this assignment statement.
Creating this variable will now allow us to access the value 5, using the name x.
Here in the Shell, to access a variable after creating it, you simply need to type its name and press enter. So if I type x and press enter, we see 5. After creating a variable, you can access it as many times as you want. I'll type x again and press enter, and it still gives me 5.
But remember that if we were running this from a python file instead, then you would have to use the print function.
When accessing a variable, you do not put quotation marks around the name, because that would make it a string. If I place x in quotation marks, then this is not referring to the variable x that we created. Rather, this is referring to a literal string value that contains the letter x. So these 2 are completely different entities. If I press enter after typing this string, then the interpreter just echos the same string literal back to me.
Ok so let's go ahead and practice creating a few more variables. Let's do this in the code editor. In the first line I will type x equals 1, in the next line i will type y equals 3.14, and in the next line, I will type z is equal to the string python. So now we have 3 variables. The first one has an integer. The second one has a float. And the third one has a string. Let's go ahead and print out the values. Print x, print y, print z. And then let's save this, and then if we run this, these print statements will output the values assigned to these variables, and NOT the variable names themselves.
Variable names are author-defined. This means that you, as the author of your own program, will be the one to decide what to name your variables. However, there are some rules that need to be followed.
In python, variable names can contain only the following characters:
- the uppercase and lowercase letters A to Z,
- the underscore _
- a variable name can also contain the numerical characters 0 to 9, except that they cannot be the first character in a variable name
Here are some examples of valid variable names:
x1, x2, x3, _x, xYZ, x_y_z
And here are some examples of invalid variable names:
1x and xyz!
1x is invalid because variable names are not allowed to start with a digit. They can contain digits as long as it is not the first character. xyz! is invalid because exclamation points are not allowed in variable names. All special characters, except for the underscore, are not allowed in Python. Take note that in other programming languages, other special characters are also allowed. In Java for example, the dollar sign is allowed.
Variable names are also case sensitive. If you have a variable name that is a lower case x and another one that is an uppercase X, then those two are separate entities. The python interpreter will know that these are 2 separate and distinct variables. It will not be confused by them. Although it might confuse anyone else reading your code, including yourself, so it might not be such a good idea to have these 2 variables in the same program.
#Python #PythonTutorial #LearnToCode
Комментарии