python100个必背知识-python编程面试中必考的知识点,数据类型全解,笔记超全面...
原标题:python编程面试中必考的知识点,数据类型全解,笔记超全面
python作为一门高级编程语言,它的定位是优雅、明确和简单。阅读Python编写的代码感觉像在阅读英语一样,这让使用者可以专注于解决问题而不是去搞明白语言本身。Python虽然是基于C语言编写,但是摒弃了C中复杂的指针,使其变得简明易学。并且作为开源软件,Python允许对代码进行阅读,拷贝甚至改进。这些性能成就了Python的高效率,有“人生苦短,我用Python”之说,是一种十分精彩又强大的语言。
字符串(string)
1.字符串常用功能
name = 'derek'print(name.capitalize()) #首字母大写 Derekprint(name.count("e")) #统计字符串出现某个字符的个数 2print(name.center(10,'*')) #打印30个字符,不够的“*”补齐 **derek***print(name.endswith('k')) #判断字符串是否以"k"结尾 True
print('244'.isdigit()) #判断字符是否为整数 Trueprint('+'.join(['1','2','3'])) #把join后的内容加入到前面字符串中,以+为分割符 1+2+3print(' 123'.strip()) #strip去掉换行符print("1+2+3+4".split("+")) #以+为分隔符生成新的列表,默认不写为空格 ['1', '2', '3', '4']
msg = 'my name is {name} and i am {age} old'print(msg.format(name='derek',age=20))
my name is derek and i am 20 old
2.字符串内置方法
View Code
列表
#创建
fruit = ['apple','pear','grape','orange']
#切片print(fruit[1]) #pearprint(fruit[1:3]) #['pear', 'grape']print(fruit[-1]) #orangeprint(fruit[:2]) #['apple', 'pear']
# 追加
fruit.append('peach')print(fruit) #['apple', 'pear', 'grape', 'orange', 'peach']
# 删除
fruit.remove('peach') #删除指定的print(fruit) #['apple', 'pear', 'grape', 'orange']
fruit.pop() #删除列表最后一个元素print(fruit) #['apple', 'pear', 'grape']
del fruit[2] #删除指定的索引print(fruit) #['apple', 'pear']
# 插入
fruit.insert(1,'grape') #把"grape’加入到索引为1的位置print(fruit) #['apple', 'grape', 'pear']
# 修改
fruit[2] = 'orange' #直接修改print(fruit) #['apple', 'grape', 'orange']
# 扩展
fruit1 = ['apple','orange']
fruit2 = ['pear','grape']
fruit1.extend(fruit2)print(fruit1) #['apple', 'orange', 'pear', 'grape']
# 统计print(fruit1.count('apple')) #1
# 排序fruit1.sort()print(fruit1) #['apple', 'grape', 'orange', 'pear']
fruit1.reverse()print(fruit1) #['pear', 'orange', 'grape', 'apple']
# 获取下标print(fruit1.index('apple')) #3
# 同时获取下标和值for index,item in enumerate(fruit1):
print(index,item)
# 结果 0 pear1 orange2 grape3 apple
元组
# 创建元组
fruit = ('apple','orange','grape')
# 常用功能print(fruit.count('apple')) #1print(fruit.index('orange')) #1
字典
# 创建
fruit = {1:'apple',2:'orange',3:'grape'}print(fruit)
# 增加
fruit[4] = 'pear'print(fruit) #{1: 'apple', 2: 'orange', 3: 'grape', 4: 'pear'}
# 修改
fruit[4] = 'peach'print(fruit) #{1: 'apple', 2: 'orange', 3: 'grape', 4: 'pear'}
# 删除
fruit.pop(4) #删除指定的keyprint(fruit) #{1: 'apple', 2: 'orange', 3: 'grape'}
# 查找valueprint(fruit.get(1)) #apple
fruit = {1:'apple',2:'orange',3:'grape'}
# 循环for k,v in fruit.items():
print(k,v)
1 apple2 orange3 grape
for k in fruit.keys():
print(k)
1
2
3
for v in fruit.values():
print(v)
apple
orange
grape
集合
# 创建
fruit = set(['apple','orange','pear'])print(fruit) #{'orange', 'pear', 'apple'}
# 添加
fruit.add('grape') #add只能添加一个print(fruit) #{'apple', 'orange', 'pear', 'grape'}
fruit.update(['peach','banana']) #update添加多个print(fruit){'banana', 'pear', 'apple', 'peach', 'grape', 'orange'}
# 删除
fruit.remove('banana') #删除指定的print(fruit)
fruit.pop() #随机删除print(fruit)
num1 = set([11,22,33,44])
num2 = set([33,44,55,66])
# 并集print(num1.union(num2)) #{66, 11, 22, 33, 44, 55}
# 差集print(num1.difference(num2)) #{11, 22}
# 交集print(num1.intersection(num2)) #{33, 44}
如果对这方面感兴趣或者在学习Python的过程中有什么问题的话,可以加君羊:588090942,君羊内有大量Python资料,也会有人解答问题,大家一起学习,一起交流。希望每位学习Python的人都能学有所成。返回搜狐,查看更多
责任编辑: