Variables and types
Variables: Stores data in a program's run, with 3 elements: variable name, variable type, variable value. Python is a weakly typed language and does not need to declare variable types.
[ email protected] python]# ipython3in [1]: a=1 //variable name = variable value; A value of 1 is stored in a zone in the heap memory, memory is divided into heap memory and stack memory, and the stack memory is a reference. Points to the value in heap memory. In [4]: b= theIn [5]: c=a+BIn [7]: cout[7]: theIn [8]: aout[8]:1In [9]: Type (a) out[9]:intIn [Ten]: Type (c) out[Ten]:intIn [ One]: str="Hello"In [ A]: strout[ A]:'Hello'In [ -]: x,y=1,2In [ -]: xout[ -]:1In [ the]: yout[ the]:2In [ -]:in [ -]: Type (str) out[ -]: Str
Variable type
identifiers : Custom symbols and names, identifiers that are defined by themselves, such as variable names, function names, and so on.
Rule: Consists of letters, underscores, and numbers, and cannot start with a number, and cannot contain symbols that have special meanings, such as #.&! (), and case-sensitive.
Casing rules:
Hump naming method: such as small hump: Username/userloginflag Big Hump: UserName; class names are generally capitalized.
underline rule : User_name User_login_flag
reserved words/ keywords: words that have special meanings in the computer.
In [1]:Import keywordin [2]: keyword.kwlistout[2]: ['False', 'None', 'True', ' and', ' as', 'assert', ' Break', 'class', 'Continue', 'def', 'del', 'elif', 'Else', 'except', 'finally', ' for', ' from', 'Global', 'if', 'Import', 'inch', ' is', 'Lambda', 'nonlocal', ' not', 'or', 'Pass', 'Raise', 'return', 'Try', ' while', ' with', 'yield']in [3]:
Output and output formats
Normal output, print ()
In [4]:Print('Hello',' World','Zyj','Hello') //output Multiple variables Hello world zyj helloin [5]:Print(111+222)//Output Expression333In [6]: Exit[[email protected] python]#Vim 3.py1 a=35 2Print("my age is:%d"%a)//variable substitution [[email protected] python]#Python3 3.pyMy age is: 35[[email protected] python]#Vim 3.py1 a=35 2 b=10 3Print("my age is:%d"%a+b) [[email protected] python]#Python3 3.pyTraceback (most recent): File"3.py", Line 3,inch<module>Print("my age is:%d"%A+B)//Error line typeerror:must be str, notINT//error type [[email protected] python]#Vim 3.py1 a=35 2 b=10 3Print("my age is:%d"% (A+B))//expressions need to be enclosed in parentheses [[email protected] python]#Python3 3.pyMy age is: 45[[email protected] python]#Vim 3.py1 a=35 2 b=10 3 my_name="Zyj"4Print("my age is:%d,my name is:%s"% (A+b,my_name))//If more than one variable substitution is required, and each variable is separated by commas,%d represents an integer type substitution, and%s represents all type variable substitution, you can only remember the%s[[email protected] python]#Python3 3.pyMy age is: 45,my Name is: Zyj
[[email protected] python]#Vim 3.pymoney=2Print("I have money:%4d",%money)//output is 4 bits, default bit space [[email protected] python]#Python3 3.pyI have Money:2[[email protected] python]#Vim 3.pymoney=2Print("I have money:%04d"%money)//not enough 4 bits in front with 0 padding [[email protected] python]#Python3 3.pyI have money:0002
Python data type