python data type mind map
edit
#int-整数
num1 = 1
# float-浮点数
num2 = 1.1
# str-字符串 #字符串是不可变的数据类型
num3 = '1'
# set-集合
num4 = {'a',1}
# list-列表
num5 = ['a',1]
# bool-布尔型
num6 = True
# tuple-元组
num7 = (10,20)
# dict-字典
num8 = {'name':'gg'}
formatted output format
age = 18
name = 'Tom'
weight = 75.5
stu_id = 1
stu_id2 = 1000
print("今年我的年龄是%d岁" % age)
print("我的名字是%s" % name)
print("我的体重是%.2f公斤" % weight)
print("我的学号是%d" % stu_id)
print("我的学号是%03d" % stu_id)
print("我的学号是%03d" % stu_id2)
print("我的名字是%s, 今年%d岁,明年是%d岁" % (name ,age,age+1) )
print("我的名字是%s, 今年%d岁,明年是%d岁,体重%.2f公斤,学号是%d" % (name ,age,age+1,weight,stu_id))
Note: In addition to %s, the format string can also be written as f'{expression}' - efficient, new formatting method in python3.6
age = 18
name = 'Tom'
print(f"我的名字是{name}, 今年{age}岁" )
python默认自带换行符end="\n"——n可以修改其他比如t
print('hello',end="\n")
Type conversion function:
num = input("请输入数字:")
print(num)
# 默认是字符串
print(type(num)) # str
# 转换成int——int(srt)
print(type(int(num))) #int
print(type(float(num))) #float
print(type(tuple(num))) #tuple
# eval()——转换成原本的数据类型
num = '12' #现在是字符串
print(type(eval(num))) #现在是变回了原来的样子——int数字
arithmetic operators
Assignment method
multiple variable assignments
num1,float1,str1=10,0.5,'hello,world'
print(num1)
print(float1)
print(str1)
assigning the same value to multiple variables
a=b=c=10
print(a)
print(b)
compound assignment operator
a = 10
a += 1
a -= 2
print(a)
c = 10
c += 1 + 2
print(c)
注意先算=右边的加法
d = 10
d *= 1 + 2
print(d) #30
comparison operator
Logical Operators
a = 0
b = 1
c = 2
print(a < b and c < b)
print(not False)
Ternary operator
条件成立执行的表达式 if 条件 else 条件不成立执行的表达式
# a = 1
# b = 2
#
# c = a if a > b else b
#
# print(c)
aa = 10
bb = 16
cc = aa - bb if aa > bb else bb -aa
print(cc)
random module
import 模块名
import random
num = random.randint(0,2)
print(num)
loop statement
while syntax
i = 1
while i <= 100:
print("我错了")
i += 1
while loop application case
even accumulation
# 准备数据
i = 1
# 结果变量
result = 0
# 循环
while i <= 100:
if i % 2 == 0:
result += i
i += 1
result)
print rectangle
j = 1
while j <= 5:
# 第一行
i = 1
while i <= 5:
print("*", end='') # end=''取消换行符
i += 1
# 一行星星是结束:借助空的print换行显示下一行
print()
j += 1
print triangle
j = 1
while j <= 5:
# 第一行星星打印
i = 1
# i表示每行里面星星的个位数,这个数字要和行号相等,
# 所以i一定要和j联动
while i <= j:
print("*", end='') # end=''取消换行符
i += 1
# 一行星星是结束:借助空的print换行显示下一行
print()
j += 1
Ninety-nine multiplication table
j = 1
while j <= 9:
# 第一行表达式
i = 1
# i表示每行里面星星的个位数,这个数字要和行号相等,
# 所以i要和j联动
while i <= j:
print(f"{i} * {j} = {i*j}", end='\t') # end=''取消换行符
i += 1
# 一行星星是结束:借助空的print换行显示下一行
print()
j += 1
for loop syntax