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

Python base variable declaration

編輯:Python

1. Declare a legal variable name

1. Only letters , Numbers , Underline composition

2. Cannot start with a number

3. Case sensitive

Legal variable name :

x =True

_y =False

a =“test”

a_1 =“OK”

a_a_1=“Also OK”

Illegal variable name :

9a=1  # SyntaxError: invalid syntax

Case sensitive , therefore x,X It's a different variable

x=1

y = X +2

2. keyword

import keyword

print(keyword.kwlist)

Output :

[‘False’, ‘None’, ‘True’, ‘__peg_parser__’, ‘and’, ‘as’, ‘assert’, ‘async’, ‘await’, ‘break’, ‘class’, ‘continue’, ‘def’, ‘del’, ‘elif’, ‘else’, ‘except’, ‘finally’, ‘for’, ‘from’, ‘global’, ‘if’, ‘import’, ‘in’, ‘is’, ‘lambda’, ‘nonlocal’, ‘not’, ‘or’, ‘pass’, ‘raise’, ‘return’, ‘try’, ‘while’, ‘with’, ‘yield’]

None of these keys can be used as variable names

3. data type

Use type( ) function

Integers

a =3

print(a)

print(type(a))

b =123456789087654321

print(b)

print(type(b))

Floating point numbers

pi =3.1415

print(pi)

print(type(pi))

character string

s1 =‘a’

print(s1)

print(type(s1))

s2 =‘Ritchie Lee’

print(s2)

print(type(s2))

Boolean type

b =True

print(b)

print(type(b))

Null type

x =None

print(x)

print(type(x))

Output :

3

<class ‘int’>

123456789087654321

<class ‘int’>

3.1415

<class ‘float’>

a

<class ‘str’>

Ritchie Lee

<class ‘str’>

True

<class ‘bool’>

None

<class ‘NoneType’>

4. Multivariable assignment

a, b, c =1, 2, True

print(a, b, c)

Output :

1 2 True

If the variable has more than one assigned value , It will be abnormal

a, b, c =1, 2,

print(a, b, c)

Is abnormal :

a, b, c = 1, 2,

ValueError: not enough values to unpack (expected 3, got 2)

If the assigned number is more than the variable :

a, b =1, 2, True

print(a, b)

Is abnormal :

a, b = 1, 2, True

ValueError: too many values to unpack (expected 2)


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