数组
# Author:FengFeng
name = ["1","2","3","4","5","6"]
print(name)
print(name[0])
print(name[1:4]) #包括头不包括尾
print(name[-1]) #取最后一个值
print(name[-3:])
print(name[-3:-1])
name.append("7") #追加
print(name)
name.insert(1,"fengfeng")
print(name)
name.insert(3,"xiaorong")
print(name)
name[2] = "newstring" #更改
print(name)
name.remove("fengfeng") #删除
print(name)
del name[2]
print(name)
name.pop() #删除最后一个(默认)
name.pop(3)
print(name)
print(name.index("5"))
print(name.count("4"))
#反转
name.reverse()
#排序
name.sort()
name2 = [1,2,3,4]
#扩展(合并)
name.extend(name2)
for i in name:
print(i)
print(name)
print(name2[0:-1:2])
name2 = name.copy()
print(name2)
字典
# Author:FengFeng
info = {
'1':'fengfeng',
'2':'lele',
'3':'feifei'
}
print('5' in info)#判断是否存在
print(info.get('5'))#查找
print(info,info['1'])
info['1'] = 'juhua'#如果key存在就修改,如果不存在就自动追加一个
print(info)
del info['1']
#info.popitem()#随便删除一个
info.pop('2')
print(info)
例子
# Author:FengFeng
product_list = [
('iphone',5800),
('mac pro',9800),
('car',100000),
('watch',20200),
('coffee',30),
('alex python',120)
]
shopping_list = []
salary = input("input your salary:")
if salary.isdigit():
salary = int(salary)
while True:
for index, item in enumerate(product_list):
#print(product_list.index(item),item)
print(index,item)
user_choice = input("选择要买什么:")
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice < len(product_list) and user_choice >=0:
p_item = product_list[user_choice]
if p_item[1] <= salary: #买得起
shopping_list.append(p_item)
salary -= p_item[1]
print("还剩 %s 元" %salary)
else:
print("金额不足")
else:
print('商品不存在')
exit()
elif user_choice == 'q':
print('exit...')
exit()
else:
print("invalid option")
字符串
# Author:FengFeng
name = 'fengfeng name is {name} and {year} old'
print(name.capitalize())#首字母大写
print(name.count("f"))#字符出现的次数
print(name.center(50,'-'))#打印50个字符,不够用 - 补上
print(name.endswith("g"))#判断以什么结尾
print(name.expandtabs())
print(name.find("g"))
print(name.format(name='alex',year=18))
print(name.format_map({'name':'sss','year':12}))
print(name.isalnum())#包含26个英文字符和0-9数字判断是否
print(name.isalpha())#是否是纯英文字符
print(name.isdecimal())#进制
print(name.isdigit())#是否是整数
print(name.isidentifier())#判断是否是一个合法的标识符 是否是合法的变量名
print(name.islower())#是否是小写
print(name.isnumeric())#判断是否是数字
print(name.isspace())#判断是否时空格
print(name.istitle())#每个首字母大写则为true
print(name.isupper())#是否是大写
print(name.join("--"))
#数组转为字符串
print(','.join(['1','2','3']))
print(name.ljust(50,'*'))#长度为50不够的话追加*
print(name.rjust(50,'*'))#相反
print(name.lower())#大写变为小写
print(name.upper())#小写变为大写
print('\nssss'.rstrip())#去除右边空格
print('ssss\n'.lstrip())#去除左边空格
print('ssss\n'.strip())#去掉空格全部
#p = str.maketrans()
print(name.replace('f','F'))
print(name.rfind('e'))
print(name.split('n'))#按照字符转为列表
print(name.splitlines())#按照换行
print(name.swapcase())
copy
# Author:FengFeng
import copy
person = ['name',['saving',100]]
'''
p1 = copy.copy(person)
p2 = person[:]
p3 = list(person)
'''
p1 = copy.deepcopy(person)
p2 = copy.deepcopy(person)
p1[0] = "alex"
p2[0] = "fengjie"
p1[1][1] = 50
print(p1)
print(p2)
#元组
fengzi = ('a','b','c')
print(fengzi,fengzi.count(1))
sys
# Author:FengFeng
import sys
import os
#先到当前目录寻找路径,没有path方法,此时会报错。但是3.x并未报错
print(sys.path) #打印环境变量
print(sys.argv) #打印相对路径
cmd_res = os.system("dir")
cmd_res = os.popen("dir").read()
print("-->",cmd_res) #0 代表命令执行成功。。。不保存结果
#os.mkdir("new_dir")
#pyc是什么?
#相当于是缓存文件,当运行程序的时候,会先去这个文件中找,执行pyc。如果修改了源代码,就会再次生成(更新pyc),然后程序运行,再次执行pyc.
#三元运算
a,b,c = 1,3,5
d = input("number:")
d = a if a > b else c
print(d)
msg = "fengfeng ai ma"
print(msg)
#转化为二进制
print(msg.encode(encoding="utf-8"))