赞
踩
#eg:1^3+5^3+3^3=153
number=input("please input number:")
if number.isdecimal():
if len(number)==3:
x1=int(number[0])
x2=int(number[1])
x3=int(number[2])
if x1**3+x2**3+x3**3==int(number):
print("其为水仙花数")
else:
print("不是水仙花数")
else:
print("数字不正确")
else:
print("请输入三位数合法数字")
list=["周大苏打","周对手的防守","马虎他","周星星"]
#这里有坑,因为在列表的删除过程中,删除一个元素其他会前移,会出现问题,所以采用倒叙删除
for i in range(len(list)-1,-1,-1):
if list[i][0]=="周":
list.pop(i)
print(list)
''' 学习中遇到问题没人解答?小编创建了一个Python学习交流QQ群:725638078 寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书! ''' cars=["鲁A2444","鲁B1233","京B8989M","黑C49678","黑C46555","沪B25041"] locals={"沪":"上海","黑":"黑龙江","鲁":"山东","鄂":"湖北","湘":"湖南","京":"北京"} #车牌分块区分,现给出车牌,根据车牌的信息,分析的各省车牌持有量 countlist={} for i in cars: if locals[i[0]] not in countlist: countlist[locals[i[0]]]=1 else: countlist[locals[i[0]]]=countlist[locals[i[0]]]+1 print(countlist) ---------------------------------------------------------- #升级版 countlist={} for i in car: countlist[locals[i[0]]]=countlist.get(locals[i[0]],0)+1 print(countlist)
dic={}
list1=[]
with open ("a.txt",encoding="utf-8",mode="r") as f1:
for line in f1:
line=line.strip("\n").split(" ")
dic["name"]=line[0]
dic["price"] = line[1]
dic["count"] = line[2]
list1.append(dic)
print(list1)
server = [ {"name": "服务器1", "price": 10000}, {"name": "服务器2", "price": 10500}, {"name": "服务器3", "price": 10700}, {"name": "服务器4", "price": 10800}, ] while True: for index,dic in enumerate(server): print(index,dic["name"],dic["price"]) number=input("请输入商品序号:").strip() if number.isdecimal(): number=int(number) if 0<number<len(server): print(server[number-1]["name"],server[number-1]["price"]) else: print("你输入的超出了范围") elif number.upper()=="Q": break else: print("你输入的为非法字典")
判断dict中“name"值是否在字典中
dict={"name":"zhu","time":"fdsf"}
print("name" in dict.keys())
list=[]
while 1:
username=input("请输入用户名:")
if username.upper()=="Q":break
else:
password=input("输入密码:")
dic={}
dic["name"]=username
dic["password"]=password
list.append(dic)
print(list)
v1={"k1":"v1","k2":[1,2,3]}
v2={"k1":"v1","k2":[1,2,3]}
result=v1==v2
result1=v1 is v2
print(result,result1)
#结果True False
因为不论是同代码的缓存机制还是小数据池缓存机制都只只用于bool int str三种数据类型
v1="dfsfsf"
v2=[1,2,4,v1]
v1="fsfdfsfsdfs"
print(v2)
#结果[1, 2, 4, 'dfsfsf']
原因解释是这里是字符串,不可以变得数据结构,然后在V1赋值的时候相当于开辟了一个新的数值地址,因为不可变还是V2还是指向了v1运来值的地址
list=[]
dict={}
for i in range(10):
dict["user"]=i
list.append(dict)
print(list)
#结果[{'user': 9}, {'user': 9}, {'user': 9}, {'user': 9}, {'user': 9}, {'user': 9}, {'user': 9}, {'user': 9}, {'user': 9}, {'user': 9}]
list=[]
for i in range(10):
dict = {}
dict["user"]=i
list.append(dict)
print(list)
#结果[{'user': 0}, {'user': 1}, {'user': 2}, {'user': 3}, {'user': 4}, {'user': 5}, {'user': 6}, {'user': 7}, {'user': 8}, {'user': 9}]
解释,前面因为最后一步对字典进行了更改,后面因为每次添加的列表都是一个新的字典,所以数字不是重复的
number=88
count=0
while count<3:
inputnumber=int(input("请输入数字:"))
if inputnumber>number:
print("结果大了")
elif inputnumber<number:
print("结果小了")
else:
print("恭喜你猜对了")
break
count+=1
else:
print("你太蠢了")
count=1
s=0
while count<100:
if count%2==0:
s=s-count
else:
s=s+count
count+=1
print(s)
count=0 while count<3: username=input("please input your username:") password=input("please input your password:") code="abcd" your_code=input("please input your code:") if your_code==code: if username=="xiaoming"and password=="123": print("success!") break else: print("your uesename or password is error!") count=count+1 else: print("your code is error!!") count+=1 exit("You've logged in three times")
#方法一比较傻瓜式
count=0
flag=True
while flag:
if count==100:
flag=False
else:
count=count+1
print(count)
#方法二
number=0
while number<100:
number=number+1
pint(number)
count=2
while True:
print(count)
count=count+2
if count==102:
break
结尾给大家推荐一个非常好的学习教程,希望对你学习Python有帮助!
Python基础入门教程推荐:更多Python视频教程-关注B站:Python学习者
Python爬虫案例教程推荐:更多Python视频教程-关注B站:Python学习者
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。