How to define a variable in python

silencement
Release: 2019-06-21 14:24:07
Original
14102 people have browsed it

How to define a variable in python

Variables in Python do not need to be declared. Each variable must be assigned a value before use. The variable will not be created until the variable is assigned a value.

In Python, a variable is a variable, it has no type. What we call "type" is the type of the object in memory pointed by the variable.

The equal sign (=) is used to assign values ​​to variables.

The left side of the equal sign (=) operator is a variable name, and the right side of the equal sign (=) operator is the value stored in the variable. For example:

counter = 100          # 整型变量
miles   = 1000.0       # 浮点型变量
name    = "runoob"     # 字符串
 
print (counter)
print (miles)
print (name)
Copy after login

Executing the above program will result in output

100
1000.0
runoob
Copy after login

Assigning multiple variables

Python allows you to assign values ​​to multiple variables at the same time. For example:

a = b = c = 1
Copy after login

The above example creates an integer object with a value of 1, assigns values ​​from back to front, and the three variables are assigned the same value.

You can also specify multiple variables for multiple objects. For example:

a, b, c = 1, 2, "runoob"
Copy after login

The above is the detailed content of How to define a variable in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!