标签:alt 32位 默认 鼠标 -o 组成 机器 enter git
运算符 |
1、算数运算:
2、比较运算:
3、赋值运算:
4、逻辑运算:
5、成员运算:
6、身份运算:
7、位运算:
8、运算符优先级:
1 a = 60 # 60 = 0011 1100 2 b = 13 # 13 = 0000 1101 3 c = 0 4 5 c = a & b; # 12 = 0000 1100 6 print "Line 1 - Value of c is ", c 7 8 c = a | b; # 61 = 0011 1101 9 print "Line 2 - Value of c is ", c 10 11 c = a ^ b; # 49 = 0011 0001 12 print "Line 3 - Value of c is ", c 13 14 c = ~a; # -61 = 1100 0011 15 print "Line 4 - Value of c is ", c 16 17 c = a << 2; # 240 = 1111 0000 18 print "Line 5 - Value of c is ", c 19 20 c = a >> 2; # 15 = 0000 1111 21 print "Line 6 - Value of c is ", c
更多运算符内容:猛戳 <<<
基本数据类型 |
2 是一个整数的例子。
长整数 不过是大一些的整数。
3.23和52.3E-4是浮点数的例子。E标记表示10的幂。在这里,52.3E-4表示52.3 * 10-4。
(-5+4j)和(2.3-4.6j)是复数的例子。
int(整型)
"hello world"
1 name = ‘liulei‘ 2 print(‘i am %s‘ % name ) 3 print(‘i am ‘+ name) 4 5 # 二者均输出 i am liulei
PS: 字符串是 %s;整数 %d;浮点数%f
在使用input输入时,程序默认输入的为字符串
4、列表
基本操作:
数据类型详情请猛戳:这里 <<<
1 x = [1,2,3] 2 x = list(‘小一同学‘) 3 range(5)表示从0—4五个数,多用于whlie循环中 4 list(range(5)) = [0,1,2,3,4] 5 输出指定数据 6 x = [0:2]表示输出0、1所表示的数/[:2]/[0:2]/[:]/[::k]k表示隔多少数输出一个 7 len(x) # 表示输出列表长度 8 min(x) # 最小值 9 min(x) 10 sum(x) 11 x.index(‘小一同学‘) # 检索某个元素第一次出现时的位置下标 12 # 检索某个元素出现的次数 13 x.count(1)
5、元组
6、字典(无序)
常用操作:
PS:循环,range,continue 和 break
附加 |
1 list = [11,22,33,44] 2 for item in list: 3 print item
1 list = [11,22,33] 2 for k,v in enumerate(list, 1): 3 print(k,v) 4 inp = input(‘请输入需要查找数字的序号:‘)#input输出的为字符串格式 5 inp_num = int(inp)# 将字符串型转换为整型 6 print(list[inp_num-1]) 7 #更改k的值并没有更改enumerate对应的值,5、6行为修改对应的值
3、range 指定范围,生成指定的数字
1 print range(1, 10) 2 # 结果:[1, 2, 3, 4, 5, 6, 7, 8, 9] 3 4 print range(1, 10, 2) 5 # 结果:[1, 3, 5, 7, 9] 6 7 print range(30, 0, -2) 8 # 结果:[30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2]
练习 |
一、元素分类
有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。
即: {‘k1‘: 大于66的所有值, ‘k2‘: 小于66的所有值}
1 list = [11,22,33,44,55,66,77,88,99,111,222,333] 2 temp1 = [] 3 temp2 = [] 4 for i in list: 5 if i < 66: 6 temp1.append(i) 7 else: 8 temp2.append(i) 9 key1 = {‘key1‘:temp1} 10 key2 = {‘key2‘:temp2} 11 print(key1,key2)
1 list = ["alec", " aric", "Alex", "Tony", "rain"] 2 tuple = ("alec", " aric", "Alex", "Tony", "rain") 3 dict = {‘k1‘: "alex", ‘k2‘: ‘ aric‘, "k3": "Alex", "k4": "Tony"} 4 item1 = [] 5 item2 = [] 6 item3 = [] 7 for i in list: 8 j = i.strip() 9 if(j.startswith(‘a‘) or j.startswith(‘A‘)) and j.endswith(‘c‘) 10 item1.append(i) 11 for i in tuple: 12 m = i.strip()#移除空白 13 if(m.startswith(‘a‘) or m.startswith(‘A‘)) and m.endswith(‘c‘) 14 item2.append(i) 15 for i in dict: 16 n =dict[i].strip() 17 if(n.startswith(‘a‘) or n.startswith(‘A‘)) and n.endswith(‘c‘) 18 item3.append(dict[i]) 19 #把适合的值通过append函数添加到item中去 20 key1 = {‘list‘:item1} 21 key2 = {‘tuple‘:item2} 22 key3 = {‘dict‘:item3} 23 print(key1,key2,key3)
1 list = ["J31", "T99", ‘M50‘, ‘98K‘, ‘F22‘, ‘F35‘] 2 for i,j in enumerate(list,1)#从序号1开始 3 print(i,j) 4 while True: 5 temp = input(‘请输入需要的武器‘) 6 temp_num = int(temp) 7 prnt(‘list[temp_num-1]‘)
功能要求:
1 shopping_cart = [] 2 salary = int(input("请输入用户初始总资产: ")) 3 #salary = 2000 4 goods = [ 5 {"name": "电脑", "price": 3000}, 6 {"name": "鼠标", "price": 103}, 7 {"name": "游艇", "price": 200000}, 8 {"name": "美女", "price": 998}, 9 ] 10 for i in enumerate(goods): 11 index = i[0] # 序号 12 p_list = i[1] # 商品清单 13 p_name_list = p_list.get(‘name‘) # 商品名称列表 14 p_price_list = p_list.get(‘price‘) # 商品价格列表 15 print(index, ":", p_name_list,‘->‘, p_price_list) 16 17 while True: 18 choice = input("请输入需要商品的序号:").strip() 19 if choice.isdigit(): # 如果选择为正整数 20 choice = int(choice) # 输入为数字 21 if choice < len(goods) and choice >= 0: # 选择小于列表长度大于0时 22 p_item = goods[choice] # 加入购物车 23 p_name = p_item.get(‘name‘) 24 p_monery = p_item.get(‘price‘) 25 if p_monery <= salary: # 如果商品价格小于等于余额 26 shopping_cart.append(p_item) # 加入购物车 27 salary -= p_monery # 结算 28 print("购买的商品\033[32m:%s\033[0m已加入到购物车".center(40, ‘-‘) % p_name) 29 for p_item in shopping_cart: 30 print(p_name, p_monery) 31 print("您的余额为\033[31m:%s\033[0m元" % salary) 32 else: 33 print("您的余额不足,差%s元" % (abs(p_monery - salary))) 34 else: 35 print("没有此件商品!") 36 else: 37 print("参数错误") 38 if choice == "q" or choice == "quit": 39 cost = 0 40 print("您购买的商品清单如下:") 41 for p in shopping_cart: 42 print(p_name, p_monery) 43 cost += p_monery 44 print("\033[32m消费总金额:", cost, "元\033[0m") 45 print("\033[32m您的余额为:", salary, "元\033[0m") 46 break
标签:alt 32位 默认 鼠标 -o 组成 机器 enter git
原文地址:http://www.cnblogs.com/liulei-primerscript/p/7471688.html