(1) 关键字
# -*- coding: utf-8 -*- #!/usr/bin/python3 """ 1、关键字(保留字) ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'] """ import keyword print(keyword.kwlist) """2、注释""" #单行注释 """ 多行注释""" '''多行注释'''
(2)数据类型,此处重点字符串常用方法
# -*- coding: utf-8 -*- #!/usr/bin/python3 #1、数字类型 """ 数字有四种类型:整数、布尔型、浮点数和复数。 int (整数), 如 1, 只有一种整数类型 int,表示为长整型,没有 python2 中的 Long。 bool (布尔), 如 True。 float (浮点数), 如 1.23、3E-2 complex (复数), 如 1 + 2j、 1.1 + 2.2j """ #2、字符串 参考:https://www.cnblogs.com/fyknight/p/7895894.html def strFuc(): a = "hello" b = a *3 print(a) #python2 print不要括号 print(b) print(a,b) #不换行 print(a,end=",") print(b,end=",") """ hello hellohellohello hello hellohellohello hello,hellohellohello, """ s = "hello stupid" #---------<常用方法>----------- #index 返回元素下标 index = s.index("u") print(index) # 8 #find leftIndex = s.find("stu") print(leftIndex) # 6 #count 得到字符串元素的个数 count = s.count("l") print(count) # 2 #strip 类似java trim(),移除左右两边空格 print(" add ".strip()) #split 分隔符,返回列表 s = "hello,stupid" lis = s.split(",") print(lis) print(isinstance(lis, list)) # 列表 #replace 替换 s = "hello,stupid" rep = s.replace("stupid", "笨蛋") print(rep) # hello,笨蛋 #join 序列中的元素以指定的字符拼接生成一个新的字符串,并返回这个字符串 tup = ("Hello,my ", "beautiful", " girl") #tup = ["Hello,my ","beautiful"," girl"] str = "".join(tup) print(str) # Hello,my beautiful girl #format # 按默认顺序对字符串进行格式化 s = "I am dark {},I'm {} years old!" s1 = s.format('knight', '28') print(s1) # I am dark knight,I'm 28 years old! #按位置对字符串进行格式化 s = "I am dark {1},I'm {0} years old!" s1 = s.format('28', 'knight') print(s1) #I am dark knight,I'm 28 years old! # endoce()和decode() # 以utf-8的方式进行编码并解码 s = '暗黑骑士' s1 = s.encode('utf-8') print(s1) # 得到以"utf-8"编码后的二进制数 s2 = s1.decode('utf-8') print(s2) # 用decode解码,以什么编码就要以什么解码,否则乱码 """ b'\xe6\x9a\x97\xe9\xbb\x91\xe9\xaa\x91\xe5\xa3\xab' 暗黑骑士 """ # startswith(suffix,start,end)和endswith(suffix,start,end) s = 'dark knight' s1 = s.startswith('dark') print(s1) # True # 判断字符串是否以指定字符串开头,并指定范围 s = 'dark knight' s1 = s.startswith('k', 5) print(s1) # True #upper() 和lower() # title() 将字符串中的所有单词的首字母替换成大写字母 s = "i am hero" print(s.title()) # I Am Hero # swapcase() 将字符串的大小写字母进行转换,并返回转换后的字符串 print(s.swapcase()) # I AM HERO #capitalize() 返回一个将字符串首字母大写、字符串中的其它字母变为小写的字符串 s = 'dARk kNigHt' s1 = s.capitalize() print(s1) #Dark knight #center(len,sginl) 返回一个居中对齐的字符串,并使用指定长度的填充符号,不指定填充符号默认使用空格作为填充符号 s = 'dark knight' s1 = s.center(30) print('开始->', s1, '<-结束') #开始-> dark knight <-结束 print('开始->', s.center(30,"*"), '<-结束') #开始-> *********dark knight********** <-结束 #zfill() 按指定长度在字符串的左侧填充"0"补齐,并返回这个补齐“0”后的字符串 s = "hej" print(s.zfill(15)) #000000000000hej #isdigit() 判断字符串是否只由数字组成,并返回一个布尔值 #isalpha() 判断字符串是否只由字母组成 #isalnum() 判断字符串是否只由字母或数字组成 #isupper() 判断字符串中所有的字母是否为大写。并返回一个布尔值 #islower() 判断字符串中所有的字母是否为小写。并返回一个布尔值 #isspace() 判断字符串是否只由空白字符组成,并返回一个布尔值
(3)python 导入:
commonModule.py
# -*- coding: utf-8 -*- #!/usr/bin/python3 def isGreater(x,y): ''' x是否大于 :param x: :param y: :return:yTrue or False ''' return x > y
moduleTest.py
# -*- coding: utf-8 -*- #!/usr/bin/python3 import com.grammar.commonModule as commonModule if __name__ == "__main__": a = commonModule.isGreater(3,2) print(a)
此处需要指定工程为sourceFolder。具体有下两种:
1、pycharm:file>setting>
2、找到工程python37.iml文件
(4)数据类型(列表、元组、字典、集合)
列表:
# -*- coding: utf-8 -*- #!/usr/bin/python3 def listFuc(): lis = [2,3,"hello"] #列表的数据项不需要具有相同的类型 print(lis) #len(list) 列表元素个数 #max(list) 返回列表元素最大值 注意:元素相同类型,否则报错 #min(list) 返回列表元素最小值 #list(seq) 将元组转换为列表 #----列表方法 #list.append(obj) 在列表末尾添加新的对象 #list.count(obj) 统计某个元素在列表中出现的次数 #list.extend(seq) 在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表 #list.index(obj) 从列表中找出某个值第一个匹配项的索引位置 #list.insert(index, obj) 将对象插入列表 list1 = ['Google', 'Runoob', 'Taobao'] list1.insert(1, 'Baidu') print('列表插入元素后为 : ', list1) #列表插入元素后为 : ['Google', 'Baidu', 'Runoob', 'Taobao'] #list.pop([index=-1]) 移除列表中的一个元素(默认最后一个元素),并且返回该元素的值 #list.remove(obj) 移除列表中某个值的第一个匹配项 #list.reverse() 反向列表中元素 #list.sort(key=None, reverse=False) 对原列表进行排序,其中key是按照某种规则排序 vowels = ['e', 'a', 'u', 'o', 'i'] vowels.sort(reverse=True) ## 降序 print('降序输出:', vowels) # 降序输出: ['u', 'o', 'i', 'e', 'a'] random = [(2, 2), (3, 4), (4, 1), (1, 3)] # 以元组第二元素有小到达排序 random.sort(key=lambda x: x[1]) # [(4, 1), (2, 2), (1, 3), (3, 4)] print(random) #list.clear() 清空列表 #list.copy() 复制列表 #遍历列表(1) lis = [2,3,4,5,1,7] for i in lis: print(i,end=",") else: print("结束") #遍历列表(2) for i in range(len(lis)): print("序号:%s 值:%s"%(i,lis[i])) # 遍历列表(3) for i, val in enumerate(lis): print("序号:%s 值:%s" % (i, val)) """ 序号:0 值:2 序号:1 值:3 序号:2 值:4 序号:3 值:5 序号:4 值:1 序号:5 值:7 """ #添加元素 list = [2, 3, 4, 5, 1, 7] # list[6] =8 #IndexError list.append(8) # ok print(list) #删除数据 delList = [2,3,4,5,1,7] #del delList[6] #IndexError 注意:不要超出范围 del delList[5] print(delList)#[2, 3, 4, 5, 1] #序列 seqList = [2,3,4,5,1,7] print(seqList[0:2]) #[2, 3] print(seqList[1:]) # [3, 4, 5, 1, 7] print(seqList[0:len(seqList)]) #[2, 3, 4, 5, 1, 7]
未完待续。。。