__author__ = 'qq593'#!/usr/bin/env python#-*- coding:utf-8 -*-n1 =123n2 =456print(n1+n2)print(n1.__add__(n2))n3=4print(n3.bit_length()) #取比特位 4为100 ->3
__author__ = 'qq593'#!/usr/bin/env python#-*- coding:utf-8 -*-#environment:python3.6s = "alex"# print(s[0]) #a# print(s[1]) #l# print(s[2]) #e# print(s[3]) #x# print(s[0:2])#al 切片[a,b] 取出来的为 0<= 结果位置
__author__ = 'qq593'#!/usr/bin/env python#-*- coding:utf-8 -*-name_list=['alex','eric','rain']'''#索引print(name_list[0]) #->alex#切片print(name_list[0:2]) #->['alex', 'eric']#lenprint(name_list[2:len(name_list)]) #大于等于2,小于3,结果为rain -> ['rain']#forfor i in name_list: print (i)'''"""#其他功能#append 追加元素name_list.append('seven')name_list.append('seven')name_list.append('seven')print(name_list)#统计出现次数print(name_list.count('seven')) #->3#iterable 可迭代的#extend 扩展列表temp=['test','haha']name_list.extend(temp)print(name_list) #->['alex', 'eric', 'rain', 'seven', 'seven', 'seven', 'test', 'haha']#index 定位元素位置print(name_list.index('eric'))#insert 在某个位置前插入name_list.insert(1,'SB')print(name_list)#pop 删除列表最后一个值,并返回最后一个元素print(name_list.pop())#remove 移除某个元素name_list.remove('seven')name_list.remove('seven')print(name_list)#reverse 反转name_list.reverse()print(name_list)#排序 按照首字符对应的ASCII码由小到大排序name_list.sort()print(name_list)"""#del 删除指定索引位置print(name_list)del name_list[1]print(name_list)
__author__ = 'qq593'#!/usr/bin/env python#-*- coding:utf-8 -*-#1.元组和列表几乎是一样的#2.元组是不可修改的,列表是可修改的#########元组########name_tuple = ('alex','eric')#索引print(name_tuple[0])#lenprint(name_tuple[len(name_tuple)-1])#切片print(name_tuple[0:1]) #0<= <1 取出第0个,alex#forfor i in name_tuple: print(i)#count 计算元素出现次数print(name_tuple.count('alex'))#index 获取指定元素索引位置print(name_tuple.index('alex'))
__author__ = 'qq593'#!/usr/bin/env python#-*- coding:utf-8 -*-#字典的每一个元素都是一个键值对。user_info = { 'name':'alex', 'age':73, 'gender':'M'}# #索引# print(user_info['age']) #->73## #没有切片### #for循环 默认输出key# for i in user_info:# print(i)## a = user_info.keys() #获取所有的key# b = user_info.values() #获取所有key对应的值# c = user_info.items() #获取所有键值对# print(a)# print(b)# print(c) #dict_items([('name', 'alex'), ('age', 73), ('gender', 'M')])## for i in user_info.keys():# print(i)### for i in user_info.values():# print(i)### for i in user_info.items():# print(i)for k,v in user_info.items(): #k,v分别获取key,value print(k) print(v)#clear 清除所有内容# user_info.clear()# print(user_info)#get 根据key获取值,如果key不存在,可指定默认值:val=user_info.get('age')print(val)#索引不存在则报错,而get的key不存在则可指定返回值val = user_info.get('dslfk',"不存在")print(val)#python3.6没有has_key方法,但是可实现:ret = 'age' in user_info.keys()print(ret)#pop 获取并移除指定key项#popitem 获取并移除最后一个#update:print(user_info)test = { 'a1':123, 'a2':456}user_info.update(test)print(user_info)#deldel test['a1']print(test)
__author__ = 'qq593'#!/usr/bin/env python#-*- coding:utf-8 -*-#1.enumerate# l1=['电脑','鼠标垫','U盘','游艇']# for item in l1:# print(item)## inp = input('请输入商品:')"""#enumerate 自动给列表加序号key(默认从0开始)l1=['电脑','鼠标垫','U盘','游艇']print(l1.index('电脑'))for key,item in enumerate(l1): print(key,item)inp = input('请输入商品序号:')#字符串转换成intprint(l1[int(inp)])#如果enumerate指定起始值不从0开始,从1开始,则输入1时仍然是显示电脑,我们需要在print(l1[int(inp)])中改为print(l1[int(inp)-1])""""""#python2.7中#range,xrange#range是用来获取指定范围内的数,range(0,10000000)马上创建从0-10000000的内存空间#xrange暂时不创建,只有在for 循环中创建#Python3中range等同于2.7中的xrangeprint(range(1,10)) #python3.6中->range(1, 10)#print(range(1,10)) #python2.7中->[1,2,3,4,5,6,7,8,9]for i in range(1,10,2): #range中可插入第三个参数,表示步长 print(i)for i in range(10,1,-2): print(i)"""li = ['alex','eric']#打印所有索引值#方法一ret = enumerate(li)for i,j in ret: print(i)#方法二:leee =len(li)for i in range(0,leee): print(i,li[i])