数据的简单应用
1、什么是数据类型
数据的种类,不同种类的数据的存取机制不一样,用途也不一样
整型int
浮点型float
字符串类型str
列表类型list
字典类型dict
布尔类型bool
2、数据为何要分类型
数据是事物的状态,事物的状态是分为多种多样的,对应着就应该用不同类型的数据去记录
-----------------------------------
1、整型int
1.定义:
age= 18 #age = int(18) level = 10
2.作用:年龄、等级、各种号码,个数
2、浮点型float
1.定义
salary = 3.1 #salary = float(3.1) print(type(salary))
2.作用:身高、体重、薪资
3、字符串类型
定义:在" "或者' '或者""" """或者''' '''中间包含一系列字符
msg1="你he xxx" msg2='你he xxx'
ps:外层用双引号,内层就需要用单引号
作用:记录一些描述性质的内容,比如名字、国籍、一段话
4、列表类型
定义:在[ ]内逗号分割开多个任意类型的值
l=[111,3.1,"123"] print(l[2])
作用:按照索引/顺序存放多个值
5、字典类型
定义:在{ }内用逗号分隔开多个key:value,其中value可以是任意类型,而key通常是字符串类型
作用:按照属性名存放多个值,key:value组合
info={"name":"egon","age":18,"gender":"male","level":10,"annual_salary":18} print(info["age"])
6、布尔类型
定义:总共就两个值:True,Flase
显式的布尔值:像是True和Flase
隐式的布尔值:所有数据类型的值都可以当作隐式的布尔值去用,其中0,空,None三者的布尔值为Flase,其余均为True
作用:通常会作为条件
与用户交互
接受用户的输入
name=input("请输入你的用户名:") print(name)
基本运算符
算数运算符
print(10 + 3) print(10 - 3) print(10 * 3) print(10/3) print(10//3)整除 print(10%3)取余
比较运算符
print(10 == 10) print("abc" != "ddd")
赋值运算符
age=10 增量赋值 age += 2 #age=age+2 age **= 2 #age=age ** 2 print(age) 链式赋值 x=y=z=10 print(id(x)) print(id(y)) print(id(z)) #这三个id都是一样的 交叉赋值 x=10 y=20 #temp=x #x=y #y=temp x,y=y,x print(x) print(y) 解压赋值 dic={"k1":111,"k2":222,"k3":333} #x,y,z=dic #print(x,y,z) x,*_=dic print(x)
逻辑运算符
not:代表把紧跟其后的条件结果取反
print(not 10 > 3)
and:连接左右两个条件,左右两个条件必须同时成立,最终结果才为True
print(True and 1 > 0 and "aaa" == "aaa") print(True and 1< 0 and "aaa" == "aaa")
or:连接左右两个条件,左右两个条件但凡有一个是成立,最终结果就为True
print(True or 1 > 0 or "aaa" == "aaa") print(False or 1 < 0 or "aaa" != "aaa")
短路运算=》偷懒原则
优先级:not>and>or
res=3>4 and or not 1==3 and 'x' == 'x' or 3 >3 #res=(3>4 and 4>3) or (not 1==3 and 'x'=='x')or 3 >3 print(res)
流程控制之if判断
一:if判断的完整语法: if 条件1: 代码1 代码2 代码3 elif 条件2: 代码1 代码2 代码3 elif 条件3: 代码1 代码2 代码3 ... else: 代码1 代码2 代码3 二:单分支 if 条件1: 代码1 代码2 代码3 三:双分支: # 3.1 if 条件1: 代码1 代码2 代码3 else: 代码1 代码2 代码3 # 3.2 if 条件1: 代码1 代码2 代码3 elif 条件2: 代码1 代码2 代码3 四 多分支
while循环
什么是循环 循环就是重复做某件事 2、为何要用循环 为了让计算机能够像人一样去重复做某件事 3、如何用循环 while 条件: 代码1 代码2 代码3 结束while循环的两种方式 # 方式一:条件改为假 # 方式二:break干掉本层循环 # 区别是:break会立即终止本层循环,而方式一需要等到下一次循环判断条件时才会生效 while+continue:终止本次循环,直接进入下一次 # count=0 # while count < 6: # if count == 3: # count += 1 # continue # continue同级别之后一定不要写代码 # # print(count) # count+=1 while+else # count=0 # while count < 6: # if count == 3: # break # print(count) # count+=1 # else: # print("==========>") # 当while循环正常结束后才会执行 死循环 while True: x=input(">>: ")