I. INPUT and PRINT
Input (): Receive user input print (): Output
1. print("aaa","bbb") 2. name="张三" age=18 print("我叫"+name+"今年"+str(age)+"岁") 3. 不换行输出 print("中国",end="") print("北京") print(r"中国\n北京") 输出换行 print("中国\n北京") 4. 格式化输出 name="张三" age= 12 herght=1.999 print("我叫%s,今年%d,身高%.2f"%(name,age,herght)) #%s字符型;%d int型;%。2f 输出2位小数 5. 格式化输出如果遇到了显示%,要用两个%%来表示原样输出 n = 99.99 print("您战胜了%s%%全国的用户"%n)
Second, the Format function
- functions that format the output string to quickly process various strings
UserName = "Shi"
PassWord = "123"
Print ("username: {}, Password: {}". Format (UserName, PassWord)
Print ("user name: {0}, Password: {1}". Format (Username,password))
- Multiple locations output a variable
print (username: {0}, password: {1 }{1} ". Format (Username,password))
- Define a variable in the Format function
Print (" username: {0}, Password: {1}, Captcha: {code} ". Format ( username,password,code=4211))
Pyint ("{}={}". Format ("Python", "123.1111")
Print ("{1}={0}". Format ("Python" , "123.1111"))
Print ("{1}={0}". Format ("2")
-
Reserved is 10 bytes
print ("{0:a>10}={2:a ^10}={1:A<10} ". Format (" Shi "," Yi "," Sheng ")) Comments:: The edge is the key value, a fill empty;> right alignment, ^ center,< left-aligned, 10 is the number of reserved bytes of different types, showing the results of the difference
-
Floating-point display
Print ("{: f},{:.2f},{}". Format (3.141592654,3.141592654,3.141592654))
Note: F retains 6 decimal places by default. 2f retains 2 decimal places {} output
-
Format binary conversion
Print ("{: X},{:o},{:b}". Format (230,23 0,230))
Note:
{: x} decimal to hexadecimal
{: o} decimal to octal
{: b} decimal to binary
Third, the data type:
Data type: numeric type: int, float Boolean: true, False null object: None sequence: list, tuple (tuple), Dict (dictionary), str, range ()
type int, str type, float type
Print (Type (18))
Print (Type ("18"))
Print (Type (1.88))
Boolean nature (True = = 1 False = = 0)
Print (BOOL) #输出True
Print (bool ( -1)) #输出True
Print (BOOL ("123")) #输出True
Print (BOOL ("")) #输出False
Print (bool ([])) #输出False
注释: 0, 0.0 ,None , "", [] , () 这些都会被当做False处理
Built-in functions: Rounding round ()
Print (Round (3.6))
Print (Round (3.1))
Forcing type conversions
num = "123"
int = int (num)
print (int)
Print (type (int))
Eval () automatically judges and converts strings to the appropriate type
e1 = eval ("1.23")
E2 = eval ("11")
Print (E1)
Print (E2)
Print (Type (e1))
Print (Type (e2))
Binary conversion
# binary 0b
# octal 0o
# hex Ox
Print (0b1101)
Print (0o177)
Print (0X9FF)
# bin() 将十进制转换成二进制 # hex() 将十进制转换成十六进制 # oct() 将十进制转换成八进制 print(bin(12)) print(hex(12)) print(oct(12)) **ascii ** **chr() 接收的是0-255,返回的是对应的ascii值的字符** print(ord("a")) print(chr(97))
Iv. Module Math
import math #先调用模块 print(math.floor(5.66)) print(math.ceil(5.66)) print(math.trunc(-1.66)) 注释: math.floor() 向下取整(坐标轴左边取值) math.ceil() 向上取整(坐标轴右边取值) math.trunc() 截断 (往0的方向取整)
V. Decimal module
运算结果出现问题,我们需要处理浮点类型 import decimal #先调用模块 print (0.1+0.1+0.1-0.3) n=decimal.Decimal("0.1")+decimal.Decimal("0.1")+decimal.Decimal("0.1")-decimal.Decimal("0.3") print (n) from decimal import Decimal n1 = Decimal("0.1")+Decimal("0.3") print(n1)
logical operators
Print (1==2 and 2==2) # returns True if both expressions are formed
Print (1==2 or 2==1) # Both sides of the expression will return true if one is set
Print (not 1==2) # not negate
Pi
Print (Math.PI)
Recurses
Pow () 2 of the 3-time party
Print (POW (2,2))
Python Input and print