地址: 练习 13 参数,解包,变量 - 《笨办法学Python3(Learn Python3 The Hard Way 中文版)》 - 书栈网 · BookStack
1.0 变量和赋值
a1 = 123 # Int
a2Float = 12.30 #Float
b1_str = 'abc' #string
c1 = f"There are {a1} types of people." # {} 是变量, " "中输出参数值需要加f, 即format 格式化后放入
#多行字符串用""" """或''' '''
fat_cat = """
I'll do a list:
\t* Cat food
\t* Fishies
\t* Catnip\n\t* Grass
"""
附、常用格式:
| Code | C 类型 | Python 类型 | 最小字节 |
|---|---|---|---|
| 'b' | signed char | int | 1 |
| 'B' | unsigned char | int | 1 |
| 'u' | Py_UNICODE | Unicode | 2 |
| 'h' | signed short | int | 2 |
| 'H' | unsigned short | int | 2 |
| 'i' | signed int | int | 2 |
| 'I' | unsigned int | int | 2 |
| 'l' | signed long | int | 4 |
| 'L' | unsigned long | int | 4 |
| 'f' | float | float | 4 |
| 'd' | double | float | 8 |
1.1 输入 & 输出
print(a1)
print("a1=", a1)
hilarious = False
joke_evaluation = "Isn't that joke so funny?! {}"
print(joke_evaluation.format(hilarious)) # .format 格式化后放入
print("How old are you?", end=' ') # 'end=' 不另起一行
age =input()
print(f"So, you're {age} old")
1.2 数组和遍历
a3 = [1, 3.5, "Hello"]
import array as arr
a = arr.array('d', [1.1, 3.5, 4.5]) # array模块需要同类型