import copy
a = [1,2,3,[4,5],6]
b=copy.copy(a)
c=copy.deepcopy(a)
a.append(7)
a[3].append(9)
print(a,id(a))
print(b,id(b))
print(c,id(c))
a=5
print(id(a))
print(id(a[3][1]))
2、判断是否为闰年
y = int(input("请输入年份"))
if y%4==0 and y%100!=0 or y%400==0:
print(y,"是闰年")
else:
print(y,'不是闰年')
4、not 的用法
stu = True
if not stu:
print("你不是学生")
else:
print("是学生,可以进校园")
5、or 前面不成立,取决于后面
a = 5
b=a>7 or 4
print(b)
输出4
6、将一百以内质数放到列表内
a=[]
for x in range(2,101):
for i in range(2,x):
if x %i==0:
#print(x,"不是质数")
break
else:#注意else不要放错地方注意注意
#print(x,"是质数")
a.append(x)
print(a)
a=[]
for data in range(2,100):
for i in range(2,data):
if data%i==0:
break
else:
a.append(data)
print(a)
7、判断是否为质数
num = int(input("请输入一个数:"))
i = 2
while i
8、九九乘法表
for i in range(1,10):
for j in range(1,i+1):
print(j,"*",i,"=",j*i,end="\t")
print()
for i in range(1,10):
j=1
while j<=i:
print(j, "*", i, "=", j * i, end="\t")#\t相当于tab键
j+=1
print()
i = 1
while i<=9:
for j in range(1,i+1):
print(j, "*", i, "=", j * i, end="\t")
i+=1
print()
i= 1
while i<=9:
j=1
while j<=i:
print(j,"*",i,"=",i*j,end="\t")
j+=1
print()
i+=1
10、输出正方形*
i= 1
while i<=5:
j = 1
while j<=5:
if i==1 or j==1 or i==5 or j==5:
print("*",end="")
else:
print(" ",end="")
j+=1
print()
i+=1
在python中保存这种键值对格式的数据一种类型叫做字典 dict
a={'name':'张三','age':"19","sex":"男"}
print(a["name"])
print(a["sex"])
字典key不能是可修改数据类型,例如列表
a={[1]:'张三','age':"19","sex":"男"}
print(a["name"])
print(a["sex"])
a={'name':'张三','age':"19","sex":"男"}
a['score']=99#没有添加
print(a)
a['age']=18#有就覆盖
print(a)
a={'name':'张三','age':"19","sex":"男"}
a.setdefault("age",20)
print(a)
a.setdefault('score',99)
print(a)
a={'name':'张三','age':"19","sex":"男"}
b=a.pop('name')#列表可以无参数或有参数,字典必须有
print(b)
print(a)
a={'name':'张三','age':"19","sex":"男"}
a.clear()
print(a)
a={'name':'张三','age':"19","sex":"男" }
print(a.keys())
for k in a.keys():
print(k)
结果:
dict_keys(['name', 'age', 'sex'])
name
age
sex
a = {'name': '张三', 'age': "19", "sex": "男"}
print(a.values())
for v in a.values():
print(v)
结果:
dict_values(['张三', '19', '男'])
张三
19
男
a={'name':'张三','age':"19","sex":"男"}
print(a.items())
for kv in a.items():
print(kv)
for k,v in a.items():
print(k,v)
结果:
dict_items([('name', '张三'), ('age', '19'), ('sex', '男')])
('name', '张三')
('age', '19')
('sex', '男')
name 张三
age 19
sex 男
1、字典转换列表在转换字典
a={'name':'张三','age':'19','sex':'男'}
b=[]
c={}
for k,v in a.items():
b.append((k,v))
print(b)
for x in b:
print(x[0],x[1])
c[x[0]]=x[1]
print(c)
集合中的元素是无序的,唯一的、不可改变类型
a=(1,2,3)
print(a,type(a))
print(a[0])
a=(1,2,3)
i=0
while i