1.注释:单行通过#注释,多行注释使用快捷键 ctrl+?
2.变量:变量名+变量值+变量类型
3.列表:lista=[1,2,3,4];
元组:tuple1=(1,2,3,4) 元组不支持修改元素;
字典:dicta={"name":"张三","age":"18"} 键值对的形式;
集合:seta={1,2,3,"hello"}无序,元素不重复,只有键没有值的字典;
4.if-else:条件冒号不要去掉,缩进要一致
money = int(input("请输入你的存款:(万)"))
day = int(input("今天星期几:(1-7)?"))
if money>100:#如果
print("恭喜你可以买宝马!")
print("真开心")
if day<=5:
print("周末去提车")
else:
print("今天下午去提车")
elif 50<=money<=100:
print("买丰田")
elif 20<=money<50:
print("买二手车")
else:
print("骑自行车吧")
print("程序结束!")
5.round()函数:保留数值后面几位小数
num1=1001
price1=1.991
name1="苹果"
num2=1002
price2=2.991
name2="香蕉"
num3=1003
price3=3.991
name3="梨"
number=int(input("请输入商品的编号:"))
count=int(input("请输入商品的数量:"))
#round 保留值后面几位小数
#提前声明变量,保存后面需要的商品名称和价格
price=0
name=""
if number==num1:
price=price1
name=name1
elif number==num2:
price=price2
name=name2
elif number==num3:
price=price3
name=name3
else:
print("此商品不存在")
amount=price*count
if price!=0:
print("你购买的商品是:",name,"商品数量是:",count,"商品总价是:",round(amount,2))
paymoney=float(input("请输入你的付款金额"))
if paymoney>amount:
print("找零:",round(paymoney-amount,2))
else:
print("你的付款金额不足")
6.循环:for循环 while循环
#循环语句
# for 变量名 in 序列:
# 循环语句
for x in range(5):#range(开始值,结束值(不包含),步长)
print("hello world")
for s in ["a","b","c"]:
print(s)
for循环遍历容器,将容器中的数据一个一个获取出来
通过for循环遍历四种容器的方法:
# #list数组
# names=["张三","李四","王五"]
#
# #直接遍历
# for name in names:
# if name=="王五":
# print("有此人")
#
# #构造索引
# for i in range(0,len(names)):
# print(names[i])
# #元组
# scores = (60, 70, 80, 90)
# sum=0
# for score in scores:
# sum=sum+score
# print("总分是:",sum)
# print("平均分是:",sum/len(scores))
# print(len(scores))
#字典
#如果用循环遍历字典,只能获取键
dicta={"name":"张三","age":"18","hobby":"打篮球"}
for s in dicta:
print(s,dicta[s])
7.自定义函数