table of Contents
python based learning (start)
Let us follow the python master learning magic, the first day of learning python master to teach the following concepts:
variable
What is a variable?
Variable name suggests, is the amount of change. Things in the world can be seen as an attribute variables, such as height, weight.
The purpose of the reference variable?
Since all states have the attributes or values, then there should be a label to describe and store it for next time use a direct call.
Representation at variable python
name = "猪猪侠" age = 8 爱好 = ["打抱不平","吃零食"]
*** NOTE avoid it Keywords:
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
Variable naming
Hump body
AgeOfGGBond = 8
Underline (recommended)
age_of_GGBond = 8
constant
Since there are variables, then it is a necessity to have a representation of the constant, constant or variable nature in python, but the default constants in all capital letters, such as:
NAME = "GG Bond"
pyhton not seem like a statement like JAVA a finally int Age = 8 .
Variable memory management
Python variables created simultaneously, the computer will automatically assign a memory address for storing variable data, it is worth mentioning that, if a not b alias (i.e., the absence of a = b , directly or indirectly, this assignment statement), then theoretically "iS a b" (even if a == B ) should return False , meaning that they point to the memory address is not the same. But the following values above theory does not apply:
Integer [-5,256] is: python order to optimize speed to avoid an integer frequent application and release memory space, set up a small integer pool [-5,256], integer in this range are pre-built and will not be recycled garbage collection mechanism. (Similarly, a single letter is true)
bool: Boolean
str :
1 or less in length, the direct buffer
If the string length is greater than 1. The only numbers, letters, underline, is cached
Sys module using the intern () cache string mechanism is as follows:
intern mechanism is to let him just take up memory space occupied by a "helloooooooooooooooooo". To maintain the reference count on when to release.
python garbage collection
We mentioned above garbage collection python, then he is in the end what kind of it? Simple explanation is to rely on reference counting to judge this data is not active object, if the object reference count is zero , then the system will immediately release the object. (In most cases is so, other cases will be used to clear another algorithm, we may discuss later)
Other variables assignment mode
In addition to the traditional variables a = 1 this assignment the way, there are several common assignment:
- Chain assignment
a = b = c = d = 10
Cross assignment
x = 1 y = 2 x,y = y,x # x=2 y=1
Today small problem programming
Demand: Given a standard age input by the user is equal to the standard Age Age Age determination, if it is equal - print guessed; is less than - guess the small printed; if more than - large print guess
age=22
condition=1
while condition:
guess=input("你猜小明今年多大了?")
if guess.isdigit(): #判断输入的是否是数字
gs=int(guess)
if gs==age:
print("\033[1;31;m恭喜你猜对咯!\033[0m") #红色高亮字体提醒
condition=0
elif gs < age:
print("\033[1;34;m猜小了!\033[0m") # 蓝色高亮字体提醒
elif gs> age :
print("\033[1;36;m猜大了!\033[0m") #青蓝色高亮字体提醒
else:
print("\033[1;31;46m请输入正确的数字哦!\033[0m") #青蓝背景+红色文字
** on changing the font color code, I will list in detail in another essay