Posted at: https://laptrinhcanban.com/en
To use variables in python, we need to declare the variable first. You will learn how to declare variables in python as well as how to declaring variable types in pythonafter this lesson.
Declare variables in python
When declaring variables in python , we do not need to initialize the variable nor specify the data type of the variable . Python will automatically determine the data type of the variable being declared.
We just need to declare the variable in python by assigning the variable in python with the following syntax:
Syntax of Declare variables in python
variable
= value
Trong đó
variable
is set according to the Python Variable Naming Rule that you learned in the article What is Variable in Python .- The mark
=
used to assign variables in python . Thevalue
in the right of the mark=
will be assigned to the variable on the left. value
used to declare variables can be strings, numbers, or other types of objects in python.
Example of variable declaration in python
For example:
old = 24 # Variable value is number |
Using variables in python
After declaring the variable in python, you can use it by writing the variable name in the program. When using a variable in python, the value assigned to that variable will be used instead of the variable when processed in python.
num = 30 |
Like in the above example, after declaring a variable by assigning a value 30
to a variable num
, when using a variable num in the function print, the assigned value 30 will be used instead of the variable when printed on the screen.
Declaring variable types in python
When you declare a variable in python, Python will automatically determine the data type of the variable to be declared. So you do not need to worry about whether the data type you have declared in the variable is correct or not.
If you want to test the data type after declaring a variable in python, use the function typle() in python like the following example:
myvar = 1234 |
Summary
Above, Kiyoshi showed you how to declare a variable in python. To better understand the lesson content, practice rewriting today’s examples.
And let’s learn more about Python in the next lessons.
URL Link
HOME › python for beginners>>variables in python