官术网_书友最值得收藏!

Creating and assigning values to variables

In Python, variables don't need to be declared explicitly to reserve memory space. So, the declaration is done automatically whenever you assign a value to the variable. In Python, the equal sign = is used to assign values to variables.

Consider the following example:

#!/usr/bin/python3
name = 'John'
age = 25
address = 'USA'
percentage = 85.5
print(name)
print(age)
print(address)
print(percentage)

Output:
John
25
USA
85.5

In the preceding example, we assigned John to the name variable, 25 to the age variable, USA to the address variable, and 85.5 to the percentage variable.

We don't have to declare them first as we do in other languages. So, looking at the value interpreter will get the type of that variable. In the preceding example, name and address are strings, age is an integer, and percentage is a floating type.

Multiple assignments for the same value can be done as follows:

x = y = z = 1

In the preceding example, we created three variables and assigned an integer value 1 to them, and all of these three variables will be assigned to the same memory location.

In Python, we can assign multiple values to multiple variables in a single line:

x, y, z = 10, 'John', 80

Here, we declared one string variable, y, and assigned the value John to it and two integer variables, x and z, and assigned values 10 and 80 to them, respectively.

主站蜘蛛池模板: 南雄市| 自贡市| 怀柔区| 渑池县| 怀集县| 镇巴县| 宣恩县| 夹江县| 灵川县| 交城县| 靖边县| 广平县| 民乐县| 江阴市| 宜兴市| 沂水县| 个旧市| 额尔古纳市| 浦北县| 霍林郭勒市| 嵩明县| 汽车| 吕梁市| 斗六市| 竹山县| 吴桥县| 灵山县| 江孜县| 成安县| 建湖县| 青河县| 墨江| 临清市| 江阴市| 鲁甸县| 临泉县| 永济市| 旌德县| 石门县| 盐城市| 黄梅县|