@zhangyy
2019-12-11T10:17:14.000000Z
字数 5636
阅读 254
Python学习
一: Python的 文件操作
二: Python的 函数
三: Python函数的局部变量
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:zhangyy
f = open("yesterday","r",encoding="utf-8") # 打开文件
data = f.read()
print(data)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:zhangyy
f = open("yesterday1","w",encoding="utf-8") # 打开文件
f.write("zhangyy")
f.write("anebi")
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:zhangyy
f = open("yesterday1","a",encoding="utf-8") # 打开文件
f.write("homeupdate,")
f.write("yangyang")
print(f)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:zhangyy
# f = open("yesterday","r",encoding="utf-8").read()
f = open("yesterday","r",encoding="utf-8")
for i in range(6):
print(f.readline())
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:zhangyy
f = open("yesterday","r",encoding="utf-8")
for index,line in enumerate(f.readlines()):
if index == 9:
print('----分割线-----')
continue
print(line.strip())
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:zhangyy
f = open("yesterday","r",encoding="utf-8")
count = 0
for line in f:
if count == 9:
print("----分割线-----")
count +=1
continue
print(line)
count +=1
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:zhangyy
f = open('yesterday','r',encoding='utf-8')
print(f.tell()) ### 定位标示符
print(f.read(5))
print(f.tell())
print(f.seek(0)) ### 光标返回
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:zhangyy
import sys
import time
'''
f = open("yesterday2","w",encoding="utf-8") # 打开文件
f.write("hello1,\n")
f.write("hello2,\n")
'''
for i in range(20):
sys.stdout.write("#")
sys.stdout.flush()
time.sleep(0.1)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:zhangyy
f = open("yesterday3","a",encoding="utf-8")
f.seek(10)
f.truncate(20)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:zhangyy
f = open("yesterday2","r+",encoding="utf-8") # 读写文件
g = open("yesterday4",'w+',encoding="utf-8") # 写读文件
t = open("yesterday4",'a+',encoding="utf-8") # 追加读写
y = open("yesterday4",'rb') ## 以二进制的读写
r = open("yesterday4",'wb') ### 以二进制的方式写读
z = open("yesterday4",'wb') ### 以二进制的方式追加读写
r.write("hello binary\n".encode())
r.close()
'''
g.write("spark,\n")
g.write("spark,\n")
print(g.readline())
print(g.readline())
g.seek(5)
g.write("------hadoop-------")
print(g.readline())
g.seek(5)
'''
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:zhangyy
with open("yesterday","r",encoding="utf-8") as f:
for line in f:
print(line)
# print(f.readline())
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:zhangyy
### 函数
def func1():
print('in thi func1')
return 0
# 过程 ---- 过程就是没用返回值的函数
def func2():
print("in the func2")
x = func1()
y = func2()
print("from funcl return is %s" %x)
print('from fun2 return is %s' %y)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:zhangyy
def test1():
pass
def test2():
return 0
def test3():
return 0,'hello',['a','b','c'],{'name':'alex'}
x=test1()
y=test2()
z=test3()
print(x)
print(y)
print(z)
### python 的返回值最后 把所有的返回值 放到一个元组里面
#### 函数的返回值:
#### 默认没有返回值就是 none
#### 返回值 = 1 默认的返回值 就是 一个object (对象)
#### 返回值 > 1 默认返回值就是就是一个元组 (tuple)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:zhangyy
def test(x,y,z):
print(x)
print(y)
print(z)
# test(y=2,x=1) #与形参顺序无关
# test(1,2) #与形参一一对应
#test(x=2,3)
test(3,z=2,y=6)
def conn(host,port=3306):
pass
### #默认参数特点:调用函数的时候,默认参数非必须传递
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:zhangyy
def test1(x,*args):
print(x)
print(args)
test1(1,2,3,4,5,6,7)#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:zhangyy
#*args: 接收N 个 位置参数,转换成元组的方式
#*kwargs:接收N 关键字参数,转换成字典的方式
def test1(x,*args):
print(x)
print(args)
test1(1,2,3,4,5,6,7)
def test2(**kwargs):
print(kwargs)
print(kwargs['name']) ### 取值
test2(name="zhangyy",age="8",sex = "boy")
#test2(**{'name':'alex','age':8})
def test3(name,**kwargs):
print(name)
print(kwargs)
test3("alex",age =18,sex="boy")
def test4(name,age =18,**kwargs):
print(name)
print(age)
print(kwargs)
test4("alex",sex="boy")
def test5(name,age=18,*args,**kwargs):
print(name)
print(age)
print(args)
print(kwargs)
test5('zhangyy',age=34,sex='boy',hobby='wtt')
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:zhangyy
def change_name(name):
print("before change",name)
name = "flyfish" #### 局部变量问题 ,只在函数内生效。
print("after change",name)
name = "zhangyy"
change_name(name)
print(name)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:zhangyy
school = "edu,oldboy"
def change_name(name):
global school #### 更改 局部变量的 提权问题
school = "mage linux"
print("before change",name,school)
name = "flyfish"
age = 23
print("after change",name)
name = "zhangyy"
change_name(name)
print(name)
print(school)
#### 不应该在函数里面改全局变量,特殊情况下才使用,一般不用
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:zhangyy
bigdata = ["spark","hadoop","hue","oozie"]
def change_name():
bigdata[0] = "zookeeper"
print(bigdata)
change_name()
print(bigdata)
#### 列表,集合, 字典 ,包括 类都可以修改
递归的特性
1. 必须有一个明确的结束条件
2. 每次进入更深一层递归时,问题规模相比上次递归都应有所减少
3. 递归效率不高,递归层次过多会导致栈溢出(在计算机中,函数调用是通过栈(stack)这种数据结构实现的,每当进入一个函数调用,栈就会加一层栈帧,每当函数返回,栈就会减一层栈帧。由于栈的大小不是无限的,所以,递归调用的次数过多,会导致栈溢出)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:zhangyy
data = [1, 3, 6, 7, 9, 12, 14, 16, 17, 18, 20, 21, 22, 23, 30, 32, 33, 35]
def binary_search(dataset, find_num):
print(dataset)
if len(dataset) > 1:
mid = int(len(dataset) / 2)
if dataset[mid] == find_num: # find it
print("找到数字", dataset[mid])
elif dataset[mid] > find_num: # 找的数在mid左面
print("\033[31;1m找的数在mid[%s]左面\033[0m" % dataset[mid])
return binary_search(dataset[0:mid], find_num)
else: # 找的数在mid右面
print("\033[32;1m找的数在mid[%s]右面\033[0m" % dataset[mid])
return binary_search(dataset[mid + 1:], find_num)
else:
if dataset[0] == find_num: # find it
print("找到数字啦", dataset[0])
else:
print("没的分了,要找的数字[%s]不在列表里" % find_num)
binary_search(data, 66)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:zhangyy
def calc (n):
print(n)
if int (n/2) >0:
return calc(int(n/2))
print("-->",n)
calc(10)
变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数,这种函数就称之为高阶函数。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:zhangyy
def add(x, y, f):
return f(x) + f(y)
res = add(3, -6, abs)
print(res)