Python Base Two
//fourth day to study python
24. In python , how to create funcation.
we can use def to define funcation. such as:
def MyFirstFuncation():
print('this is my first funcation')
MyFirstFuncation()
-> this is my first funcatio.
if there are no MyFirstFuncation , it will be have an error.
of course , we can pass a parameter
def MySecondFuncation(language):
print(' i love ' + language)
MySecondFuncation('python')
->i love python
if parameter is more , we can use comma divided parameter, such as:
def AddFuncation(num1,num2):
print(num1 + num2)
-> 3
how to use return:
def SubFuncation(num1,num2):
return (num1 - num2)
print(SubFuncation(6,3))
-> 3
ok , next to study
def myFuncation(m):
'do you know what is funcation'
#this is secret
print('my'+m+'roy')
myFuncation.__doc__
-> do you know what is funcation
next example:
def sortFuncation(str1,str2):
print(str1,str2)
sortFuncation('second','first')
-> second first
sortFuncation(str2 = 'second', str1 = 'first')
-> first second
default funcation:
def defaultFuncation(name='wyg',age='12'):
print(my name is '+name+'age is '+age)
defaultFuncation()
-> my name is wyg age is 12
def textPara(*paras):
print('parameters length is:',len(paras))
print('the second parameters is:',paras[1])
textPara(1,2,3,'roy')
->
parameters length is: 4
the second parameters is: 2
def textPara(*paras,exp):
print('parameters length is:',len(paras),exp)
print('the second parameters is:',paras[1])
textPara(1,2,3,'roy',exp = 6)
->
parameters length is: 4 6
the second parameters is: 2
//fifth day to study python(2016/8/6)
25. In python ,how to return one or more value, such as:
def back():
return [1,3.14,'roy']
print(back())
-> [1,3.14,'roy']
def back():
return 1,3.14.'roy'
print(back())
-> (1,3.14,'roy')
26. In python , Local Variable and Global Variable . Look at example,
def textFuncation(num1, num2):
result = num1 * num2
return result
num1_input = float(input('input num1:'))
num2_input = float(input('input num2:'))
res = textFuncation(3,5)
print(result is:',res)
-> result is 15
now we analyse which is local variable and which is global variable
num1,num2,result is local variable
num1_input,num2_input,res is global variable
but we need to know if we modify global variable in funcation , global variable will not change , it will create a local variable with the same name global name ,we can access global variable in funcation ,but try not to modify it. if you want to mofify it , we continue to study it.
27. In python , if you want to change global variable in funcation ,how to achieve it.
count = 5
def myFun():
global count
count = 10
print(count)
myFun()
print(count)
-> 10, 10
if you not to use global ,the result is 10, 5
28. In python , how to invoke a funcation in another funcation, the follow is most simple example:
def fun1():
print('fun1 is using')
def fun2():
print('fun2 is using')
fun2()
fun1()
->
fun1 is using
fun2 is using
fun2 not invole by other except fun1
29. In python , support lambda, such as:
def fun(x):
return 2 * x + 1
fun(5)
-> 11
g = lambda x : 2 * x + 1
g(5)
-> 11
g = lambda x, y : x + y
g(3,4)
-> 7
30. In python , there are two important built-in funcation i will introduce it
first is filter(None or funcation, iterable)
such as :
temp =filter(None,[1,0,False,True,3.14,-4])
->
[1,True,3.14,-4] #result is true
def odd(x):
return x % 2
temp = range(10)
show = filter(odd,temp)
list(show)
->
[1,3,5,7,9]
<->
list(filter(lambda x:x % 2, range(10)))
-> [1,3,5,7,9]
the second is map()
list(map(lambda x:x * 2, range(5)))
-> [0,2,4,6,8]
31. In python ,there are alse have recursion.
by default ,the depth is 100 , but you can change it.
import sys
sys.setrecursionlimit(1000)
def recursion():
return recursion()
recursion()
now we can count a simple question:
how to achieve 1 * 2 * 3 * 4 * 5
def funcation(n):
result = n
for i in range(1, n)
result *= i
return result
print(funcation(5))
-> 120
now we can change a way to achieve it ,by recurtion:
def recurtion(n):
if n ==1:
return 1
else:
return n * recurtion(n-1)
print(recurtion(5))
-> 120
we can see another question, how to achieve :1, 1, 2, 3, 5, 8, 13, 21
as you know ,the regular is :
f(1) = 1,
f(2) = 1,
f(n) = f(n-1)+f(n-2)
def funcation(n):
if n == 1 or n == 2:
return 1
else:
return funcation(n - 1) * funcation(n - 2)
print(funcation(6))
-> 8
now we see a classic question hanoi:
def hanoi:(n,x,y,z):
if n == 1:
print(x,'->,z)
else:
hanoi(n-1,x,z,y)
print(x,'->',z)
hanoi(n-1,y,x,z)
Python Base Two的更多相关文章
- Python Base of Scientific Stack(Python基础之科学栈)
Python Base of Scientific Stack(Python基础之科学栈) 1. Python的科学栈(Scientific Stack) NumPy NumPy提供度多维数组对象,以 ...
- Python Base Four
35. In python, file operation syntax is similar to c. open(file,'r',……) //the first parameters is ne ...
- Python Base One
//this is my first day to study python, in order to review, every day i will make notes (2016/7/31) ...
- Python Base Five
// 8 day(2016/8/11) 38. In python , it is oop. class Baskball: def setName(self, name): ...
- Python Base Three
//sixth day to study python(2016/8/7) 32. In python , there are have an special type dictionary , it ...
- 2019-04-18 Python Base 1
C:\Users\Jeffery1u>python Python 3.7.3 (default, Mar 27 2019, 17:13:21) [MSC v.1915 64 bit (AMD64 ...
- python base 64
python中base64编码与解码 引言: 在一些项目中,接口的报文是通过base64加密传输的,所以在进行接口自动化时,需要对所传的参数进行base64编码,对拿到的响应报文进行解码: Bas ...
- Python Base HTTP Server
import BaseHTTPServer import cgi, random, sys MESSAGES = [ "That's as maybe, it's still a frog. ...
- 基于Python+协程+多进程的通用弱密码扫描器
听说不想扯淡的程序猿,不是一只好猿.所以今天来扯扯淡,不贴代码,只讲设计思想. 0x00 起 - 初始设计 我们的目标是设计一枚通用的弱密码扫描器,基本功能是针对不同类型的弱密码,可方便的扩展,比如添 ...
随机推荐
- 交叉熵cross entropy和相对熵(kl散度)
交叉熵可在神经网络(机器学习)中作为损失函数,p表示真实标记的分布,q则为训练后的模型的预测标记分布,交叉熵损失函数可以衡量真实分布p与当前训练得到的概率分布q有多么大的差异. 相对熵(relativ ...
- centos7 python3 Saltstack配置
Python安装完毕后,提示找不到ssl模块 pip is configured with locations that require TLS/SSL, however the ssl module ...
- c++链表-双向链表+增删查改
基于双向链表的增删改查和排序(C++实现) 双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱.所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前 ...
- python之函数的初识
1. 面向过程编程的缺点 代码重复 代码可可读性不高 2. 函数的定义*** 函数是以功能为导向,一个函数封装一个功能.登录,注册,文件的改的操 3.函数的作用*** 函数减少代码的重复性,增 ...
- PAT (Basic Level) Practise (中文)-1020. 月饼 (25)
http://www.patest.cn/contests/pat-b-practise/1020 月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量. ...
- 27. Remove Element@python
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
- 学习笔记(三): Generalization/Overfitting/Validation
目录 Generalization: Peril of Overfitting Low loss, but still a bad model? How Do We Know If Our Mod ...
- 哈希表(Hash Table)/散列表(Key-Value)
目录 1. 哈希表的基本思想 2. 哈希表的相关基本概念 1.概念: 2.哈希表和哈希函数的标准定义: 1)冲突: 2)安全避免冲突的条件: 3)冲突不可能完全避免 4)影响冲突的因素 3. 哈希表的 ...
- 理解JWT的使用场景和优劣
理解JWT的使用场景和优劣 淘楼小能手 百家号04-2816:20 经过前面两篇文章<JSON Web Token - 在Web应用间安全地传递信息><八幅漫画理解使用JSON We ...
- 在 Ubuntu 环境下实现插入鼠标自动关闭触摸板
Ubuntu 以及其他衍生版本,如 Linux Mint 等等都可以用官方的 PPA 来安装"触摸板指示"应用程序.打开一个终端,运行以下命令: sudo add-apt-repo ...