Variables and data types

Definition and use of variables

Python is a dynamic language, declaring variables without types; Python does not use curly braces to indicate logical progression; instead, it uses indentation;

Print (id(name) print(id(name)) Print (' type ', type(name)) <class 'STR '> print(' value ', name) #Copy the code

Common Python data types

Integer type int

n1 = 90
n2 = -76
n3 = 0
print(n1, type(n1))
print(n2, type(n2))
print(n3, type(n3))
Copy the code

Float float

A = 3.14 print(a, type(a))Copy the code

Boolean type bool

F1 = True f2 = False print(f1, type(f1)) print(f2, type(f2)) True=1,False=0 print(f1 + 1) # 2 print(f2 + 1) # 1Copy the code

String type STR

# str1 = 'Life is short, I use Python' str2 = "life is short, I use Python" # str3 =" Life is short, I use Python""Copy the code

Data type conversion

Print (type(name) + name + age) # TypeError: print(type(name) + age) # TypeError: Can only concatenate STR (not "int") to STR (' I am '+ name +' age '+ STR (age)) # STR ( print(type(a), type(b), type(c)) print(type(str(a)), type(str(b)), Type (STR (c)) print(STR (a) + STR (b) + STR (c)) # int() s1 = '123' s2 = '3.14' s3 = 3.14 Type (s2), type(s3), type(s4)) print(int(s1)) Print (int(s4)) # print(int(s4)) # print(int(s4)) S3 = 3 s4 = False print(type(s1), type(s2), type(s3), Type (s4)) print(float(s1)) # 123.0 print(float(s2)) # 3.14 print(float(s3)) # 3.0 print(float(S4)) # 0.0Copy the code

annotation

Python uses # as a single-line comment; Use triple quotation marks for multi-line comments

Print (' I/O ') """ "I/O """ I/o "" I/o ""Copy the code

Reserved words/keywords in Python

Import keyword print(keyword. Kwlist) # Reserve the words ['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']Copy the code