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

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.

主站蜘蛛池模板: 齐齐哈尔市| 临猗县| 乳山市| 大足县| 新丰县| 乌拉特中旗| 盘锦市| 永登县| 丹阳市| 合作市| 泰安市| 商南县| 徐水县| 宁乡县| 房产| 霍林郭勒市| 和平县| 聂拉木县| 佛教| 静海县| 崇明县| 招远市| 嘉禾县| 社会| 乐都县| 康定县| 岳阳县| 普宁市| 青阳县| 彰武县| 广元市| 上饶市| 清涧县| 井冈山市| 乐亭县| 富宁县| 湖南省| 招远市| 连江县| 宁陕县| 五原县|