- Variable
- Number Type
- Operator
- Process Control
- While loop
- Dead loop
- Continue vs Break
- While else
Variable (varibles)
The role of variables:
- Variables are used to store information so that subsequent code calls
- Tag Description Data
declaring variables
name = "Jason"
- Name: variable name
"Jason": Variable Value
Definition Specification for variables
- A variable can only be a combination of letters, numbers, or underscores
- The first character of a variable name cannot be a number
Variable assignments cannot have spaces such as:
name age = 22
The following keywords cannot be declared as variable names
["and", "as", "assert", "break", "class", "continue", "del", "elif", "else", "except", "exec", "finally", "del", "for", "from", "gobal", "if", "import", "in", "is", "lambda", "not", "or", "pass", "print", "rasie", "return", "try", "while", "with", "yeild"]
Variable naming habits
Constant
Constant, all uppercase letters.
AGE_OF_OLDBOY = 56
Number Type
Python's long integers do not refer to the positioning width, i.e. python does not limit the size of long integer values, but the actual number of long integers we use cannot be infinite due to limited machine memory.
Note >> from python2.2, if an integer overflow occurs, Python automatically converts the integer data to grow integers.
string Definition: quoted is the string
"", "," "" "" ", are all strings
Single and double quotes are no different
string concatenation
name = "jack"age = "22"print(name + age) # 字符串相加# jack22print(name * 2) # 字符串相乘# jackjack
There are only two types of Boolean types True or False and cannot exist simultaneously
Formatted output
name = "Jason"age = 23job = "it"salary = "10k"info = """"------info of {_name}-----Name={_name}Age={_age}Job={_job}Salary={_salary}---------end-------""".format(_name=name, _age=age, _job=job, _salary=salary)print(info)info2 = """------info of %s-----Name=%sAge=%dJob=%sSalary=%s---------end-------""" % (name, name, age, job, salary)print(info2)info3 = """------info of {0}-----Name={0}Age={1}Job={2}Salary={3} ---------end-------""".format(name, age, job, salary)print(info3)
+ 两个数相加- *% 取模,例如9%4等于1 ** 例如 2**3等于8// 例如9//4等于2比较运算 == 等于!= 不等于<> 不等于(python2,python3没有)><>=<=
Assignment operations
==+= c+=a等效于c=c+a-= c-=a等效于c=c-a*= c*=a等效于c=c*a%= c%=a等效于c=c%a//= c//=a等效于c=c//a**= c**=a等效于c=c**a
Logical operations
and 布尔“与” ,x和y同时为真即x and y为真,否则为假or 布尔“或”,x和y只要有一个为真即 x or y 为真 ,X和y同时为假则x or y为假not 布尔“非”,x为真则返回为假,x为假则返回为真
Member operations
in 如果在指定序列中找到值返回ture,否则返回Fasle.in not 如果在指定的序列中没有找到值就返回ture,否则返回false
Identity operations
is is是判断两个标识符是不是引用自一个对象is not is not 是判断两个标识符是不是引用自不同对象
Bit arithmetic
Move the bits of a to the left 2 bits, and 0 to the right. If the left-hand side does not contain 1, then each left-shift is equal to the number multiplied by 2.
a=0011 0011 #十进制等于51a==a<<2a==1100 1100 #十进制等于204
-
A = a >> 2 shifts the bits of a to the right 2 bits
a=1100 1100 #十进制为204a==a>>2a=00110011 #十进制为51
Ternary operations
a,b,c=1,3,5d=a if a>b else cprint(d)# 5
Process Control
Single Branch
If condition:
Code to execute when the condition is met
a = 1if a < 2: print(“lalala”)
x = 2if x == 1: print("i am 1")elif x == 2: print("i am 2")elif x==3: print("i am 3")else: print("unknow")
While loop
While condition:
Execute code ...
count = 0while count <= 100: print("loop", count) count += 1
Dead Loop
Dead loop
count = 0while True: print("forever love") coungt += 1
Break & Continue
- The break:break is used to completely end a loop, jumping out of the loop after the loop executes the statement
count = 0while count <= 100: print("loop", count) if count == 5: break count += 1print("---out of while loop---")
- Continue:continue and break are a bit similar, the difference is that continue just terminates the loop, and then executes the subsequent loop, and break terminates the loop completely.
```
Count = 0
While Count <= 100:
Count + = 1
If Count > 5 and Count < 95:
Continue
Print ("Loop", count)
Print ("---out of a while loop---")
### while else当while循环正常执行完,中间没有被break终止的话,就会执行else后面的语句。
Count = 0
While Count <= 5:
Count + = 1
Print ("Loop", Count)
Else
Print ("Loop normal execution")
Print ("---out of a while loop---")
```
Python basic syntax