python笔记

  1. cls清屏
  • 字典(dict

键值对的形式,关键字不可变,不重复,键 可以为任意不可变数据(元组,数字,字符串)

  1. 创建方法
#1.{}
dict_01 = {
    (1,2,3):"zs"
    1:18
}
print(type(dict_01))

#2.dict
dict_02 = dict((["uname""zhangsan"],["age",18]))
print(dict_02)

#3.创建空字典
dict_03 = {}
  1. 使用方法
dict_01 = {
     "姓名":"张三""年龄":18
}

#增
dict_01["技能"] = "python"
print(dict_01)

#减
del dict_01["姓名"] = "安全"
print(dict_01)

#查
 1.字典名[键名]
 2.get函数,可以设置默认返回值,当查找的值不存在,则返回默认值
print(dict_01.get("技能","返回值"))
 3.keys(),返还字典中所有的键
    dict_01 = {
        "uname" = "zhangsan",
        "age" = 18
    }
    (uname,age)
 4.items(),返还字典中所有的键和值
    (uname,zhangsan),(age,18)
 5.values(),以列表的形式返回字典中所有的值
    (zhangsan,21)
 6.fromkeys()第一个参数作为键,第二个作为值
   d1 = dict_01.fromkeys(("姓名""年龄"),("zhangsan",18))
    print(d1)
 7.popitem()删除最后一位,并以元组的形式返还最后一位的键值对

1.循环

通过条件的不断变化,从而确定循环体是否继续执行

1.while循环

形式

while表达式:
(只要结果为布尔值即可)
    循环体

ex.

index = 1
while index <= 100
    print("hello world")#循环体
    index +=1

死循环

!0 = true
while true:
    print("hello")
2.break continue

break:终止循环

continue:终止本次循环,提前开始下一次循环

while index <=100:
    index +=1
    if index == 50
    continue
3.while…else
index = 1
while index <100:
    index += 1
    print(index)
    if index == 50:
        break
else:
    print("jieshu")
4.循环嵌套
while:
    while:
5.for循环

结构

for 临时变量 in 容量:
    代码块
迭代容器中的每一个元素

for既能遍历有序的容器,也能遍历无序的容器

不止能迭代容器,还能完成普通循环

range:取整值
range(start,end,step)
range(num):[0,num)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值