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
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
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’>
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)