#!/usr/bin/env python
# -*- coding:utf-8 -*-
'''
day04作业
'''
'''
1. 简述解释性语言和编译型语言的区别?
'''
#解释型语言是把代码逐行解释成二进制文件,计算机逐行执行,类似于同声传译
# 编译型语言是直接把代码编译成二进制文件,交给计算机一次性执行,类似于翻译
'''
2. 列举你了解的Python的数据类型?
'''
# int \ float \ bool \ string \ list \ tuple \ dic \ None \
'''
3. 写代码,有如下列表,按照要求实现每一个功能。
li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
- 计算列表的长度并输出
'''
# li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
# print(len(li))
'''
- 请通过步长获取索引为偶数的所有值,并打印出获取后的列表
'''
li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
print(li[::2])
'''
- 列表中追加元素"seven",并输出添加后的列表
'''
# li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
# li.append('seven')
# print(li)
'''
请在列表的第1个位置插入元素"Tony",并输出添加后的列表
'''
# li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
# li.insert(0,'Tony')
# print(li)
'''
请修改列表第2个位置的元素为"Kelly",并输出修改后的列表
'''
# li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
# li[1]='Kelly'
# print(li)
'''
- 请将列表的第3个位置的值改成 "太白",并输出修改后的列表
'''
# li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
# li[2]="太白"
# print(li)
'''
- 请将列表 l2=[1,"a",3,4,"heart"] 的每一个元素追加到列表li中,并输出添加后的列表
'''
# li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
# l2=[1,"a",3,4,"heart"]
# for x in l2:
# li.append(x)
# print(li)
'''
- 请将字符串 s = "qwert"的每一个元素添加到列表li中,一行代码实现,不允许循环添加。
'''
li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
# s = "qwert"
# li.extend(s) #字符串extend
# print(li)
##补充
# a = [1,"a",2,"i"]#列表extend
# li.extend(a)
# print(li)
#
# q = (1,"a",2,"i")#元组extend
# li.extend(q)
# print(li)
'''
- 请删除列表中的元素"ritian",并输出添加后的列表
'''
# li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
# li.remove("ritian")
# print(li)
'''
- 请删除列表中的第2个元素,并输出删除元素后的列表
'''
# li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
# li.pop(1)
# print(li)
'''
- 请删除列表中的第2至第4个元素,并输出删除元素后的列表
'''
# li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
# del li[2:]
# print(li)
'''
4. 请用三种方法实现字符串反转 name = "小黑半夜三点在被窝玩愤怒的小鸟"(步长、while、for)
'''
# way1
# name = "小黑半夜三点在被窝玩愤怒的小鸟"
# print(name[::-1])
# way2
# name = "小黑半夜三点在被窝玩愤怒的小鸟"
# for x in name[::-1] :
# print(x,end="")
# way3
# name = "小黑半夜三点在被窝玩愤怒的小鸟"
# index=len(name)
# while index>0 :
# new_name=name[index-1]
# print(new_name,end="")
# index-=1
'''
5. 写代码,有如下列表,利用切片实现每一个功能
li = [1, 3, 2, "a", 4, "b", 5,"c"]
-1 通过对li列表的切片形成新的列表 [1,3,2]
-2 通过对li列表的切片形成新的列表 ["a",4,"b"]
-3 通过对li列表的切片形成新的列表 [1,2,4,5]
-4 通过对li列表的切片形成新的列表 [3,"a","b"]
-5 通过对li列表的切片形成新的列表 [3,"a","b","c"]
-6 通过对li列表的切片形成新的列表 ["c"]
-7 通过对li列表的切片形成新的列表 ["b","a",3]
'''
#1
# li = [1, 3, 2, "a", 4, "b", 5,"c"]
# list1=li[:3]
# print(list1)
#2
# list2=li[3:6]
# print(list2)
#3
# list3=li[::2]
# print(list3)
#4
# list4=li[1:6:2]
# print(list4)
#5
# list5=li[1::2]
# print(list5)
#6
# list6=li[-1:]
# print(list6)
#7
# list7=li[-3::-2]
# print(list7)
'''
6. 请用代码实现循环输出元素和值:users = ["武沛齐","景女神","肖大侠"] ,如:
0 武沛齐
1 景女神
2 肖大侠
'''
# way1
# users = ["武沛齐","景女神","肖大侠"]
# for x in range(3):
# print("%d %s"%(x,users[x]))
# way2
# users = ["武沛齐","景女神","肖大侠"]
# for x in enumerate(users):
# print(x,type(x))
'''
7. 请用代码实现循环输出元素和值:users = ["武沛齐","景女神","肖大侠"] ,如:
1 武沛齐
2 景女神
3 肖大侠
'''
# users = ["武沛齐","景女神","肖大侠"]
# for x in range(1,4):
# print("{:<3} {:<2}".format(x,users[x-1]))
'''
8. 写代码,有如下列表,按照要求实现每一个功能。
lis = [2, 3, "k", ["qwe", 20, ["k1", ["tt", 3, "1"]], 89], "ab", "adv"]
-1 将列表lis中的"k"变成大写,并打印列表。
-2 将列表中的数字3变成字符串"100"
-3 将列表中的字符串"tt"变成数字 101
-4 在 "qwe"前面插入字符串:"火车头"
'''
# lis = [2, 3, "k", ["qwe", 20, ["k1", ["tt", 3, "1"]], 89], "ab", "adv"]
# lis[2]=lis[2].upper()
# print(lis)
#
# lis[1]="100"
# print(lis)
#
# lis[3][2][1][0]="100"
# print(lis)
# lis = [2, 3, "k", ["qwe", 20, ["k1", ["tt", 3, "1"]], 89], "ab", "adv"]
# lis[3].insert(0,"火车头")
# print(lis)
'''
9. 写代码实现以下功能
- 如有变量 googs = ['汽车','飞机','火箭'] 提示用户可供选择的商品:
0,汽车
1,飞机
2,火箭
- 用户输入索引后,将指定商品的内容拼接打印,如:用户输入0,则打印 您选择的商品是汽车
'''
# googs = ['汽车','飞机','火箭']
# print("""
# 可以选择的商品:
# 0,汽车
# 1,飞机
# 2,火箭
# """)
# index=int(input("请输入商品序号:"))
# if index<3:
# print("您选择的商品是:{}".format(googs[index]))
# else:
# print("输入有误")
'''
10. 请用代码实现
li = "alex"
利用下划线将列表的每一个元素拼接成字符串"a_l_e_x"
'''
# li = "alex"
# li2="_".join(li)
# print(li2)
'''
11. 利用for循环和range找出 0 ~ 100 以内所有的偶数,并追加到一个列表。
'''
# li=[]
# for x in range(0,100):
# if x%2==0:
# li.append(x)
# print(li)
'''
12. 利用for循环和range 找出 0 ~ 50 以内能被3整除的数,并追加到一个列表。
'''
# li=[]
# for x in range(0,50):
# if x%3==0:
# li.append(x)
# print(li)
'''
13. 利用for循环和range 找出 0 ~ 50 以内能被3整除的数,并插入到列表的第0个索引位置,最终结果如下:
[48,45,42...]
'''
# li=[]
# for x in range(50):
# if x%3==0:
# li.append(x)
# li.reverse()
# print(li)
'''
14. 查找列表li中的元素,移除每个元素的空格,并找出以"a"开头,并添加到一个新列表中,最后循环打印这个新列表。
li = ["TaiBai ", "alexC", "AbC ", "egon", " riTiAn", "WuSir", " aqc"]
'''
# li = ["TaiBai ", "alexC", "AbC ", "egon", " riTiAn", "WuSir", " aqc"]
# li2=[]
# for x in li:
# y=x.strip() #字符串不能不修改,修改要新增变量
# if y.startswith("a"):
# li2.append(y)
# print(li2)
'''
15. 判断是否可以实现,如果可以请写代码实现。
li = ["alex",[11,22,(88,99,100,),33] "WuSir", ("ritian", "barry",), "wenzhou"]
-1 请将 "WuSir" 修改成 "武沛齐"
-2 请将 ("ritian", "barry",) 修改为 ['日天','日地']
-3 请将 88 修改为 87
-4 请将 "wenzhou" 删除,然后再在列表第0个索引位置插入 "文周"
'''
#1
# li = ["alex",[11,22,(88,99,100,),33], "WuSir", ("ritian", "barry",), "wenzhou"]
# li[2]="武沛齐"
# print(li)
#2
# li = ["alex",[11,22,(88,99,100,),33], "WuSir", ("ritian", "barry",), "wenzhou"]
# li[3]=['日天','日地']
# print(li)
#3
# li = ["alex",[11,22,(88,99,100,),33], "WuSir", ("ritian", "barry",), "wenzhou"]
# li[1][2]=(87,99,100,)
# print(li)
#4
# li = ["alex",[11,22,(88,99,100,),33], "WuSir", ("ritian", "barry",), "wenzhou"]
# li.pop()
# li.insert(0,"文周")
# print(li)