1>变量:储存数据的容器
a=int(input(""))
b=34;d=34;d='wef'
2>数据类型:int float bool str dict set tuple
int>a=23
a,b=23,43=》a=23;b=43
float>12.34 23.4
a=32.3,b=23,9
a,b,c=12.3,445.3,67.0=》a=12.3;b=445.3;c=67.0
bool =》输出True=1 或 False=0
a=3
b=34
bool(a<b)#结果为True
print(a<b)#结果为True
set =》{}包裹,数据之间用,号隔开,无序
set1={1,2,3,4,5,5}
虽然set1中有两个5,但他们是不一样的,可以用生活或表示为:世界上没有两个相同的人,即使他们长得一样
元组 =》用()包裹,用,号将数据隔开,不可更改
tuple1=(1,2,3,4,5,6)
列表 =》用[]包裹,数据之间用,号隔开,有顺序
运算符:赋值=
比较:<>= <= >= !-
算术运算符:+-*/ ** // %
逻辑运算符: and or not
成员运算符:in not in
+= *= /= //= %= -= **=
a=23
li1=[1,23]
print(a in li1)#True
运算符的优先级:括号优先,赋值最后,比较其次,逻辑靠后,逻辑之中,自左向右
序列的操作方式:
下标:自左向右0开始,依次递增1,自右向左-1开始,一次递增-1
索引的运用:变量名[下标]
切片:变量[开始:结尾]#实际取值包头不包尾,[开始,结尾)==[开始:结尾-1]
步长:变量[开始:结尾:步长]--从想取值的第一个到下一个的下标值相减的绝对值,符合索引和切片的使用规则
pop 删除下标#一次只能删除一个----list
列表
list1=[1,2,3,4,5,6]#列表的pop删除下标运用
list1.pop(1)
print(list1)
元组
tuple1=(1,2,3,4,6,)
print(tuple1)#结尾有,号对结果无影响
#tuple1.pop(1)#AttributeError: 'tuple' object has no attribute 'pop'
#元组类型的数据构成的变量不可以用pop来删除数据,tuple类型
集合
set1={1,2,3,4,5,6}
#set1.pop(1)#TypeError: pop() takes no arguments (1 given)
print(set1)
字符串
str1='周杰伦在开演唱会'
#str1.pop(1)
print(str1)#AttributeError: 'str' object has no attribute 'pop'
int
int1=1234
#int.pop(1)#AttributeError: type object 'int' has no attribute 'pop'
print(int1)
remove 删除一个确定的数据,一次只能删除一个-----list set
列表
list1=[1,2,3,4,54,5,6,]
print(list1)#列表数据的最后多一个,号对结果没有影响
list1.remove(1)
print(list1)
#list1.remove(2,3)#remove()只能接受一个参数
元组
tuple1=(1,32,34,)
tuple1.remove(1)#AttributeError: 'tuple' object has no attribute 'remove'
#元素对象没有属性remove
字符串
str1='123'
str1.remove(1)#AttributeError: 'str' object has no attribute 'remove'
集合
set1={1,3,4,5,67}
set1.remove(1)
print(set1)#{67, 3, 4, 5}
print(set1.remove(3))#None
clear--一次性清除所有数据
str1='23424'
str1.clear()#AttributeError: 'str' object has no attribute 'clear'
list1=[1,3,4,56]
list1.clear()
print(list1)#[]
list2=[1,3,3,54,55]
print(list2.clear())#None
set1={1,3,4,566,5}
set1.clear()
print(set1)#set()
a={1,2,22,43,54}
a.clear()
print(a)#set()
print(a.clear())#None
tuple1=(1,3,4,45)
#tuple1.clear()#AttributeError: 'tuple' object has no attribute 'clear'
print(tuple1)
int1=1223
#int.clear()#AttributeError: type object 'int' has no attribute 'clear'
extend() 拆封添加
#拆封添加在列表中
a=123
li=[]
#li.extend(a)#TypeError: 'int' object is not iterable
#int 不可拆分添加
b='123'
li.extend(b)
print(li)#['1', '2', '3']
#字符串可以拆封添加在列表中
c={1,3,4,5,6}
li.extend(c)
print(li)#['1', '2', '3', 1, 3, 4, 5, 6]
#多元素集合可以添加在列表中
tuple1=(1,23,34,4,5)
li.extend(tuple1)
print(li)#['1', '2', '3', 1, 3, 4, 5, 6, 1, 23, 34, 4, 5
#多元素元组可以拆封添加在列表中
dict1={1:123,2:23}
li.extend(dict1)
print(li)#['1', '2', '3', 1, 3, 4, 5, 6, 1, 23, 34, 4, 5, 1, 2]
#字典的键会被添加到列表中(其中的数字不会被拆分为个位数输出保留键的完整形式)
dict2={323:23,234:343}
li.extend(dict2)
print(li)
expend
replace
update
sep=''控制print()中用逗号隔开的多条语句输出的间隔格式
end=''控制print结尾的格式
print()格式化输出
1> f''
2 > .format()
3> %()
day='星期一'
print(f'今天是{day}')
print('今天是:',day)#今天是 星期一#有间隔
print('今天是%s'%(day))
print('今天是{}'.format(day))
强制转换:
int(input(""))·#将输入的数据转化为整型
float(input(""))
input("")#输入的数据会自动转化为字符串类型
流程控制:if elif else
a=int(input(""))
if a==1:
print("吃饭")
elif a==2:
print("吃黄焖鸡")
else:
print("吃方便面")
分支嵌套:
a=int(input(""))
if a==1:
print("吃饭")
b=input('')
if b=='不想吃':
print("那你就不吃吧!")
elif b == '想吃肉,不想吃素':
print("那你就吃素吧!")
else:
print("输入有误,吃土去吧!")
循环:for while
range() range(4)-------0,1,2,3
for a in range(10):
print(a)
1
2
3
4
5
6
7
8
9
#in是成员运算符
li=[1,3,45,56,67,876]
for i in li:
print(i)
1
3
45
56
67
876
函数:封装着特定功能的代码块
格式:
def 函数名():
函数体
嵌套函数
def han1():
sum=0
def han2():
nonlocal sum
for i in range(1,101):
sum+=i
print(sum)
return han2
han1()()
#闭包函数的案例:
def han1(fun):
eum=0
def han2():
nonlocal eum
fun()
for i in range(10):
eum+=i
print(f"他可是有{eum}扇翅膀")
return han2
@han1
def han3():
print("周杰伦在演唱会")
#han(han3)()
def han1(fun):
import random
def han2():
player=int(input("请输入你的号码:(1--2)"))
ai=random.randint(1,2)
print(ai)
if player==ai:
print("马上进入抽奖系统\n")
fun()
else:
pass
return han2
@han1
def han3():
import random
jiangpinliebiao=['周杰伦专辑','张杰的签名','薛之谦的演唱会门票'
,'谢谢惠顾','给你一巴掌']
print("恭喜你进入抽奖系统")
ai1=random.choice(jiangpinliebiao)
print(f"恭喜你,系统赐予你{ai1}的奖品")
han3()
def han1(fun):
import random
def han2():
player=int(input("请输入你的号码:(1--2)"))
ai=random.randint(1,2)
print(ai)
if player==ai:
print("马上进入抽奖系统\n")
fun()
else:
pass
return han2
@han1
def han3():
import random
jiangpinliebiao={1:'周杰伦专辑',2:'张杰的签名',3:'薛之谦的演唱会门票'
,4:'谢谢惠顾',5:'给你一巴掌'}
li1=[1,2,3,4,5]
print("恭喜你进入抽奖系统,请在(1--5)号选一个号码。")
player2=int(input(""))
ai1=random.choice(li1)
if player2==ai1:
print(ai1)
print(f"恭喜你,系统赐予你{jiangpinliebiao[ai1]}的奖品")
else:
print(ai1)
print(f"恭喜你,系统赐予你{jiangpinliebiao[ai1]}的奖品")
han3()
'''
import keyword
print(keyword.kwlist)#可查看关键字
自我复习(python 快三周的学习)
于 2023-12-15 09:35:46 首次发布