部分python语言总结

Python语言总结

元组(tuple):
元素不能更改,可存储不同类型的元素
可用于函数的输入参数,返回值
info_tuple: Tuple[str, int, float, str] = (“zhangsan”, 3, 12.56, “xiaohong”)

print(type(info_tuple))

print(info_tuple[0])
print(info_tuple.index(“zhangsan”))
print(info_tuple.count(“zhangsan”))
print(len(info_tuple))
for info1_tuple in info_tuple:
print(info1_tuple)
字典:
xiaoming = {“name”: “小明”,
“age”: 18,
“gender”: True,
“weight”: 75.5,
“height”: 1.60}
print(xiaoming[“name”])
xiaoming[“age”] = 12
xiaoming.pop(“height”)
print(len(xiaoming))

合并字典

te = {“height”: 1.68}
xiaoming.update(te)

清空字典

xiaoming.clear()
遍历字典:
for k in xiaoming:
print("%s - %s" % (k, xiaoming))

For循环的运用:列表循环遍历
for num1_list in num_list:
print(num1_list)

if的运用:
holiday_name = str(input(‘今天是什么节日?(请输入 平安夜 或 生日 或 情人节 )’))
if holiday_name == ‘平安夜’:
print(‘买苹果’)
elif holiday_name == ‘生日’:
print(‘买蛋糕’)
elif holiday_name == ‘情人节’:
print(‘买玫瑰’)
else:
print(‘输入错误’)
字符串:
info = “%s年龄为%d身高为%.2f” % info_tuple
str = ‘我的外号是"西瓜"’

在一个字符串中取出一个字符

print(str[3])

遍历每一个字符

for char1 in str:
print(char1)
常用操作:
len(str)
str.count(“我”)
str.index(“号”)

发布了17 篇原创文章 · 获赞 5 · 访问量 341

猜你喜欢

转载自blog.csdn.net/weixin_45116412/article/details/104396002