- Mastering Python Scripting for System Administrators
- Ganesh Sanjiv Naik
- 279字
- 2021-07-02 14:00:19
Variables
Like other programming languages, there's no need to declare your variables first. In Python, just think of any name to give your variable and assign it a value. You can use that variable in your program. So, in Python, you can declare variables whenever you need them.
In Python, the value of a variable may change during the program execution, as well as the type. In the following line of code, we assign the value 100 to a variable:
n = 100
Here are assigning 100 to the variable n. Now, we are going to increase the value of n by 1:
>>> n = n + 1
>>> print(n)
101
>>>
The following is an example of a type of variable that can change during execution:
a = 50 # data type is implicitly set to integer
a = 50 + 9.50 # data type is changed to float
a = "Seventy" # and now it will be a string
Python takes care of the representation for the different data types; that is, each type of value gets stored in different memory locations. A variable will be a name to which we're going to assign a value:
>>> msg = 'And now for something completely different'
>>> a = 20
>>> pi = 3.1415926535897932
This example makes three assignments. The first assignment is a string assignment to the variable named msg. The second assignment is an integer assignment to the variable named a and the last assignment is a pi value assignment.
The type of a variable is the type of the value it refers to. Look at the following code:
>>> type(msg)
<type 'str'>
>>> type(a)
<type 'int'>
>>> type(pi)
<type 'float'>
- 軟件安全技術(shù)
- OpenShift開(kāi)發(fā)指南(原書(shū)第2版)
- Hands-On Image Processing with Python
- 深度學(xué)習(xí):算法入門(mén)與Keras編程實(shí)踐
- Functional Kotlin
- R Deep Learning Cookbook
- Spring+Spring MVC+MyBatis整合開(kāi)發(fā)實(shí)戰(zhàn)
- 從零開(kāi)始學(xué)Linux編程
- Illustrator CS6設(shè)計(jì)與應(yīng)用任務(wù)教程
- Emotional Intelligence for IT Professionals
- 計(jì)算語(yǔ)言學(xué)導(dǎo)論
- 你真的會(huì)寫(xiě)代碼嗎
- Solr權(quán)威指南(下卷)
- INSTANT Lift Web Applications How-to
- Oracle Database 12c DBA官方手冊(cè)(第8版)