- Mastering Python Scripting for System Administrators
- Ganesh Sanjiv Naik
- 248字
- 2021-07-02 14:00:19
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.
- 微服務(wù)與事件驅(qū)動(dòng)架構(gòu)
- Software Testing using Visual Studio 2012
- Python高級(jí)編程
- ASP.NET程序設(shè)計(jì)教程
- Oracle從入門到精通(第5版)
- Unity 2018 Shaders and Effects Cookbook
- Visual C#.NET Web應(yīng)用程序設(shè)計(jì)
- Principles of Strategic Data Science
- 微課學(xué)人工智能Python編程
- QlikView Unlocked
- Learning Kotlin by building Android Applications
- 交互設(shè)計(jì)師成長手冊(cè):從零開始學(xué)交互
- Developing Java Applications with Spring and Spring Boot
- 輕松學(xué)Scratch 3.0 少兒編程(全彩)
- HTML5程序開發(fā)范例寶典