- 函数的参数
函数的参数可以有默认值(带默认值的参数一定要统一放在最后),也支持使用可变参数,传递参数时可以不按照设定的顺序进行传递,所以Python不需要像其他语言一样支持函数的重载,因为在定义一个函数的时候可以让它有多种不同的使用方式。
字符串和常用数据结构
- 使用字符串
字符串就是由零个或多个字符组成的有限序列
#1. 通过len函数计算字符串的长度
def main():
a1 = 'hello world!'
print(len(a1))
main()
运行结果:
12
#2.获得字符串首字母大写的输出
def main():
a1 = 'hello world!'
print(a1.capitalize())
main()
运行结果:
Hello world!
#3. 获得字符串变大写后的输出
def main():
a1 = 'hello world!'
print(a1.upper())
main()
运行结果:
HELLO WORLD!
#4.从字符串中查找子串所在位置
def main():
a1 = 'hello world!'
print(a1.find('or'))
main()
运行结果:
8
#5.检查字符串是否以指定的字符串开头
def main():
a1 = 'hello world!'
print(a1.startswith('He'))
main()
运行结果:
False
#6.检查字符串是否以指定的字符串结尾
def main():
a1 = 'hello world!'
print(a1.endswith('!')
main()
运行结果:
True
#7.将字符串以指定的宽度居中并在两侧填充指定的字符
def main():
a1 = 'hello world!'
print(a1.center(50, '*'))
main()
运行结果:
*******************hello world!*******************
#8.将字符串以指定的宽度靠右放置左侧填充指定的字符
def main():
a1 = 'hello world!'
print(a1.rjust(50, ' '))
main()
#9. 从字符串中取出指定位置的字符(下标运算)
def main():
a2 = 'abc123456'
print(a2[2])
main()
#10.检查字符串是否由数字构成
def main():
a2 = 'abc123456'
print(a2.isdigit())
main()
运行结果:
False
#11.检查字符串是否以字母构成
def main():
a2 = 'abc123456'
print(a2.isalnum())
main()
#12.获得字符串修剪左右两侧空格的拷贝
def main():
a3 = ' baibailu@112.com '
print(a3.strip())
print(a3.lstrip())
print(a3.rstrip())
main()
除了字符串,Python还内置了多种类型的数据结构,如果要在程序中保存和操作数据,绝大多数时候可以利用现有的数据结构来实现,最常用的包括列表、元组、集合和字典。
- 使用列表
#1.计算列表长度(元素个数)
d
计算列表长度(元素个数)
def main():
list1 = [2, 4, 5, 7, 78]
print(len(list1))
main()
运行结果:
5
#2.下标(索引)运算
def main():
list1 = [2, 4, 5, 7, 78]
print(list1[4])
main()
运行结果:
78
#3.添加元素
def main():
list1 = [2, 4, 5, 7, 78]
list1.append(100)
list1.insert(1, 400)
list1 += [101, 202]
print(list1)
main()
运行结果:
[2, 400, 4, 5, 7, 78, 100, 101, 202]
#4.删除元素
def main():
list1 = [2, 4, 5, 7, 78]
list1.remove(4)
print(list1)
main()
运行结果:
[2, 5, 7, 78]
#5.清空列表元素
def main():
list1 = [2, 4, 5, 7, 78]
list1.clear()
print(list1)
main()
运行结果:
[]
#6.去重
a = [1,1,2,3,1,4,5,3,1,6,6]
b = []
for i in a:
if i not in b:
b.append(i)
print(b)
运行结果:
[1, 2, 3, 4, 5, 6]
和字符串一样,列表也可以做切片操作,通过切片操作可以实现对列表的复制或将列表中的一部分取出来创建出新的列表
#1. 循环遍历列表元素
def main():
fruits = ['grape', 'apple', 'strawberry', 'waxberry']
fruits += [''pear', 'mango']
for fruit in fruits:
print(fruit.title(), end=' ')
print()
运行结果:
Grape Apple Strawberry Waxberry Pear Mango
#2.列表切片
def main():
fruits = ['grape', 'apple', 'strawberry', 'waxberry']
fruits1 = fruits[1:2]
print(fruits1)
main()
运行结果:
['apple']
#3.可以通过反向切片操作来获得倒转后的列表的输出
def main():
fruits = ['grape', 'apple', 'strawberry', 'waxberry']
fruits2 = fruits[::-1]
print(fruits2)
main()
运行结果:
['waxberry', 'strawberry', 'apple', 'grape']
当你要在其他py文件中引用本文件中的任何东西,如果你不想在其他文件中运行该文件中的某些代码,就可以使用“if name == ‘main’:”
- 使用元组
元组与列表类似,不同之处在于元组的元素不能修改,把多个元素组合到一起就形成了一个元组,所以它和列表一样可以保存多条数据。
#1.获取元组中的元素
def main():
# 定义元组
t = ('wd', 38, True, 'shanghai')
print(t)
print(t[1])
main()
运行结果:
38
#2.重新给元组赋值
def main():
t = ('lulu', 38, True, 'shanghai')
t[0]
#3.将列表转换成元组
def main():
fruits_list = ['apple', 'banana', 'orange']
fruits_tuple = tuple(fruits_list)
print(fruits_tuple)
main()
运行结果:
('apple', 'banana', 'orange')
1.如果不需要对元素进行添加、删除、修改的时候,可以考虑使用元组
2.如果一个方法要返回多个值,可以使用元组
3.元组在创建时间和占用的空间上面都优于列表
- 使用集合
与列表和元组不同,集合的最大特点是:集合里边的元素是不可重复的并且集合内的元素还是无序的。
一般情况下集合常用的两个场景是:
- 去重(如:列表去重);
- 关系测试(如:取交集、取并集、取差集等)
- 使用字典
字典是另一种可变容器模型,可以存储任意类型对象,与列表、集合不同的是,字典的每个元素都是由一个键和一个值组成的“键值对”,键和值通过冒号分开。
字典是无序的,没有索引操作,除非转换成有序字典。 - dict = {key:value}
#1.通过键可以获取字典中对应的值
def main():
scores = {'ww': 95, 'wx': 78, 'xx': 82}
print(scores['ww'])
main()
运行结果:
95
#2. 对字典进行遍历(遍历的其实是键再通过键取对应的值)
def main():
scores = {'ww': 95, 'wx': 78, 'xx': 82}
for i in scores:
print('%s\t--->\t%d' % (rong, scores[i]))
main()
运行结果:
ww ---> 95
wx ---> 78
xx ---> 82
#3.更新字典中的元素
def main():
scores = {'ww': 95, 'wd': 78, 'hy': 82}
scores['wd'] = 65
print(scores)
main()
运行结果:
{'ww': 95, 'wd': 65, 'hy': 82}
#4. get方法也是通过键获取对应的值但是可以设置默认值
#5.删除字典中的元素
def main():
scores = {'wd': 95, 'ww': 78, 'xx': 79}
pop_obj = scores.pop('wd')
print(pop_obj)
main()
#6.清空字典
def main():
scores = {'wd': 95, 'ww: 78, 'xx': 79}
scores.clear()
print(scores)
main()
- 匿名函数
(lambda x,y:print(x,y))(100,10)
关键字lambda表示匿名函数,冒号前面的表示函数参数。
匿名函数有个限制,就是只能有一个表达式,不用写return,返回值就是该表达式的结果。
用匿名函数有个好处,因为函数没有名字,不用担心函数名冲突。匿名函数也是一个函数对象,也可以把匿名函数赋值给一个变量,再利用变量来调用该函数。