//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的更多相关文章

  1. Python Base of Scientific Stack(Python基础之科学栈)

    Python Base of Scientific Stack(Python基础之科学栈) 1. Python的科学栈(Scientific Stack) NumPy NumPy提供度多维数组对象,以 ...

  2. Python Base Four

    35. In python, file operation syntax is similar to c. open(file,'r',……) //the first parameters is ne ...

  3. Python Base One

    //this is my first day to study python, in order to review, every day i will make notes (2016/7/31) ...

  4. Python Base Five

    // 8 day(2016/8/11) 38. In python , it is oop. class Baskball:         def setName(self, name):      ...

  5. Python Base Three

    //sixth day to study python(2016/8/7) 32. In python , there are have an special type dictionary , it ...

  6. 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 ...

  7. python base 64

    python中base64编码与解码   引言: 在一些项目中,接口的报文是通过base64加密传输的,所以在进行接口自动化时,需要对所传的参数进行base64编码,对拿到的响应报文进行解码: Bas ...

  8. Python Base HTTP Server

    import BaseHTTPServer import cgi, random, sys MESSAGES = [ "That's as maybe, it's still a frog. ...

  9. 基于Python+协程+多进程的通用弱密码扫描器

    听说不想扯淡的程序猿,不是一只好猿.所以今天来扯扯淡,不贴代码,只讲设计思想. 0x00 起 - 初始设计 我们的目标是设计一枚通用的弱密码扫描器,基本功能是针对不同类型的弱密码,可方便的扩展,比如添 ...

随机推荐

  1. JS给数字加千位分隔符

    本文原链接:https://www.jianshu.com/p/928c68f92c0c JavaScript实现千位分隔符 将普通的数字转换为带千位分隔符格式的数字字符串是一个非常常见的问题,千位分 ...

  2. E - Polycarp and Snakes

    E - Polycarp and Snakes 题意:在一个全是点的图上开始画线,每次将一行或一列任意长度染成字母,一笔染一种字母,字母必须从a开始连续到后面某个字母可以覆盖. 问所给图案是否满足 , ...

  3. samba修改smb.conf后,不需要重启服务,就可生效

    在修改完smb.conf后,不需要重启服务.在Centos7.3与Ubuntu18.04上验证都没有问题. 猜测可能的原因:samba是在客户端进行连接时,smb服务程序读取smb.conf配置文件信 ...

  4. Spring多种方式实现依赖注入

    平常的Java开发中,程序员在某个类中需要依赖其它类的方法. 通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理. Spring提出了依赖注入的思想,即依赖类不由 ...

  5. Java 吃金币游戏设计与制作,下载版后补,代码没问题

    package com.swift; import java.awt.Color; import java.awt.Point; import java.awt.event.KeyEvent; imp ...

  6. ★iOS 性能测试工具 SDK

    一.概括 1. 做一个类似GT的性能测试工具: 2. 第一期主要是CPU.内存功能,要求可以绘制曲线,可以选择曲线区间,自动计算最小值.最大值.均值等,支持曲线全屏显示 目标的视觉效果是类似股票走势图 ...

  7. 【转】Windows 邮件槽(MailSlot)

    Windows 邮件槽(MailSlot) 来自<Windows网络编程第二版 中文版> 优点:通过网络,将一条消息广播给一台或多台计算机.   缺点:只允许从客户机到服务器,建立一种不可 ...

  8. 字节跳动后端开发实习生面试(Python)

    一面: 1.自我介绍. 2.介绍“工大小美”项目相关. 3.Python中的GIL(全局解释器锁),以及哪种情况下使用python的多线程性能有较大的提升. 4.项目中用到了SQLite数据库,如果有 ...

  9. Java 的Throwable、error、exception的区别

    1. 什么是异常? 异常本质上是程序上的错误,包括程序逻辑错误和系统错误.比如使用空的引用(NullPointerException).数组下标越界(IndexOutOfBoundsException ...

  10. VMware安装Ubuntu配置NAT模式下静态IP,解决访问外网问题

    安装好VMware后,打开网络连接可以看到有VMware Network Adapter VMnet1和VMware Network Adapter VMnet8两个网络适配器,VMnet1是针对桥接 ...