容器

输入

name=input('姓名') print(type(name),name)

类型转换

height=1.87 print('转换前的类型:',type(height)) height2=int(height) print( '转换后的类型:',type(height2))

int: float——int 整数类型的字符串——int

float: int——float 数字类型的字符串——float

任何类型都可以转换为str

格式化

name='柒' age=18 height=1.87 store=90 stunum=7

print('名字%s,年龄%d,身高%.3fm,及格率%d%%,学号%03d' %(name,age,height,store,stunum)) print(f'名字{name},年龄{age},身高{height:.3f}m,及格率{store}%,学号{stunum:03d}') print('名字{},年龄{},身高{:.3f}m,及格率{}%,学号{:03d}'.format(name,age,height,store,stunum))

运算符:按优先级

算术(幂乘/%//+-),比较,赋值,逻辑(not,and.or)

条件运算,逻辑运算,格式化的运用:

name=input('username') if name=='admin'or name=='test': print(f'welcom{name}back!') else: print('error')

if,elif,else 结构:

score=input('分数') if float(score)>=90: print('excellent') elif float(score)>=80 and float(score)<90: print('good') elif float(score)>=60 and float(score)<80: print('pretty') else: print('continue study')

类型转换另一种写法: score=int(input('分数')) if score>=90: print('excellent') elif score <90 and score>=80: print('very good') elif score <80 and score>=60: print('good') else: print('not good')

全部用if写: score=int(input('分数')) if score>=90: print('excellent') if score <90 and score>=80: print('very good') if score <80 and score>=60: print('good') if score<60: print('not good')

debug 调试代码:打断点:查看代码的执行步骤

if嵌套:猜拳游戏

import random computer=random.randint(1,3) while True: player=int(input('show hand:')) if player==1 and computer==2 or player==2 and computer==3 or player==3 and computer==1: print('player win') elif player==computer: print('winwin') elif player >3: print('error') else: print('compter win') if player==0: break

1-100之间偶数之和:

num=0 i=2 while i<=100: if i%2==0: print(i) num=num+i i=i+1 print(num)

1-100之间奇数之和:

num=0 i=1 while i<=100: if i%2==1:

    # print(i)
    num=num+i
i=i+2

print(num)

1-100之间数之和:

'''确定变量'''

num=0

'''确定初始值'''

i=1

'''循环条件'''

while i<=100:

'''循环语句'''

num=num+i

'''更新值'''

i=i+1

'''输出结果'''

print(num)

判断闰年:

while True: year=int(input('年份')) if (year%4==0 and year%100==1) or year%400==0: print(f'{year}闰年') else: print('%d不是闰年' %year)

for循环的基本格式:

for 变量 in 容器 循环执行的代码 for 变量 in range(n): pass for 变量 in range(start,end): pass

简述break和continue的作用:

break 终止循环的执行; continue 终止此次循环,继续下一次循环

简述字符串查找和替换的方法:

字符串.find(sub_str,start,end) 字符串.replace(oldstr,newstr,count)

简述字符串拆分和连接的方法:

字符串.split('sep',maxsplit) 字符串.split() 字符串.split(maxsplit=n)

字符串.join(列表) 可以是字符串,可以是元祖,可以是字典 将字符串添加到每个元素之间 列表中的元素必须是字符串类型

下标(索引):

字符串[下标] 指获取指定位置的字符

获取字符串中字符的个数:

num=len(str) print(num)

切片:获取字符串中多个字符,多个字符下标等差数列:step默认1,可不写

字符串.[start:end:step] 反转逆置字符串:str[::-1]


列表

定义:

mylist=[] mylist=[1,'小明',2.14.False] 列表支持下标、切片、长度: 获取第一个数据:list[0] 获取最后一个数据:list[-1]

第一和第二数据:list[0:2]

len(mylist)

查找

通过数据查找下标 列表.index(数据,start,end)

查找-判断是否存在:

数据 in 容器

查找-统计出现的次数:

列表.count(数据)

增加数据的方法:

尾部添加:list.append(数据) 指定下标位置添加:列表.insert(下标,数据)

列表合并:

将list2中'name'的数据添加到list1中:list1.extend(list1) 将list2添加到list1中:list1.extend([list1])

删除:

  • 根据下标删除pop list1=['wo','and','you'] 删除指定下标数据,默认删除最后一个数据: list2=list1.pop() print(list1) 打印出的是删除后剩余的数据 print(list2) 打印出的是删除的数据you
  • 根据数据值删除remove list2=list1.remove('you') print(list1) 打印出的是删除后剩余的数据 print(list2) 打印出的是 None
  • 清空数据 list2=list1.clear() print(list1) 打印出的是删除后的空列表 print(list2) 打印出的是 None

    修改数据:

    list1[下标]='wom'

    列表的反转和逆置:

    list[::-1] 会产生一个新的列表,原列表不会改变 list.reverse() 原列表会改变

    列表的复制:

    list1.copy() list2=list1[:]

    列表的升序排序:

    list1.sort()

    列表的降序排序:

    list2=list1.sort(reverse=True)

    列表的嵌套:

    列表的去重:set


元组的使用:index,count,in

列表转换为元组:tuple2=tuple([1,2,3])

查看:下标和切片

tuple[下标]


字典:不能转换。键一般是字符串或数字,但不能是列表

增加和修改:键已存在则修改,不存在则尾部添加

dict1['键']=增加或修改的数据

删除:

del dict1['like'] 删除某个数据 dict1.clear() 清空

获取对应的数据:字典无下标概念,需要使用键获取数据

字典[键] 如果不存在则会报错 字典.get(键,数据值) 如不存在则返回 None,数据值一般不写

字典的遍历:

  • 对键进行遍历: 写法1 for 变量 in 字典:
             print(变量)
    
    写法2 for 变量 in 字典.keys():
             print(变量)
    
  • 对值进行遍历: for 变量 in 字典.values():
  • 对键值对进行遍历: for 变量键,变量值 in 字典.items() print(变量键,变量值)

    容器部分总结

    1.字符串,列表,元组支持加法运算 2.字符串列表元组支持乘一个数字
  • len()在容器中都可以使用
  • in关键字在容器中都可以使用,注意,在字典中判断的是字典的键是否
JSRUN前端笔记, 是针对前端工程师开放的一个笔记分享平台,是前端工程师记录重点、分享经验的一个笔记本。JSRUN前端采用的 MarkDown 语法 (极客专用语法), 这里属于IT工程师。