python data type mind map

pictureedit


#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

picture

picture

age = 18name = 'Tom'weight = 75.5stu_id = 1stu_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 = 18name = 'Tom'

print(f"我的名字是{name}, 今年{age}岁" )

python默认自带换行符end="\n"——n可以修改其他比如tprint('hello',end="\n")

Type conversion function:

picture

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

picture

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=10print(a)print(b)

compound assignment operator

picture

a = 10a += 1a -= 2print(a)

c = 10c += 1 + 2print(c)

注意先算=右边的加法d = 10d *= 1 + 2print(d) #30

comparison operator

picture

Logical Operators

picture

a = 0b = 1c = 2
print(a < b and c < b)
print(not False)

picture

Ternary operator

  

  条件成立执行的表达式 if 条件 else 条件不成立执行的表达式# a = 1# b = 2## c = a if a > b else b## print(c)

aa = 10bb = 16cc = 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

picture

i = 1while 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

print("result的结果为:", result)

print rectangle

j = 1while j <= 5:# 第一行i = 1while i <= 5:print("*", end='') # end=''取消换行符i += 1# 一行星星是结束:借助空的print换行显示下一行print()j += 1

print triangle

j = 1while j <= 5:# 第一行星星打印i = 1# i表示每行里面星星的个位数,这个数字要和行号相等,# 所以i一定要和j联动while i <= j:print("*", end='') # end=''取消换行符i += 1# 一行星星是结束:借助空的print换行显示下一行print()j += 1

Ninety-nine multiplication table

j = 1while 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

picture

message