Python 是一種動態類型語言,這意味着我們在使用它之前不需要提到變量類型或聲明。它使 Python 成為最高效、最易於使用的語言。在 Python 中,每個變量都被視為一個對象。
在聲明變量之前,我們必須遵循給定的規則。
- 變量的第一個字符可以是字母或(
_
)下劃線。 - 變量名中不應使用特殊字符(@、#、%、^、&、*)。
- 變量名區分大小寫。例如——年齡和年齡是兩個不同的變量。
- 保留字不能聲明為變量。
讓我們理解幾個基本變量的聲明。
民數記
Python 支持三種類型的數字——整數、浮點數和複數。我們可以聲明任意長度的變量,沒有限制聲明任意長度的變量。使用以下語法聲明數字類型變量。
示例-
num = 25
print("The type of a", type(num))
print(num)
float_num = 12.50
print("The type of b", type(float_num))
print(float_num)
c = 2 + 5j
print("The type of c", type(c))
print("c is a complex number", isinstance(1 + 3j, complex))
輸出:
The type of a <class 'int'>
25
The type of b <class 'float'>
12.5
The type of c <class 'complex'>
c is a complex number True
用線串
該字符串是 Unicode 字符的序列。它使用單引號、雙引號或三引號來聲明。讓我們理解下面的例子。
示例-
str_var = 'JavaTpoint'
print(str_var)
print(type(str_var))
str_var1 = "JavaTpoint"
print(str_var1)
print(type(str_var1))
str_var3 = '''This is string
using the triple
Quotes'''
print(str_var3)
print(type(str_var1))
輸出:
JavaTpoint
<class 'str'>
JavaTpoint
<class 'str'>
This is string
using the triple
Quotes
<class 'str'>
多重分配
1。給多個變量賦值
我們可以在同一行同時分配多個變量。例如-
a, b = 5, 4
print(a,b)
輸出:
5 4
值按給定的順序打印。
2。給多個變量賦值
我們可以將單個值賦給同一行的多個變量。考慮下面的例子。
示例-
a=b=c="JavaTpoint"
print(a)
print(b)
print(c)
輸出:
JavaTpoint
JavaTpoint
JavaTpoint
原創文章,作者:W5JC8,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/127821.html