列表
name_list = ["zhangsan", "lisi", "wangwu"]
print(name_list[2])
print(name_list.index("wangwu"))
print(name_list)
name_list[1] = "李四"
name_list.append("王小二")
name_list.insert(1, "小美眉")
temp_list = ["孙悟空", "猪二哥", "沙师弟"]
name_list.extend(temp_list)
name_list.remove("wangwu")
name_list.pop()
name_list.pop(3)
name_list.clear()
del name_list[1]
list_len = len(name_list)
name_list.sort()
name_list.reverse()
for my_name in name_list:
print("我的名字叫 %s" % my_name)
元组
info_tuple = ("zhangsan", 18, 1.75, "zhangsan")
print(info_tuple[0])
print(info_tuple.index("zhangsan"))
print(info_tuple.count("zhangsan"))
for my_info in info_tuple:
print(my_info)
info_tuple = ("小明", 21, 1.85)
print("%s 年龄是 %d 身高是 %.2f" % info_tuple)
info_str = "%s 年龄是 %d 身高是 %.2f" % info_tuple
print(info_str)
print(len(info_tuple))
字典
xiaoming = {"name": "小明",
"age": 18,
"gender": True,
"height": 1.75,
"weight": 75.5}
print(xiaoming)
print(len(xiaoming_dict))
print(xiaoming_dict["name"])
xiaoming_dict["age"] = 18
xiaoming_dict["name"] = "小小明"
xiaoming_dict.pop("name")
print(xiaoming_dict)
temp_dict = {"height": 1.75,
"age": 20}
xiaoming_dict.update(temp_dict)
xiaoming_dict.clear()
for k in xiaoming_dict:
print("%s - %s" % (k, xiaoming_dict[k]))
card_list = [xiaoming,daming]
for card_info in card_list:
print(card_info)
students = [
{"name": "阿土"},
{"name": "小美"}
]
find_name = "张三"
for stu_dict in students:
print(stu_dict)
if stu_dict["name"] == find_name:
print("找到了 %s" % find_name)
break
else:
print("抱歉没有找到 %s" % find_name)
print("循环结束")
字符串
str2 = '我的外号是"大西瓜"'
print(str2[5])
for char in str2:
print(char)
hello_str = "hello hello"
print(len(hello_str))
print(hello_str.count("llo"))
print(hello_str.index("llo"))
print(hello_str.index("abc"))
hello_str = "hello world"
print(hello_str.startswith("Hello"))
print(hello_str.endswith("world"))
print(hello_str.find("llo"))
print(hello_str.find("abc"))
print(hello_str.replace("world", "python"))
print(hello_str)
poem = ["\t\n登鹳雀楼",
"王之涣",
"白日依山尽\t\n",
"黄河入海流",
"欲穷千里目",
"更上一层楼"]
for poem_str in poem:
print("|%s|" % poem_str.strip().center(10, " "))
poem_str = "登鹳雀楼\t 王之涣 \t 白日依山尽 \t \n 黄河入海流 \t\t 欲穷千里目 \t\t\n更上一层楼"
print(poem_str)
poem_list = poem_str.split()
print(poem_list)
result = " ".join(poem_list)
print(result)
转载请注明原文地址:https://blackberry.8miu.com/read-17471.html