Python之路——基础1

xiaoxiao2021-02-28  50

一、数据类型

int(整型)

  在32位机器上,整数的位数为32位,取值范围为-2**31~2**31-1,即-2147483648~2147483647  在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1,即-9223372036854775808~9223372036854775807 long(长整型)

      Python中未限制长度,但受限于内存;int 溢出时,自动转换为 long

float(浮点型)

     浮点数用来处理实数,即带有小数的数字。占8个字节(64位),其中52位表示底,11位表示指数,剩下的一位表示符号。 complex(复数)  复数由实数部分和虚数部分组成,一般形式为x+yj,其中的x是复数的实数部分,y是复数的虚数部分,这里的x和y都是实数。 布尔值     True  /  False  非0为True

二、列表、元组

name_list = ["join","Samu",888,] #定义列表 for each in name_list: #FOR 循环 if each != 888: #if...else... print(each) else: print("the number is",each) name_list.append("苏轼")#列表追加元素 name_list.count("join")#计算列表中有多少个指定元素 name_list.clear()#清空列表中的所有元素 new_list = name_list.copy()#复制列表 name_list.extend("wo")#1、指定元素必须可迭代;功能:将指定可迭代的每个单元作为元素添加到列表中 i = name_list.index("o")#返回列表中指定元素的索引号,只返回找到的第一个索引 name_list.insert(1,"hello")#在指定索引位置插入指定元素 name_list.pop()#弹出列表中的指定元素,若不指定则默认弹出最后一个元素 name_list.remove("hello")#移除列表中的指定元素 name_list.reverse()#列表翻转排列 name_list.sort()#列表元素升序排列,3.X 版本中,不同数据类型不能用次方法排序

元组:不可修改的列表

tuple1 = ("hello",99,"sun",99) print(tuple1.index(99)) print(tuple1.count(99)) print(type(123))

三、字符串

a = "I have a story!Let's go!" print(a.count("p"))#计算字符串中有多少个指定元素 print(a.count("p",1,9))#计算字符串中指定序号范围内有多少个指定元素 print(a.capitalize())#首字母大写 print(a.casefold())#大写字母全部转小写 print(a.swapcase())#大小写字母互相转换 a.lower()#大写字母全部转小写 a.upper()#小写字母全部转大写 print(a.center(50,"-"))#输出'-------------------pyThon3.0Pp--------------------' print(a.encode())#编码方式,将字符串编码成bytes格式 print(a.endswith("p"))#判断字符串是否以指定字符串结尾 print(a.startswith("w"))#判断字符串是否以指定字符串开始 print("a\tb".expandtabs(10))#如果存在\t,则将\t转换成指定长度的空格 print(a.find("Th",0,5))#查找A,找到返回其索引, 找不到返回-1 a = "my name is {},I am {} years old." print(a.format("jj",21))#输出"my name is jj,I am 21 years old." a = "my name is {a1},I am {a2} years old." print(a.format(a1="jj",a2=21))#输出"my name is jj,I am 21 years old." a = "my name is {1},I am {0} years old." print(a.format("jj",21))#输出"my name is 21,I am jj years old." a = "my name is {a1},I am {a2} years old." print(a.format_map({"a1":"dd","a2":11}))#输出"my name is dd,I am 11 years old." print(a.index("p"))#返回字符串中指定字符的索引号,只返回找到的第一个索引 print(a.isalnum())#判断字符串是否只包含字母或者数字,是返回True,含有其他字符则返回False print(a.isalpha())#判断字符串是否只包含字母,是返回True,含有其他字符则返回False print(a.isdecimal())#判断字符串是否只包含十进制数字,是返回True,含有其他字符则返回False print(a.isdigit())#判断字符串是否只包含数字,是返回True print(a.isnumeric())#判断字符串是否只包含数字字符,是返回True print(a.isidentifier())#判断字符串是否是一个合法的标识符,是则返回True print(a.islower())#判断字符串是否全部是小写 print(a.isupper())#判断字符串是否全部是大写 print(a.isprintable()) print(a.isspace())#判断是否包含空格 print(a.istitle())#判断是否标题华,即首字母大写,其余小写 print(a.strip())#删除字符串两端的空格,带参数可删除指定的字符 a.lstrip() a.rstrip() print("hello!".replace("ll","oo"))#替换 print(a.partition("a")) #a.rpartition("a") #以指定字符(字符中有的)将字符串分割成3个元素的元组返回,输出“('I h', 'a', "ve a story!Let's go!")” print(a.split("a"))#以指定字符(字符中有的)将字符串分割成列表元素返回,输出“['I h', 've ', " story!Let's go!"]” print(a.splitlines())#以换行符分割,返回一个包含各行作为元素的列表 print(a.zfill(20))#返回指定宽度的字符串,右对齐,位数不够添0 print("I have a story!Let's go!".translate(str.maketrans("asto","1234")))#输出“I h1ve 1 234ry!Le3'2 g4!” print("-".join("hello"))#输出“h-e-l-l-o” print("-".join(["你","们","好"]))#输出“你-们-好”

四、字典、集合

字典:无序,每个元素包含 key :value

dict1={1:"sun",2:"moon",3:"floor",4:"money"} dict1[5]="bike" dict1.copy() dict1.clear()#清空字典 dict1.pop(4)#删除指定KEY的字典元素 del dict1[5]#删除指定KEY的字典元素 dict1.popitem()#随机删除字典某个元素 dict1.get(3)#获取指定KEY的值 dict1.setdefault(8)#与get类似,但键不在字典中会自动添加到字典,值为None dict1.keys()#获取字典key dict1.values()#获取字典值 dict1.update({7:"glass",8:"lenovo"})#更新或增加字典元素 dict1.fromkeys(10)#创建或增加字典的值,参数1为键,参数2为值,参数2为空,则值为None for key in dict1: #字典遍历一 print(dict1[key],end=",") for k,v in dict1.items():#字典遍历二 print(k,v)

集合:每一个元素唯一,无序

s1 = set([1,3,5,7,9]) s2 = set([]) s3 = frozenset({2,1,4})#定义不可变集合 s2.add(3) s2.clear() s2 = s1.copy() s2.difference(s1)#返回在S2中且不在S1中的集合,原有集合不变,等价于 S2-S1 s2.difference_update(s1)#剔除s2中与S1相同的元素,等价于S2 =S2-S1 s2.discard(9)#抛弃集合中的某个元素 s2.intersection(s1)#返回一个新的集合,包含s1 s2的共有元素 s2.intersection_update(s1)#剔除s2中且不在S1同的元素 s2.issubset(s1)#测试s2中的每一个元素是否都在s1中 s2.issuperset(s1)#测试s1中的每一个元素是否都在s2中 s1.union(s2)#返回一个新的集合包含s1和s2中的所有元素,等价于S1|S2 l1 = list(set([1,2,1,1,4,5,11,2,4,9]))#列表去重

参考文章:

http://www.cnblogs.com/alex3714/articles/5465198.html

转载请注明原文地址: https://www.6miu.com/read-2624120.html

最新回复(0)