程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python variable declaration (2)

編輯:Python

1. Multivariable assignment , Use underlined placeholders

a, b, _ =1, 2, True

print(a, b)

Output :1 2

2. Multivariable assignment

a = b = c =2

print(a, b, c)

b =345

print(a, b, c)

Output :

2 2 2

2 345 2

As expected , Change variables b Value , Only variables b The value of has changed .

The test list

x = y = [7, 8, 9]

print(x)

print(y)

x = [13, 88, 99]

print(x)

print(y)

Output :

[7, 8, 9]

[7, 8, 9]

[13, 88, 99]

[7, 8, 9]

As expected , Change list x The element value of , Only list x The value of has changed .

Change the value of an element in the column :

x = y = [7, 8, 9]

x[0] =789

print(x)

print(y)

Output :

[789, 8, 9]

[789, 8, 9]

Change list x The value of the first element , Class table y The first element is also modified .

Nested list

x = [1, 2, [3, 4, 5], 6, 7]

print(x)

print(x[2])

print(x[2][1])

Output :

[1, 2, [3, 4, 5], 6, 7]

[3, 4, 5]

4

3. The same variable can be assigned different types of values

a =2

print(a)

print(type(a))

a =‘New value’

print(a)

print(type(a))

Output :

2

<class ‘int’>

New value

<class ‘str’>

4. Calculation of Boolean variables

print(True +False ==1)        # 1 + 0 == 1

print(True *True ==1)        # 1 * 1 == 1

Output :

True

True


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved