Python开发【第二篇】:Python基本数据类型
运算符
一、数字运算
二、比较运算
三、赋值运算
四、逻辑运算
五、成员运算
基本数据类型
一、数字
int(整型)
在32位机器上,整数的位数为32位,取值范围为-231~231-1,即-2147483648~2147483647
在64位系统上,整数的位数为64位,取值范围为-263~263-1,即-9223372036854775808~9223372036854775807
- int 将字符串转换为数字 a = "123" print(type(a),a) b = int(a) print(type(b),b) num = "0011" v = int(num, base=16) print(v) - bit_lenght # 当前数字的二进制,至少用n位表示 r = age.bit_length()
二、字符串
字符串一旦创建,不可修改
一旦修改或者拼接,都会造成重新生成字符串
字符串常用功能
- join
- split
- find
- strip
- upper
- lower
- replace
# 1 首字母大写 # test = "aLex" # v = test.capitalize() # print(v) # 2 所有变小写,casefold更牛逼,很多未知的对相应变小写 # v1 = test.casefold() # print(v1) # v2 = test.lower() # print(v2) # 3 设置宽度,并将内容居中 # 20 代指总长度 # * 空白未知填充,一个字符,可有可无 # v = test.center(20,"中") # print(v) # test = "alex" # v = test.ljust(20,"*") # print(v) # test = "alex" # v = test.rjust(20,"*") # print(v) # test = "alex" # v = test.zfill(20) # print(v) # 4 去字符串中寻找,寻找子序列的出现次数 # test = "aLexalexr" # v = test.count(‘ex‘) # print(v) # test = "aLexalexr" # v = test.count(‘ex‘,5,6) # print(v) # 5 # 以什么什么结尾 # 以什么什么开始 # test = "alex" # v = test.endswith(‘ex‘) # v = test.startswith(‘ex‘) # print(v) # 6 expandtabs,断句20, # test = # v = test.expandtabs(20) # print(v) # 7 从开始往后找,找到第一个之后,获取其未知 # > 或 >= # test = "alexalex" # 未找到 -1 # v = test.find(‘ex‘) # print(v) # 8 index找不到,报错 忽略 # test = "alexalex" # v = test.index(‘8‘) # print(v) # 9 格式化,将一个字符串中的占位符替换为指定的值 # test = ‘i am {name}, age {a}‘ # print(test) # v = test.format(name=‘alex‘,a=19) # print(v) # test = ‘i am {0}, age {1}‘ # print(test) # v = test.format(‘alex‘,19) # print(v) # 10 格式化,传入的值 {"name": ‘alex‘, "a": 19} # test = ‘i am {name}, age {a}‘ # v1 = test.format(name=‘df‘,a=10) # v2 = test.format_map({"name": ‘alex‘, "a": 19}) # 11 字符串中是否只包含 字母和数字 # test = "123" # v = test.isalnum() # print(v) # str # 12 是否是字母,汉子 # test = "as2df" # v = test.isalpha() # print(v) # 13 当前输入是否是数字 # test = "二" # 1,② # v1 = test.isdecimal() # v2 = test.isdigit() # v3 = test.isnumeric() # print(v1,v2,v3) # 14 是否存在不可显示的字符 # \t 制表符 # \n 换行 # test = "oiuas\tdfkj" # v = test.isprintable() # print(v) # 15 判断是否全部是空格 # test = "" # v = test.isspace() # print(v) # 16 判断是否是标题 # test = "Return True if all cased characters in S are uppercase and there is" # v1 = test.istitle() # print(v1) # v2 = test.title() # print(v2) # v3 = v2.istitle() # print(v3) # 17 ***** 将字符串中的每一个元素按照指定分隔符进行拼接 # test = "你是风儿我是沙" # print(test) # # t = ‘ ‘ # v = "_".join(test) # print(v) # 18 判断是否全部是大小写 和 转换为大小写 # test = "Alex" # v1 = test.islower() # v2 = test.lower() # print(v1, v2) # v1 = test.isupper() # v2 = test.upper() # print(v1,v2) # 19 # 移除指定字符串 # 有限最多匹配 # test = "xa" # # v = test.lstrip(‘xa‘) # v = test.rstrip(‘9lexxexa‘) # # v = test.strip(‘xa‘) # print(v) # test.lstrip() # test.rstrip() # test.strip() # 去除左右空白 # v = test.lstrip() # v = test.rstrip() # v = test.strip() # print(v) # print(test) # 去除\t \n # v = test.lstrip() # v = test.rstrip() # v = test.strip() # print(v) # 20 对应关系替换 # test = "aeiou" # test1 = "12345" # v = "asidufkasd;fiuadkf;adfkjalsdjf" # m = str.maketrans("aeiou", "12345") # new_v = v.translate(m) # print(new_v) # 21 分割为三部分 # test = "testasdsddfg" # v = test.partition(‘s‘) # print(v) # v = test.rpartition(‘s‘) # print(v) # 22 分割为指定个数 # v = test.split(‘s‘,2) # print(v) # test.rsplit() # 23 分割,只能根据,true,false:是否保留换行 # test = "asdfadfasdf\nasdfasdf\nadfasdf" # v = test.splitlines(False) # print(v) # 24 以xxx开头,以xx结尾 # test = "backend 1.1.1.1" # v = test.startswith(‘a‘) # print(v) # test.endswith(‘a) # 25 大小写转换 # test = "aLex" # v = test.swapcase() # print(v) # 26 字母,数字,下划线 : 标识符 def class # a = "def" # v = a.isidentifier() # print(v) # 27 将指定字符串替换为指定字符串 # test = "alexalexalex" # v = test.replace("ex",‘bbb‘) # print(v) # v = test.replace("ex",‘bbb‘,2) # print(v)
# 一、for循环 # for 变量名 in 字符串: # 变量名 # break # continue # index = 0 # while index < len(test): # v = test[index] # print(v) # # index += 1 # print(‘=======‘) # for zjw in test: # print(zjw) # test = "郑建文妹子有种冲我来" # for item in test: # print(item) # break # for item in test: # continue # print(item) # 二、索引,下标,获取字符串中的某一个字符 # v = test[3] # print(v) # 三、切片 # v = test[0:-1] # 0=< <1 # print(v) # 四、获取长度 # Python3: len获取当前字符串中由几个字符组成 # v = len(test) # print(v) # 五、获取连续或不连续的数字, # Python2中直接创建在内容中 # python3中只有for循环时,才一个一个创建 # r1 = range(10) # r2 = range(1,10) # r3 = range(1,10,2) # 帮助创建连续的数字,通过设置步长来指定不连续 # v = range(0, 100, 5) # # for item in v: # print(item)
三、列表
列表格式
- 列表中可以嵌套任何类型
- 中括号括起来
- ,分割每个元素
- 列表中的元素可以是 数字,字符串,列表,布尔值..所有的都能放进去
- “集合”,内部放置任何东西
- 列表,有序;元素可以被修改
#########################list类中提供的方法 ####################### # li = [11, 22, 33, 22, 44] # 参数 # 1. 原来值最后追加 # 对象.方法(..) # li对象调用append方法 # li.append(5) # li.append("alex") # li.append([1234,2323]) # print(li) # 2 清空列表 # li.clear() # print(li) # 3 拷贝,浅拷贝 # v = li.copy() # print(v) # 4. 计算元素出现的次数 # v = li.count(22) # print(v) # 5. 扩展原列表,参数:可迭代对象 # li = [11, 22, 33, 22, 44] # li.append([9898,"不得了"]) # [11, 22, 33, 22, 44, [9898, ‘不得了‘]] # li.extend([9898,"不得了"]) # for i in [9898,"不得了"]: # li.append(i) # [11, 22, 33, 22, 44, 9898, ‘不得了‘] # # li.extend("不得了") # print(li) # 6. 根据值获取当前值索引位置(左边优先) # li = [11, 22, 33, 22, 44] # v= li.index(22) # print(v) # 7. 在指定索引位置插入元素 # li = [11, 22, 33, 22, 44] # li.insert(0,99) # print(li) # 8、 删除某个值(1.指定索引;2. 默认最后一个),并获取删除的值 # li = [11, 22, 33, 22, 44] # v = li.pop() # print(li) # print(v) # li = [11, 22, 33, 22, 44] # v = li.pop(1) # print(li) # print(v) # 9. 删除列表中的指定值,左边优先 # li = [11, 22, 33, 22, 44] # li.remove(22) # print(li) # PS: pop remove del li[0] del li[7:9] clear # 10 将当前列表进行翻转 # li = [11, 22, 33, 22, 44] # li.reverse() # print(li) # 11 列表的排序 # li = [11,44, 22, 33, 22] # li.sort() # li.sort(reverse=True) # print(li)
####################################### 深灰魔法 ####################################### # 1. # 索引取值 print(li[3]) # 2 切片,切片结果也是列表 print(li[3:-1]) # 3 for循环 # while循环 for item in li: print(item) """ # 列表元素,可以被修改 # li = [1, 12, 9, "age", ["石振文", ["19", 10], "庞麦郎"], "alex", True] ############## 4 索引 # 修改 # li[1] = 120 # print(li) # li[1] = [11,22,33,44] # print(li) # 删除,第一种方式 # del li[1] # print(li) ############## 5 切片 # 修改 # li[1:3] = [120,90] # print(li) # 删除 # del li[2:6] # print(li) # 6 in 操作 # li = [1, 12, 9, "age", ["石振文", ["19", 10], "庞麦郎"], "alex", True] # v1 = "石振文" in li # print(v1) # v2 = "age" in li # print(v2) # 7 转换 # 字符串转换列表 li = list("asdfasdfasdf"), 内部使用for循环 # s = "pouaskdfauspdfiajsdkfj" # new_li = list(s) # print(new_li) # 列表转换成字符串, # 需要自己写for循环一个一个处理: 既有数字又有字符串 # li = [11,22,33,"123","alex"] # # r = str(li) # ‘[11,22,33,"123","alex"]‘ # # print(r) # s = "" # for i in li: # s = s + str(i) # print(s) # 直接使用字符串join方法:列表中的元素只有字符串 # li = ["123","alex"] # v = "".join(li) # print(v)
四、元组
元组,元素不可被修改,不能被增加或者删除
tu = (11,22,33,44) tu.count(22),获取指定元素在元组中出现的次数 tu.index(22)获取指定元素在元组的索引
# 1. 书写格式 # tu = (111,"alex",(11,22),[(33,44)],True,33,44,) # 一般写元组的时候,推荐在最后加入 , # 元素不可被修改,不能被增加或者删除 # 2. 索引 # v = tu[0] # print(v) # 3. 切片 # v = tu[0:2] # print(v) # 4. 可以被for循环,可迭代对象 # for item in tu: # print(item) # 5. 转换 # s = "asdfasdf0" # li = ["asdf","asdfasdf"] # tu = ("asdf","asdf") # # v = tuple(s) # print(v) # v = tuple(li) # print(v) # v = list(tu) # print(v) # v = "_".join(tu) # print(v) # li = ["asdf","asdfasdf"] # li.extend((11,22,33,)) # print(li) # 6.元组的一级元素不可修改/删除/增加 # tu = (111,"alex",(11,22),[(33,44)],True,33,44,) # # 元组,有序。 # # v = tu[3][0][0] # # print(v) # # v=tu[3] # # print(v) # tu[3][0] = 567 # print(tu)
五、字典
- 字典无序
- 字典的value可以是任何值
- 布尔值(1,0)、列表、字典不能作为字典的key**
# 1 根据序列,创建字典,并指定统一的值 # v = dict.fromkeys(["k1",123,"999"],123) # print(v) # 2 根据Key获取值,key不存在时,可以指定默认值(None) # v = dic[‘k11111‘] # print(v) # v = dic.get(‘k1‘,111111) # print(v) # 3 删除并获取值 # dic = { # "k1": ‘v1‘, # "k2": ‘v2‘ # } # v = dic.pop(‘k1‘,90) # print(dic,v) # k,v = dic.popitem() # print(dic,k,v) # 4 设置值, # 已存在,不设置,获取当前key对应的值 # 不存在,设置,获取当前key对应的值 # dic = { # "k1": ‘v1‘, # "k2": ‘v2‘ # } # v = dic.setdefault(‘k1111‘,‘123‘) # print(dic,v) # 5 更新 # dic = { # "k1": ‘v1‘, # "k2": ‘v2‘ # } # dic.update({‘k1‘: ‘111111‘,‘k3‘: 123}) # print(dic) # dic.update(k1=123,k3=345,k5="asdf") # print(dic)
- 字典的value可以是任何值
# info = { # "k1": 18, # "k2": True, # "k3": [ # 11, # [], # (), # 22, # 33, # { # ‘kk1‘: ‘vv1‘, # ‘kk2‘: ‘vv2‘, # ‘kk3‘: (11,22), # } # ], # "k4": (11,22,33,44) # } # print(info)
- 索引方式找到指定元素
# info = { # "k1": 18, # 2: True, # "k3": [ # 11, # [], # (), # 22, # 33, # { # ‘kk1‘: ‘vv1‘, # ‘kk2‘: ‘vv2‘, # ‘kk3‘: (11,22), # } # ], # "k4": (11,22,33,44) # } # # v = info[‘k1‘] # # print(v) # # v = info[2] # # print(v) # v = info[‘k3‘][5][‘kk3‘][0] # print(v)
- 字典支持 del 删除
# info = { # "k1": 18, # 2: True, # "k3": [ # 11, # [], # (), # 22, # 33, # { # ‘kk1‘: ‘vv1‘, # ‘kk2‘: ‘vv2‘, # ‘kk3‘: (11,22), # } # ], # "k4": (11,22,33,44) # } # del info[‘k1‘] # # del info[‘k3‘][5][‘kk1‘] # print(info)
- for循环
# dict # info = { # "k1": 18, # 2: True, # "k3": [ # 11, # [], # (), # 22, # 33, # { # ‘kk1‘: ‘vv1‘, # ‘kk2‘: ‘vv2‘, # ‘kk3‘: (11,22), # } # ], # "k4": (11,22,33,44) # } # for item in info: # print(item) # # for item in info.keys(): # print(item) # for item in info.values(): # print(item) # for item in info.keys(): # print(item,info[item]) # for k,v in info.items(): # print(k,v)
相关推荐
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20
weiiron 2020-11-16