前言:
目前大家对“存在字符串i love python取出v并输出”大概比较看重,同学们都需要分析一些“存在字符串i love python取出v并输出”的相关知识。那么小编同时在网上搜集了一些关于“存在字符串i love python取出v并输出””的相关资讯,希望同学们能喜欢,小伙伴们快快来学习一下吧!在文章开始之前先说一句,小编最近整理了一套Python学习教程,有需要的小伙伴,记得关注小编头条号,私信【学习】即可免费领取一套Python学习教程哦
一、python的数据类型
1.python的基本数据类型包括数值数据类型和字符串数据类型;基本数据类型的特点是不允许改变,如果改变基本数据类型的值,会导致内存的重新分配。
2.数值的相互转换函数
3.变量的三个属性
4.判断某个变量是否是某种类型实例
print(isinstance(3, float)) #False
5.复数类型的使用
a = complex(2,2)b = complex("3+2j")print(a)print(b)print(a+b)print(a-b)print(a*b)print(a/b)(2+2j)(3+2j)(5+4j)(-1+0j)(2+10j)(0.7692307692307692+0.15384615384615388j)
6.python的字符串类型不像Java可以与数值类型相加,如“str”+1会报错,但python支持字符串与数值相乘,表示将字符串重复多少次。
#报错#print("str"+1)<br>#重复字符串print("The life is short,i love python\n" * 3) #The life is short,i love python #The life is short,i love python #The life is short,i love python<br>#转为大写print("lower".upper()) #LOWER<br>#转为小写print("UPPER".lower()) #upper<br><br>#去除前导和尾部空白print(" hello python ".strip()) #hello python<br><br>#也可以指定去除的字符print("##hello python##!".strip("#!")) #hello python<br><br>#求长度print(len("hello")) #5<br><br>#拆分字符串print("orange banana apple".split(" ")) #['orange', 'banana', 'apple']<br><br>#用指定字符串连接一个列表print(" ".join(["orange","apple","banana","peach"])) #orange apple banana peach<br><br>#判断字符串是否以某个子串开始print("good good study".startswith("go")) #True<br><br>#判断字符串是否以某个子串开始print("day day up".endswith("up")) #True<br><br>#统计子串出现次数print("Python is a nascent programming language.".count("p")) #1<br><br>#查找子串出现的下标print("Sometimes your whole life boils down to one insane move.".find("wh")) #15<br><br>#判断是否是数字print("moive".isdigit()) #False<br><br>#判断是否是字母print("money".isalpha()) #True<br><br>#判断是否是空格print("i declared".isspace()) #False
7.Python的字符串最强大之处是切片操作
str = "Attitude determines altitude."#取出Attitude(外国人的习惯是左闭右开,即包括左下标,不包括右下标)print(str[0:9]) #Attitudeprint(str[0:20:3]) #Aiddeis (每3个取一个) print(str[-5:-1]) #tude
8.Python的字符串可以用单引号,双引号,三引号表示,但字符串跨行时只能用三引号或反斜杠
str = '''Bealaboriousbee'''print(str)str1 = "Be \a \hard-working \ man"print(str1) #Be a hard-working man
9.实现字符串反转
#切片 str[开始位置:结束位置:步数] 将步数设为-1def strReverse1(str): return str[::-1]#将字符串转为列表,反转后再连接为字符串def strReverse2(str): newList = list(str) newList.reverse() return "".join(newList)print(strReverse1("python"))print(strReverse2("python"))
二、输入输出
1、输入函数input()
#输入一个字符串myStr = input("请输入一个字符串:")print(myStr)#以逗号分割输入多个数a,b,c = eval(input("请输入3个数:"))print(a,b,c)
2.输出函数 print()
#以逗号分割输出多个变量,输出后显示的分隔符是空格,结尾会输出一个换行符print("i","am","studying","python") #i am studying python#指定分隔符和结尾符print("i","am","studying","python",sep="#",end=" ")print()#类似c语言的输出name = "jackson"age = 22salary = 5000.0print("name:%s,age:%d,salary:%.3f" % (name,age,salary))#python特有的输出形式print("I like eating {} and {}".format("banana", "pear"))print("His name is {name} and he is a {sex}".format(sex='male',name='judy'))print("{1} can arm our {0}".format("brain","knowledge"))print("The happiness rate of chinese citizen is up to {:.3f}".format(0.45))print("The population of china is {:,d} million".format(1300))
三、集合数据类型
Python的集合数据类型包括列表,元组,字典,集合
1.列表
#四种遍历方式for item in fruits: print(item,end=" ")print()for item in iter(fruits): print(item,end=" ")print()for i,item in enumerate(fruits): print(i,"===>",item,end=" ")print()for item in range(len(fruits)): print(fruits[item],end=" ")print()#重复列表四次print(fruits*4) #['orange', 'banana', 'pear', 'peach', 'orange', 'banana', 'pear', 'peach', 'orange', 'banana', 'pear', 'peach', 'orange', 'banana', 'pear', 'peach']#列表连接print(fruits+["pineapple","apple"])#删除指定下标的元素,不提供参数则删除表尾元素print(fruits.pop(2)) #['orange', 'banana', 'pear', 'peach', 'pineapple', 'apple']#追加元素 append追加和extend追加有区别#append追加的是列表#extend追加的是元素fruits.append(["Coconut"])print(fruits) #['orange', 'banana', 'peach', ['Coconut']]fruits.pop()fruits.extend(["Coconut"])print(fruits) #['orange', 'banana', 'peach', 'Coconut']#列表反转fruits.reverse()print(fruits) #['Coconut', 'peach', 'banana', 'orange']#指定位置插入fruits.insert(2, "nothing")print(fruits) #['Coconut', 'peach', 'nothing', 'banana', 'orange']#删除列表切片del fruits[1:3]print(fruits) #['Coconut', 'banana', 'orange']
2.元组
myTuple1 = ("bird","plane","vehicle","subway")#不能对单个元素重新赋值(报错)#myTuple1[0] = "parrot"#但可以对整个元组赋值myTuple1 = ("python","c++","java")print(myTuple1) #('python', 'c++', 'java')#创建空元组emptyTuple = ()print(emptyTuple) #()#注意创建一个元素的元组要在结尾加逗号oneTuple = (1)print(oneTuple) #1 不加逗号编译器会认为括号是进行算数运算,而不是创建元组oneTuple = (1,)print(oneTuple) #(1,)#元组的取值 print(myTuple1[0]) #pythonprint(myTuple1[:]) #('python', 'c++', 'java')print(myTuple1[:1]) #('python',)print(myTuple1[len(myTuple1)-1:]) #('java',)#元组的重复 print(myTuple1 *4) #('python', 'c++', 'java', 'python', 'c++', 'java', 'python', 'c++', 'java', 'python', 'c++', 'java')#元组相加print(myTuple1 + ("one","two","three")) #('python', 'c++', 'java', 'one', 'two', 'three')
3.字典
myDict = {"one":1,"two":343.33,"three":True}#字典的取值print(myDict["one"]) #1#判断键是否存在print("three" in myDict) #True#更新值myDict["one"] = 100print(myDict) #{'one': 100, 'two': 343.33, 'three': True}#清空字典myDict.clear()print(myDict) #{}
4.集合
#以字符串创建setmySet = set("hello")print(mySet) #{'o', 'l', 'e', 'h'}#以list创建set,自动去除重复元素mySet1 = set(["a","m","e","o"])print(mySet1) #{'o', 'a', 'm', 'e'}#以map创建set,值为map的键mySet2 = set({"name":1,"age":2})print(mySet2) #{'name', 'age'}#交集print(mySet & mySet1) #{'e', 'o'}#并集print(mySet | mySet1) #{'l', 'a', 'e', 'h', 'm', 'o'}#差集print(mySet - mySet1) #{'l', 'h'}#对称差分运算print(mySet ^ mySet1) #{'l', 'a', 'h', 'm'}
四、强大的列表解析功能
import random#列表解析#生成10个随机数list1 = [random.randint(0,100) for i in range(0,10)]for item in list1: print(item,end=" ") #74 38 75 45 96 1 38 93 58 80 print() list2 = [i*i for i in range(1,21)]for num in list2: print(num,end=" ") #1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400 #选出list2中的偶数list3 = [i for i in list2 if i % 2 == 0]for num in list3: print(num) #4 16 36 64 100 144 196 256 324 400 #按行遍历矩阵list4 = [[1,2,3,4],[5,6,7,8],[2,3,4,5],[6,7,8,9]]list5 = [[1,4,5,6],[2,1,3,4],[3,4,2,1],[7,8,2,1]]print([row for row in list4]) #[[1, 2, 3, 4], [5, 6, 7, 8], [2, 3, 4, 5], [6, 7, 8, 9]] #按列遍历矩阵print([list4[row][1] for row in range(len(list4))]) #[2, 6, 3, 7] #遍历对角线print([list4[i][i] for i in range(len(list4))]) # [1, 6, 4, 9] #逐个遍历print([list4[row][col] for row in range(len(list4)) for col in range(len(list4[row]))]) #[1, 2, 3, 4, 5, 6, 7, 8, 2, 3, 4, 5, 6, 7, 8, 9] #矩阵相加print([list4[row][col] + list5[row][col] for row in range(list4.__len__()) for col in range(list4[row].__len__())])#[2, 6, 8, 10, 7, 7, 10, 12, 5, 7, 6, 6, 13, 15, 10, 10]#转置矩阵print([row for row in zip(*list4)]) #[(1, 5, 2, 6), (2, 6, 3, 7), (3, 7, 4, 8), (4, 8, 5, 9)] #求最大长度对应的字符students = ["jack","shirely","kangkang","mary"]maxLen = max([len(item) for item in students])print(maxLen)maxStr = [temp for temp in students if len(temp) == maxLen]print(maxStr) #['kangkang']
五、内置函数
#几个内置的高阶函数 map,filter,reduce,sorted#map用于将一函数规则应用于一可迭代集合的所有元素#reduce用于将一函数规则反复作用于一可迭代集合的元素,传入的函数参数必须为两个#filter将一可迭代集合中适用于函数规则的元素挑选出来#sorted用于对可迭代集合进行排序#map的一个栗子(将字符串转为对应数字)f = lambda ch:{"0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9}[ch]f1 = lambda ch:int(ch)print(list(map(f,"322"))) #[3, 2, 2]#reduce的一个栗子(将字符串转换为对应的十进制数)def str2Decimal(x,y): return x*10 + yprint(reduce(str2Decimal,map(f,"232"))) #232#filter的一个栗子(使用埃氏筛选法筛选素数)seq = range(2,51)for i in seq: f = lambda x : x == i or x % i seq = list(filter(f,seq))print(list(seq)) #[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]#sorted的一个栗子rules = lambda x:x.lower()reles2 = lambda x:x["age"] students = ["jack","Nacy","Kangkang","asia"]aMap = [{"name":"kangknag","age":25},{"name":"ag","age":13},{"name":"wnag","age":23}]newlist = sorted(students, key=rules, reverse=False)print(sorted(aMap,key=reles2)) #[{'name': 'ag', 'age': 13}, {'name': 'wnag', 'age': 23}, {'name': 'kangknag', 'age': 25}]print(newlist) #['asia', 'jack', 'Kangkang', 'Nacy']
版权声明:
本站文章均来自互联网搜集,如有侵犯您的权益,请联系我们删除,谢谢。