Python运维开发之路《数据类型》

一. python数据类型

  python的五大基本数据类型,数字、字符串、列表、元组、字典;其他数据类型,类型type、Null、文件、集合、函数/方法、类、模块。

1.数字

 1 ①整型
 2 十进制转八进制
 3 oct(10)
 4 十进制转十六进制
 5 hex(10)
 6 ②长整型(目前python3里已经取消)
 7 >>> type(2**31)
 8 <type 'long'>
 9 >>> type(2**30)
10 <type 'int'>
11 ③布尔
12 >>> bool(0)
13 False
14 >>> bool(1)    #非零数字自带的布尔值都是True
15 True
16 >>> bool("hello")    #非空的字符串布尔值都是True
17 True
18 ④浮点
19 浮点数是属于有理数中某特定子集的数的数字表示,在计算机中用以近似表示任意某个实数。
20 ⑤复数
21 Python支持复数,复数由实数部分和虚数部分构成,可以用a + bj,或者complex(a,b)表示,复数的实部a和虚部b都是浮点型。

 2. 字符串

 1 msg = 'hello world'                           #定义变量msg
 2 
 3 >>> print(msg.count('l',4,10))                  #统计次数;计算字符串出现的次数,顾头不顾尾
 4 1
 5 
 6 >>> print(msg.capitalize)                       #每个单词首字母大写
 7 Hello world
 8 
 9 >>> print(msg.center(50,'*'))                   #用50个字符打印msg,多余部分用*表示
10 *******************hello world********************
11 
12 >>> print(msg.endswith('d'))                    #查看msg变量是否以d结尾
13 True
14 
15 >>> print(msg.expandtabs(tabsize=20))           #tab键用20个字符表示
16 
17 >>> print('{} {}'.format('name','agr'))         #赋值给{},必须按照位置一对一赋值
18 name agr
19 print('{0} {1} {0}'.format('name','agr'))
20 name agr
21 
22 >>> print(msg.index('l'))                       #查看索引位如果查不到就报错
23 2
24 
25 >>> print(msg.find('b'))                        #查找索引位如果查不到用-1表示
26 -1
27 
28 >>> print(msg.isalnum())                        #字母和数字都行
29 False
30 
31 >>> print(msg.isalpha())                        #只能是字母
32 False
33 
34 >>> print(msg.isdecimal())                      #是否是十进制
35 False
36 
37 >>> print(msg.isdigit())                        #是否是整型数字
38 False
39 
40 >>> print(msg.isnumeric())                      #是否是数字
41 False
42 
43 >>> print(msg.isidentifier())                   #是否一个单词包含内置关键字
44 False
45 
46 >>> print(msg.islower())                        #是否是小写(全部是小写)
47 True
48 
49 >>> print(msg.isspace())                        #是否是空格
50 False
51 
52 >>> print(msg.istitle())                           #是否是标题(一个单词只有首字母大写)
53 False
54 
55 >>> print(msg.isupper())                        #是否是大写
56 False
57 
58 >>> print(msg.ljust(50,'*'))                    #字符串内容左对齐
59 hello*********************************************
60 
61 >>> print(msg.rjust(50,'#'))                    #字符串内容右对齐
62 #############################################hello
63 
64 >>> msg = 'HELLO WORLD'
65 >>> print(msg.lower())                           #将大写转换成小写
66 hello world
67 
68 >>> msg = 'hello world'
69 >>> print(msg.upper())                          #将小写转换成大写
70 HELLO WORLD
71 
72 >>> msg = '	Hello World'
73 >>> print(msg)
74         Hello World
75 >>> print(msg.lstrip())                            #去掉左边空格
76 Hello World
77 
78 >>> print(msg.rstrip())                            #去掉右边空格
79 Hello World
80 
81 >>> print(msg.zfill(20))                          #右对齐,不够的位数0填充,可指定宽度
82 000000000hello world

3. 列表

 1 增加
 2 names.append('zhangzhenxing')                  #追加
 3 names.insert(2,'liusijia')                              #插入
 4 
 5 删除
 6 names.remove('zhangzhenxing')                  #删除值
 7 del names[3]                                             #通过下标删除
 8 names.pop[3]                                            #默认删除最后一个,可以通过下标删除
 9 
10 修改
11 names[2] = '王旭'                                        #直接修改下标
12 
13 查询
14 print(names[2])                                          #下标查看
15 print(names[0::2])
16 print(names[-3:])
17 print(names[:3])
18 names.index('wangxu')                                #取这个元素的下标
19 
20 统计
21 count   print('count',names.count('wudonghang'))    #统计出现次数
22 clear   names.clear                                              #清空列表
23 extend  names.extend(n2)                                  #合并列表
24 reverse names.reverse()                                     #反转
25 sort    names.sort()                                            #通过ascii码表排序
26 copy    names.copy()                                          #拷贝列表
27             

 二、购物车程序

1. 流程图

2.程序

 1 commodity_list = [['Iphone',5088],['Mac Pro',12888],['Bike',2088],['Starbucks Coffee',33],['GoogleSRE Book',90]]
 2 shopping_cart = []
 3 
 4 salary = input('>>>>>请输入薪水:')
 5 if salary.isdigit():
 6     salary = int(salary)
 7     while True:
 8         for index,list in enumerate(commodity_list):
 9             print(index,list)                                  #打印列表元素,并标识下标
