函数中使用的变量是局部的html
若是在外面的变量在函数中使用,须要加global
关键字python
APPLY = 100 # 全局变量 a = None def fun(): global a # 使用以前在全局里定义的 a a = 20 # 如今的 a 是全局变量了 return a+100 print(APPLE) # 100 print('a past:', a) # None fun() print('a now:', a) # 20
id()
能够显示内存地址git
列表能够经过引用其元素,改变对象自身(in-place change)。这种对象类型,称为可变数据对象(mutable object),词典也是这样的数据类型。github
数字和字符串,不能改变对象自己,只能改变引用的指向,称为不可变数据对象(immutable object)。元组(tuple),尽管能够调用引用元素,但不能够赋值,所以不能改变对象自身,因此也算是immutable object。正则表达式
当使用浅拷贝时,python
只是拷贝了最外围的对象自己,内部的元素都只是拷贝了一个引用而已。看代码:express
>>> import copy >>> a=[1,2,3] >>> c=copy.copy(a) #拷贝了a的外围对象自己, >>> id(c) 4383658568 >>> print(id(a)==id(c)) #id 改变 为false False >>> c[1]=22222 #此时,我去改变c的第二个值时,a不会被改变。 >>> print(a,c) [1, 2, 3] [1, 22222, 3] #a值不变,c的第二个值变了,这就是copy和‘==’的不一样
deepcopy
对外围和内部元素都进行了拷贝对象自己,而不是对象的引用。数组
#copy.copy() >>> a=[1,2,[3,4]] #第三个值为列表[3,4],即内部元素 >>> d=copy.copy(a) #浅拷贝a中的[3,4]内部元素的引用,非内部元素对象的自己 >>> id(a)==id(d) False >>> id(a[2])==id(d[2]) True >>> a[2][0]=3333 #改变a中内部原属列表中的第一个值 >>> d #这时d中的列表元素也会被改变 [1, 2, [3333, 4]] #copy.deepcopy() >>> e=copy.deepcopy(a) #e为深拷贝了a >>> a[2][0]=333 #改变a中内部元素列表第一个的值 >>> e [1, 2, [3333, 4]] #由于时深拷贝,这时e中内部元素[]列表的值不会由于a中的值改变而改变
建立二维数组数据结构
myList = [([0] * 3) for i in range(4)] #以上会出现浅拷贝, >>> lists = [[] for i in range(3)] >>> lists[0].append(3) >>> lists[1].append(5) >>> lists[2].append(7) #避免浅拷贝
var = var1 if condition else var2
若是 condition
的值为 True
, 那么将 var1
的值赋给 var
;若是为 False
则将 var2
的值赋给 var
。多线程
if condition1: true1_expressions elif condition2: true2_expressions elif condtion3: true3_expressions elif ... ... else: else_expressions
若是有多个判断条件,那能够经过 elif
语句添加多个判断条件,一旦某个条件为 True
,那么将执行对应的 expression
。app
在 Python 内置了工厂函数,range
函数将会返回一个序列,总共有三种使用方法
其中 start
将会是序列的起始值,stop
为结束值,可是不包括该值,相似 数学中的表达 [start, stop)
,左边为闭区间,右边为开区间。
for i in range(1, 10): print(i)
上述表达将会返回 1-9
全部整数,但不包含 10
若是省略了 start
那么将从 0 开始,至关于 range(0, stop)
step
表明的为步长,即相隔的两个值得差值。从 start
开始,依次增长 step
的值,直至等于或者大于 stop
for i in range(0,13, 5): print(i)
将会输出 0, 5, 10
。
Python 共内置了 list
、 tuple
、dict
和 set
四种基本集合,每一个集合对象都可以迭代。
在 Python 中集合类型有 list
、 tuple
、dict
和 set
等,若是该集合对象做为 while 判断语句, 若是集合中的元素数量为 0,那么将会返回 False
, 不然返回 True
。
a = range(10) while a: print(a[-1]) a = a[:len(a)-1]
上述程序将会返回 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
, 程序首先判断列表是否空,若是不为空,则 打印出最后一个内容,而后使用切片
操做去掉最后一个元素,并更新列表;如此重复,直至列表为空。
variable=input()
表示运行后,能够在屏幕中输入一个数字,该数字会赋值给自变量。
python
中 print
字符串 要加''
或者""
\n
换行命令
\t
达到 tab
对齐
使用 open
可以打开一个文件, open
的第一个参数为文件名和路径 ‘my file.txt’, 第二个参数为将要以什么方式打开它, 好比 w
为可写方式. 若是计算机没有找到 ‘my file.txt’ 这个文件, w
方式可以建立一个新的文件, 并命名为 my file.txt
my_file=open('my file.txt','w') #用法: open('文件名','形式'), 其中形式有'w':write;'r':read. my_file.write(text) #该语句会写入先前定义好的 text my_file.close() #关闭文件
open('my file.txt','a')
打开类型为 a
,a
即表示 append。
使用 file.read()
可以读取到文本的全部内容.
file= open('my file.txt','r') content=file.read() print(content) """" This is my first test. This is the second line. This the third line. This is appended file. """"
若是想在文本中一行行的读取文本, 可使用 file.readline()
, file.readline()
读取的内容和你使用的次数有关, 使用第二次的时候, 读取到的是文本的第二行, 并能够以此类推:
file= open('my file.txt','r') content=file.readline() # 读取第一行 print(content) """" This is my first test. """" second_read_time=file.readline() # 读取第二行 print(second_read_time) """ This is the second line. """
若是想要读取全部行, 并可使用像 for
同样的迭代器迭代这些行结果, 咱们可使用file.readlines()
, 将每一行的结果存储在 list
中, 方便之后迭代。
file= open('my file.txt','r') content=file.readlines() # python_list 形式 print(content) """" ['This is my first test.\n', 'This is the second line.\n', 'This the third line.\n', 'This is appended file.'] """" # 以后若是使用 for 来迭代输出: for item in content: print(item) """ This is my first test. This is the second line. This the third line. This is appended file. """
(tuple)
[list]
{dict}
函数声明只须要在须要默认参数的地方用 =
号给定便可, 可是要注意全部的默认参数都不能出如今非默认参数的前面。
若是想要在执行脚本的时候执行一些代码,好比单元测试,能够在脚本最后加上单元测试 代码,可是该脚本做为一个模块对外提供功能的时候单元测试代码也会执行,这些每每咱们不想要的,咱们能够把这些代码放入脚本最后:
if __name__ == '__main__': #code_here
若是执行该脚本的时候,该 if
判断语句将会是 True
,那么内部的代码将会执行。 若是外部调用该脚本,if
判断语句则为 False
,内部代码将不会执行。
顾名思义,函数的可变参数是传入的参数能够变化的,1个,2个到任意个。固然能够将这些 参数封装成一个 list
或者 tuple
传入,但不够 pythonic
。使用可变参数能够很好解决该问题,注意可变参数在函数定义不能出如今特定参数和默认参数前面,由于可变参数会吞噬掉这些参数。
def report(name, *grades): total_grade = 0 for grade in grades: total_grade += grade print(name, 'total grade is ', total_grade)
定义了一个函数,传入一个参数为 name
, 后面的参数 *grades
使用了 *
修饰,代表该参数是一个可变参数,这是一个可迭代的对象。该函数输入姓名和各科的成绩,输出姓名和总共成绩。因此能够这样调用函数 report('Mike', 8, 9)
,输出的结果为 Mike total grade is 17
, 也能够这样调用 report('Mike', 8, 9, 10)
,输出的结果为 Mike total grade is 27
关键字参数能够传入0个或者任意个含参数名的参数,这些参数名在函数定义中并无出现,这些参数在函数内部自动封装成一个字典(dict).
def portrait(name, **kw): print('name is', name) for k,v in kw.items(): print(k, v)
定义了一个函数,传入一个参数 name
, 和关键字参数 kw
,使用了 **
修饰。代表该参数是关键字参数,一般来说关键字参数是放在函数参数列表的最后。若是调用参数portrait('Mike', age=24, country='China', education='bachelor')
输出:
name is Mike age 24 country China education bachelor
经过可变参数和关键字参数,任何函数均可以用 universal_func(*args, **kw)
表达。
在类中,方法外的变量,全部实例对象公用
在类方法中,通常在__init__
中声明定义,对象之间独立
单下划线是Protect
双下划线是Private
设置属性的默认值, 直接在def
里输入便可,以下:
def __init__(self,name,price,height=10,width=14,weight=16):
这里和视频有点差异,我本身写了另一个模块,是计算五年复利本息的模块,代码以下:模块写好后保存在默认文件夹:balance.py
d=float(input('Please enter what is your initial balance: \n')) p=float(input('Please input what is the interest rate (as a number): \n')) d=float(d+d*(p/100)) year=1 while year<=5: d=float(d+d*p/100) print('Your new balance after year:',year,'is',d) year=year+1 print('your final year is',d)
新开一个脚本,import balance
import balance """" Please enter what is your initial balance: 50000 # 手动输入个人本金 Please input what is the interest rate (as a number): 2.3 #手动输入个人银行利息 Your new balance after year: 1 is 52326.45 Your new balance after year: 2 is 53529.95834999999 Your new balance after year: 3 is 54761.14739204999 Your new balance after year: 4 is 56020.653782067144 Your new balance after year: 5 is 57309.12881905469 your final year is 57309.12881905469 """"
在Mac系统中,下载的python模块会被存储到外部路径site-packages
,一样,咱们本身建的模块也能够放到这个路径,最后不会影响到自建模块的调用。
输出错误:try:
, except ... as ...:
看以下代码
try: file=open('eeee.txt','r') #会报错的代码 except Exception as e: # 将报错存储在 e 中 print(e) """ [Errno 2] No such file or directory: 'eeee.txt' """
处理错误:会使用到循环语句。首先报错:没有这样的文件No such file or directory
. 而后决定是否输入y
, 输入y
之后,系统就会新建一个文件(要用写入的类型),再次运行后,文件中就会写入ssss
try: file=open('eeee.txt','r+') except Exception as e: print(e) response = input('do you want to create a new file:') if response=='y': file=open('eeee.txt','w') else: pass else: file.write('ssss') file.close() """ [Errno 2] No such file or directory: 'eeee.txt' do you want to create a new file:y ssss #eeee.txt中会写入'ssss'