Declare variables in python | Laptrinhcanban.com

HOME › >>

Declare variables in python

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 . The value 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
str = "Hello" # Variable value is string
colorlist = ["red", "blue", "yerllow"] # Variable value is list

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
print(num)

#>> 10

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
print(type(myvar))

# <class 'int'>

mystr = "hello"
print(type(mystr))

# <class 'str'>

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

https://laptrinhcanban.com/en/python/nhap-mon-lap-trinh-python/bien-trong-python/khai-bao-bien-trong-python/

Thank you for reading and please Like & Share for other friends to learn.
">

HOME  › >>

  • Recent Posts
Profile
きよしです!笑

Author: Kiyoshi (Chis Thanh)

Kiyoshi was a former international student in Japan. After graduating from Toyama University in 2017, Kiyoshi is currently working at BrSE in Tokyo, Japan. Kiyoshi là một cựu du học sinh tại Nhật Bản. Sau khi tốt nghiệp đại học Toyama năm 2017, Kiyoshi hiện đang làm BrSE tại Tokyo, Nhật Bản.