1、函数定义,def
2、导入模块:import random
要调用模块的函数,需要先指明模块名。例:random.randint(1, 10)
3、print函数返回值为None,类型为NoneType。
4、print函数最多支持三个参数,end指定结束符(print('hello', end='')末尾不会有回车符号)
>>> print('cats', 'dogs', 'mice')
cats dogs mice
>>> print('cats', 'dogs', 'mice', sep=',')
cats,dogs,mice
5、global:由于python的变量不需要声明,因此如果在函数内部对全局变量直接赋值,将会创建局部变量,而不是更改了全局变量的值。使用global在函数内部声明局部变量,接下来的赋值操作则不会创建局部变量。
def func():
global eggs
eggs = 'spam'
eggs = 42
func()
print(eggs)
-----------------------
spam
-----------------------
def func():
#global eggs
eggs = 'spam'
eggs = 42
func()
print(eggs)
-----------------------
42
-----------------------
5、列表切片:
>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam[0:4] #第一个整数为开始的下标,第二个为切片结束的下标,但不包括它。
['cat', 'bat', 'rat', 'elephant']
>>> spam[0:-1] #支持负数
['cat', 'bat', 'rat']
>>> spam[:2] #省略第一个整数相当与使用0,
['cat', 'bat']
>>> spam[:] #省略第二个整数相当于使用列表的长度
6、列表连接和复制
+操作符用于连接两个列表,*操作符可以用于一个列表和一个整数,实现列表的复制
7、列表值的删除
>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> del spam[2]
>>> spam
>>>['cat', 'bat', 'elephant'] #删除后,后面所有值都将向前移动一个下标
>>> spam.remove('cat')
>>> spam
>>> ['bat', 'elephant']
8.元组
元组是列表的不可变形式,元组的值不可被修改,添加或删除
>>> type('hello')
>>> <class 'str'>
>>> type(('hello',))
>>> <class 'tuple'>
使用list()和tuple()方法可以在列表和元组之间进行转换
9.字典
使用get方法获取字典的值,get有两个参数:要取得其值的键,以及如果该键不存在时,返回的备用值。
>>> items = {'apples': 5, 'caps': 2}
>>> items.get('eggs', 0)
>>> 0
10.运行pip install,报错
Could not find a version that satisfies the requirement pyperclip (from versions: )
No matching distribution found for pyperclip
建议使用国内的源
pip install pymongo -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。