python学习笔记,快速了解上⼿python,代码+注释+例题From IT私塾五天快速上⼿python 纯⼿打学习笔记,代码+注释+例题
# -*- codeing =utf-8 -*-
# @Time : 2021/3/18 13:49
# @Author : David
# @File : DEMO1.py
# @Software :PyCharm
'''
print("标准化输出字符串")
a=18
print("这是变量:",a)
'''
#格式化输出
age=22
print("我的名字是%s,我的国籍是%s"%("David","中国"))#%s引⽤字符串,%d引⽤数字
print("我的年纪是:%d 岁"%age)
#输出显⽰
print("aaa","bbb","ccc")
print("www","baidu","com",p=".")
print("hell",end="")
print("world",end="\t")
print("python",end="\n")
print("end")
#输⼊
password=input("请输⼊密码")
print("您刚刚输⼊的密码是:",password)
#测试变量类型
a=input("输⼊:")#默认输⼊的是字符串
b=int(input("输⼊:"))#强制类型转化
print("输⼊了⼀个数:%d"%b)
#强制类型转化基础代码
a=int("123")
b=a+100
print(type(b))
#条件判断语句
if1:
print("True")
el:
print("Fal")
print("end")
#例⼦1
score =int(input("请输⼊成绩:"))
if score>=90and score<=100:
print("本次考试等级为A")
elif score>=80and score<90:
print("本次考试等级为B")
elif score>=70and score<80:
print("本次考试等级为C")
elif score>=60and score<70:
print("本次考试等级为D")
el:
el:
print("本次考试等级为E")
#例⼦2
xingbie =0#1代表男⽣,0代表⼥⽣
danshen=0#1代表单⾝,0代表有男朋友或⼥朋友
if xingbie==1:
print("男⽣",end=',')
if danshen==1:
print("我给你介绍⼀个⼥朋友")
el:
print("你给我介绍⼀个吧")
el:
print("⼥⽣",end=',')
if danshen==1:
print("我给你介绍⼀个男朋友")
el:
print("你给我介绍⼀个吧")
#例⼦3(⽯头剪⼦布游戏)
import random #引⼊随机库
a=int(input("请输⼊:剪⼑(0)、⽯头(1)、布(2):")) if a==0:
print("你的输⼊为:剪⼑(0)")
elif a==1:
print("你的输⼊为:⽯头(1)")
elif a==2:
print("你的输⼊为:布(2)")
x = random.randint(0,2)#随机⽣成[0,2]
print("随机⽣成数为:%d"%x)
if a==0and x==1:
print("哈哈,你输了:)")
elif a==1and x==2:
print("哈哈,你输了:)")
elif a==2and x==0:
print("哈哈,你输了:)")
elif a==0and x==0:
print("平局,再来⼀局")
elif a==1and x==1:
print("平局,再来⼀局")
elif a==2and x==2:
print("平局,再来⼀局")
el:
print("你赢了")
#循环控制语句
for i in range(5):
print(i)
for i in range(0,14,3):
print(i)
for i in range(-10,-100,-30):
print(i)
name="chengdu"
for x in name:
print(x,end="\t")看春晚作文
a=["aa","bb","cc","dd"]
for i in range(len(a)):
print(i,a[i])
i =0
while i<5:
print("当前是第%d次执⾏循环"%(i+1)) print("i=%d"%i)
i +=1
#例⼦(累加器,求1加到100)
a=0
i=0
while i<=100:
a=a+i
i+=1
print("1到100的和为%d"%a)
#or
a=0
for i in range(101):
a=a+i
print("1到100的和为%d"%a)
早餐饼#while&el
count=0
while count<=5:
print(count,"⼩于5")
count +=1
el:
print(count,"⼤于或等于5")
肌酸激酶高#pass例⼦
for letter in"room":
if letter =="o":
pass
print("pass")
el:
print(letter)
#循环例⼦
i=0
while i<10:
i=i+1
print("-"*30)
if i==5:
break#结束整个循环
print(i)
i=0
while i<10:
i=i+1
print("-"*30)
if i==5:
continue#结束本次循环
print(i)
#循环实战
a=1
while a<=9:
b=1
while b<=a:
print("%d*%d=%d\t"%(b,a,b*a),end=("")) b+=1
print()
a+=1
for a in range(1,10):
for b in range(1,a+1):
print("%d*%d=%d\t"%(b,a,b*a),end=(""))
print()
#字符串
a="hello"
b="wrold"
print("a+b的输出结果:",a+b)
print("a[1:4]的输出结果为:",a[1:4])#左闭右开原则,选中第⼀位不包括第四位
word='字符串'
ntence="这是⼀个句⼦"乞休
paragraph="""
这是⼀个段落
可以由多⾏组成
"""
print(word)
print(ntence)
print(paragraph)
my_str='I\'m a student'#\为转译字符,表⽰在字符串中转译
print(my_str)
my_str="Jason said \"I like you\""
print(my_str)
str="chengdu"
print(str[1:5])
print(str[:5])
print(str[5:])
print(str+",你好")
print((str+"\n")*3)#\n表⽰换⾏再,\t表⽰空格,
print(r"hello\nchengdu")#字符串前⾯有r代表正则表达式,引号内的转译符号失效,变为字符串的⼀部分#bytes.decode(encoding="utf-8",errors="strict") 读取时候,解码成utf-8
#decode(encoding="utf-8",erroes="strict") 输出时候,解码成strict
宁乐派
#列表
list=['abcd',786,2.23,'runoob',70.2]
print(list[1:3])
tinylist=[123,'runoob']
print(list+tinylist)
namelist=[]#定义⼀个空列表
namelist=["⼩张","⼩王","⼩李"]
print(namelist[0])
testlist=[1,"测试"]#列表中可以存储混合类型,保持固有类型
print(type(testlist[0]))
print(type(testlist[1]))
namelist=["⼩张","⼩王","⼩李"]
#循环输出
for name in namelist:
print(type(name),end="\t")
print("")
a=0
length=len(namelist)
print(len(namelist))#len函数可以输出列表的长度
while a<length:
print(type(namelist[a]),end="\t")
a+=1
#增
#新增数据⾄列表尾部
namelist=["⼩张","⼩王","⼩李"]
思念家乡的成语
nametemp=input("请输⼊学⽣姓名:")
namelist.append(nametemp)
print(namelist)
#列表的追加
namelist=["⼩张","⼩王","⼩李"]
spacelist=[1,2,3,4]
print(namelist)
#数据的插⼊
namelist=["⼩张","⼩王","⼩李"]
namelist.inrt(1,"⼩赵")#插⼊在第⼏位,或者理解为在第⼏位之前插⼊
print(namelist)
print(namelist)
#列表的删除
#1.delete的⽅法
namelist=["⼩张","⼩王","⼩李"]
del namelist[0]#删除该列表第0位
print(namelist)
#2.remove的⽅法
namelist=["⼩张","⼩王","⼩李"]
print(namelist)
#3.pop的⽅法
namelist=["⼩张","⼩王","⼩李"]
幸运作文
namelist.append("⼩赵")
print(namelist)
print(namelist.pop(0))#与delete类似,但是删除后可以返回删除的元素,没有默认值则删除列表最后⼀位print(namelist)
#改
namelist=["⼩张","⼩王","⼩李"]
print(namelist)
a=int(input("请输出需要修改的元素下标(0-2):"))
b=input("请输出修改后的元素:")
print(b)
namelist[a]=b #修改器的基本结构
print(namelist)
#查
namelist=["⼩张","⼩王","⼩李"]
findName=input("请输⼊你要查找的学⽣姓名:")
if findName not in namelist:#⼀般配合条件判断语句使⽤
print("没有找到")
中国最美高速公路
el:
print("在学员名单中找到了相同的名字")
#索引查询
mylist=["a","b","c","a","b"]
print(mylist.index("a",1,4))#可以查找指定下标范围的元素,并找到对应数据的下标,遵循左闭右开原则unt("b"))#统计列表中某个元素出现⼏次