第四天,for循环,格式化输出,占位符,pycharm安装.列表处理
字符格式化输出
占位符 %s s = string
%d d = digit 整数
%f f = float 浮点数,约等于小数
列表,元组
查
索引(下标) ,都是从0开始
切片
.count 查某个元素的出现次数
.index 根据内容找其对应的位置
"haidilao ge" in a
增加
a.append() 追加
a.insert(index, "内容")
a.extend 扩展
修改
a[index] = "新的值"
a[start:end] = [a,b,c]
删除
remove("内容")
pop(index) 不指定索引则删除最后一个
del a, del a[index]
a.clear() 清空
排序
sort ()
reverse()
身份判断
>>> type(a) is list
True
>>>
pycharm中同时注释多行代码快捷键:
代码选中的条件下,同时按住 Ctrl+/,被选中行被注释,再次按下Ctrl+/,注释被取消
Name=input("Input Your Name:")
Age=input("Input Your Age:")
Salary=input("Input Your Salary:")
if Age.isdigit():
Age=int(Age)
else:
print('You must input a number.')
if Salary.isdigit():
Salary=int(Salary)
else :
print('Salary must input with digit.')
msg='''
---------info of %s-----------
Name=%s
Age=%d
Salary=%f
You will retire in %d years.
------------END---------------
'''%(Name,Name,Age,Salary,65-Age)
print(msg)
#for循环
for i in range(3):
print("loop:",i)
#####range(1,101,2),第一个数表示循环起始值,第二个数为终止值,前包含后不包含,第三个数,步长;
####用户密码验证作业
_user="zoe"
_password="123123"
for i in range(3):
user = input("USER:")
password = input("PassWord:")
if user==_user and password==_password:
print("%s Welcome to login."%_user)
break #####如果break,不执行for循环的else语句
else:
print("Invalid user or password.")
continue
else: #####如果没有break,for循环正常执行完毕,则执行else语句
print("User %s is Blocked." % _user)
########while循环实现3次登陆
_user="zoe"
_password="123123"
counter = 0
while counter<3:
user = input("USER:")
password = input("PassWord:")
if user==_user and password==_password:
print("%s Welcome to login."%_user)
break
else:
print("Invalid user or password.")
counter += 1
if counter == 3:
try_again = input("还想玩吗?[Y/N]:")
if try_again == "Y":
counter = 0
else:
print("User %s is Blocked." % _user)
##########列表切片[::] ":"前第一个数是起始位置,第二个数是终止位置,第三个数是步长,可以是负数,表示倒过来数
a = ['a','b','c','d','e']
print(a[1:-1:1])##从左到右倒数第二个一个一个取
a = ['a','b','c','d','e']
print(a[1:-1:1])##从左到右倒数第二个一个一个取
b = a[len(a)::-1]
print(b)
a.append("f") #####增加元素至末尾
a.append("f")
print(a)
a.insert(5,'insert') ######插入倒表中第5位置
print(a)
a.remove(a[1]) #######删除a列表第1号元素
>>> a.insert(2,'b')
>>> a
['a', 'c', 'b', 'd', 'e', 'f']
>>> a.pop(2) #########删除索引位置元素,并返回删除内容
'b'
>>> a
['a', 'c', 'd', 'e', 'f']
>>>
a[2] = 'b' ######修改索引为2位置的元素
a[1:3] = ['a','b'] ########切片修改多个元素
del a[0] #######删除a列表索引0位置元素
del a ###########删除a列表
count:计算某元素出现次数
t=['to', 'be', 'or', 'not', 'to', 'be'].count('to')
print(t)
extend ##########扩展,把b列表扩展至a列表末尾
a = [1, 2, 3]
b = [4, 5, 6]
a.extend(b)
print(a)
print(b)
index
index # #####################根据内容找位置
a = ['wuchao', 'jinxin', 'xiaohu','ligang', 'sanpang', 'ligang', ['wuchao', 'jinxin']]
first_lg_index = a.index("ligang")
print("first_lg_index",first_lg_index)
little_list = a[first_lg_index+1:]
second_lg_index = little_list.index("ligang")
print("second_lg_index",second_lg_index)
second_lg_index_in_big_list = first_lg_index + second_lg_index +1
print("second_lg_index_in_big_list",second_lg_index_in_big_list)
print("second lg:",a[second_lg_index_in_big_list])
######################一个列表中两个同名元素取位置方法
#reverse ##############列表倒序排列
a = ['wuchao', 'jinxin', 'xiaohu','ligang', 'sanpang', 'ligang']
a.reverse()
print(a)
['ligang', 'sanpang', 'ligang', 'xiaohu', 'jinxin', 'wuchao']
#sort 排序 ######将列表中的元素排序
x = [4, 6, 2, 1, 7, 9]
x.sort(reverse=True)
print(x)#[1, 2, 4, 6, 7, 9]
a = ['wuchao', 'jinxin', 'Xiaohu','Ligang', 'sanpang', 'ligang']
a.sort()
print(a)
print(a.count("haidilao ge"))
print(a.pop())
print(a)
##########格式化输出
st='hello kitty {name} is {age}'
print(st.format(name='alex',age=37)) # 格式化输出的另一种方式 待定:?:{}
print('/n My Name Is Zoe/n'.stip()) #####去掉空格和换行
print('/n My Name Is Zoe/n'.lstip()) ####去掉左边的空格和换行
print('/n My Name Is Zoe/n'.rstip()) ####去掉右边的空格和换行
print('My title title'.split('i',1)) ######用字母i分割字符串,最后参数1为分割1次
print('My title title'.title()) ######变为标题,首字母大写