#for循环
for i in range(0,10):# i为循环变量,范围是0-9,左闭右开
print("i = %d"%(i))
rabbit = 3
print("请输入N的值")
N = int(input())#键盘输入
for i in range(0,N):
rabbit = rabbit * 2
print("%d年后,兔子的数量为 %d"%(N,rabbit))
#列表
class1 = ['Joan','Bill','Niki','Mark','Mark']
class2 = ['Tom','Linda','Bill']
for name1 in class1:
for name2 in class2:
if name1 == name2:
print(name1)
#集合
a = set()
b = {1,2,'abc'}
print(a)
print(b)
c = {1,2,1,1,2}
print(c)
a = 'abcde'#字符串
test = set(a)
print(test)
b = [1,2,3]#列表
test = set(b)
print(test)
c = (1,2,'abc')#元组
test = set(c)
print(test)
d = {'a':1,'b':2,'c':3}#字典
test = set(d)
print(test)
test = {1,2,'abc',567,8}#集合是乱序的,不能用索引访问
for item in test:
print(item)
nums = {1,2,3}
nums.add(6)#添加
nums.remove(1)#移除
print(nums)
#交集:取两个集合公共的元素 &/intersection
#并集:取两集合的全部元素 |/union
#差集:取一个集合中另一个集合没有的元素 -/difference
a = {1,2,3,4}
b = {3,4,5,6}
print(a&b)
print(a.intersection(b))
print(b&a)
print(b.intersection(a))
print(a|b)
print(a.union(b))
print(b|a)
print(b.union(a))
print(b-a)
print(b.difference(a))
num1 = int(input('输入班级1学生数量:'))
class1 = set()
for i in range(0,num1):
name = input('输入学生%d姓名:'%(i+1))
class1.add(name)
num2 = int(input('输入班级2学生数量:'))
class2 = set()
for i in range(0,num2):
name = input('输入学生%d姓名:'%(i+1))
class2.add(name)
same = class1 & class2
print("重名的学生:")
for name in same:
print(name)
#字典
container = {"苹果":"A","桃子":"B","橡胶":"C","梨子":"D"}
print(container)
print(container["桃子"])
price = dict()
price['苹果'] = 5
price['桃子'] = 6
price['香蕉'] = 3
price['梨子'] = 4
print(price)
if '苹果' in price:
print("苹果的价格 %d"%(price['苹果']))
else:
print("该水果不卖")
if '荔枝' in price:
print("苹果的价格 %d"%(price['荔枝']))
else:
print("该水果不卖")
#print(price['荔枝'])
#关系运算
a = int(input("请输入第一门成绩:"))
b = int(input("请输入第二门成绩:"))
c = int(input("请输入第三门成绩:"))
print(a<90 and a>=60)
score = (a>=90)*2+(a<90 and a>=60)+(b>=90)*2+(b<90 and b>=60)+(c>=90)*2+(c<90 and c>=60)
print("您的课程总积分为:%d" %(score))
【学习python每天几分钟】第一天笔记
于 2024-08-13 10:07:56 首次发布