10         product_id = input('>>>>>请输入商品编号(q退出):')        #输入元素下标
11         if product_id.isdigit():                               #判断是否为数字,true继续,false提示输入错误
12             product_id = int(product_id)
13             if product_id >= 0 and len(commodity_list):
14                 commodity = commodity_list[product_id]
15                 if commodity[1] <= salary:
16                     shopping_cart.append(commodity)
17                     salary -= commodity[1]
18                     print('您好!已将商品33[31;1m%s33[0m添加到购物车,已付款33[31;1m%s33[0mRMB,账户余额33[31;1m%s33[0mRMB!'%(commodity,commodity[1],salary))
19                 else:
20                     print('您好!您的余额[%sRMB]不足以购买此商品!'%salary)
21         elif product_id == 'q':
22             print('----------shopping list-----------')
23             shopping_cart2 = []
24             for i in shopping_cart:                                #去重,追加到shopping_cart2
25                 if not i in shopping_cart2:
26                     shopping_cart2.append(i)
27             for index,j in enumerate(shopping_cart2):
28                 sum_list = j[1] * shopping_cart.count(j)            #单价乘以数量
29                 print(index,j,'*',shopping_cart.count(j),'=',sum_list)
30             exit(print('
谢谢光临!您的余额剩余:33[31;1m%s RMB33[0m'%salary,'
---------------END----------------'))
31     else:
32         print("错误!您输入的商品编码不存在!")
33 else:
34     print('你是猴子请来的都比么,想打劫啊!!!')

三、三级菜单

1.程序

 1 date = {
 2         '北京':{
 3                 '昌平':{
 4                         '沙河':['oldboy','阿泰包子'],
 5                         '天通苑':['链家地产','我爱我家']
 6                         },
 7                 '朝阳':{
 8                         '双井':['富力城','UME'],
 9                         '三里屯':['阿迪达斯','优衣库']
10                         },
11                 '海淀':{
12                         '中关村':['E世界','新中关'],
13                         '西二旗':['百度大厦','神码大厦']
14                         },
15         },
16         '河北':{
17                 '石家庄':{
18                         '新华区':['革新街道','新华路街道'],
19                         '桥西区':['东里街道','中山路街道']
20                         },
21                 '廊坊':{
22                         '大厂':['大厂镇','夏垫镇'],
23                         '香河':['淑阳镇','安平镇']
24                         },
25                 '唐山':{
26                         '路南区':['友谊街道','广场街道'],
27                         '路北区':['翔云道街道','光明街道']
28                         },
29         },
30         '河南':{
31                 '郑州':{
32                         '金水区':['文化路街道','花园路街道'],
33                         '中原区':['林山寨街道','建设路街道']
34                         },
35                 '洛阳':{
36                         '西工区':['王城路街道','金谷园街道'],
37                         '老城区':['西关街道办事处','东南隅街道办事处']
38                         },
39                 '信阳':{
40                         '浉河区':['老城街道','民权街道'],
41                         '平桥区':['羊山街道','前进街道']
42                         },
43         },
44 }
45 
46 
47 while True:
48         for j in date:
49                 print(j)
50         provinces = input('请选择要进入的省份>>>>>:')
51         if provinces in date:
52                 while True:
53                         for j1 in date[provinces]:
54                                 print('可选择的城市如下:',j1)
55                         city = input('请选择进入的城市(b返回上一层,q退出)>>>>>:')
56                         if city in date[provinces]:
57                                 while True:
58                                         for j2 in date[provinces][city]:
59                                                 print('可选择城区如下:',j2)
60                                         district = input('请选择进入的城区(b返回上一层,q退出)>>>>>:')
61                                         if district in date[provinces][city]:
62                                                 for j3 in  date[provinces][city][district]:
63                                                         print('街道或标志性企业:',j3)
64                                                 street = input('最后一层展示,请按b返回,q退出>>>>>:')
65                                                 if street == 'b':
66                                                         break
67                                                 elif street == 'q':
68                                                         exit('菜单栏展示结束!')
69                                         if street == 'b':
70                                                 break
71                                         elif street == 'q':
72                                                 exit('菜单栏展示结束!')
73                                 if district == 'b':
74                                         break
75                                 elif district == 'q':
76                                         exit('菜单栏展示结束!')
77                         if city == 'b':
78                                 break
79                         elif city == 'q':
80                                 exit('菜单栏展示结束!')
81         if provinces == 'b':
82                 print('消遣哥呢?玩一把吧~~~')
83         elif provinces == 'q':
84                 exit('看看就走了啊!无趣~~~')
 1 menu = {
 2     '北京':{
 3         '海淀':{
 4             '五道口':{
 5                 'soho':{},
 6                 '网易':{},
 7                 'google':{}
 8             },
 9             '中关村':{
10                 '爱奇艺':{},
11                 '汽车之家':{},
12                 'youku':{},
13             },
14             '上地':{
15                 '百度':{},
16             },
17         },
18         '昌平':{
19             '沙河':{
20                 '老男孩':{},
21                 '北航':{},
22             },
23             '天通苑':{},
24             '回龙观':{},
25         },
26         '朝阳':{},
27         '东城':{},
28     },
29     '上海':{
30         '闵行':{
31             "人民广场":{
32                 '炸鸡店':{}
33             }
34         },
35         '闸北':{
36             '火车战':{
37                 '携程':{}
38             }
39         },
40         '浦东':{},
41     },
42     '山东':{},
43 }
44 
45 current_levl = menu
46 list_levl = []
47 
48 while True:
49     for key in current_levl:
50         print(key)
51     choies = input('要进入的层级:').strip()
52     if len(choies) == 0:continue
53     if choies == 'b':
54         if len(list_levl) == 0 :break
55         current_levl = list_levl[-1]
56         list_levl.pop()
57     if choies not in current_levl:continue
58     list_levl.append(current_levl)
59     current_levl = current_levl[choies]

本文来自博客园,作者:白日梦想家Zz,转载请注明原文链接:https://www.cnblogs.com/zzlain/p/5980428.html

原文地址:https://www.cnblogs.com/zzlain/p/5980428.html