IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> Python study notes Day 7 ‘function‘ -> 正文阅读

[Python知识库]Python study notes Day 7 ‘function‘

Python study notes Day 7 ‘function’

Today’s topic is about the function.
This is an extremely important part in the world of computer programming.
We can define a function by naming it and describe it in detail within the structure defined.

def function():
The contents of the function
function()#The function is called

Screen Shot 1

def function1():
    a = 1
    b = 2
    c = a + b
    print(a)
    print(b)
    print(c)
    
function1()

Or, we can define functions with parameters.
Parameters are local variables that have the property of acting only inside the function.
Screen Shot 2

def function2(a,b):
    c = a + b
    print(a)
    print(b)
    print(c)
    
function2(3,4)

In the picture, ‘a’ and ‘b’ are parameters and when we call a function, we assign values to both parameters.
Or, we can define and assign two global variables, and then assign the two global variables to the two parameters when the function is called.
Screen Shot 3

m = 5
n = 6
function2(m,n)
function2(n,m)

Or, we can also define a function by assigning default values to each parameter.
Screen Shot 4

def function3(a=7,b=8):
    c = a + b
    print(a)
    print(b)
    print(c)
    
function3()

Screen Shot 5

a = 1
b = 2
def function3(a=7,b=8):
    c = a + b
    print(a)
    print(b)
    print(c)
    
function3()

Screen Shot 6

def function3(a=7,b=8):
    c = a + b
    print(a)
    print(b)
    print(c)
    
function3(9)
function3(9,10)

When we want to use global variables in a function:
Screen Shot 7

a = 11
def function3(b=8):
    global a
    c = a + b
    print(a)
    print(b)
    print(c)
    
function3()

Sets the return value of the functionL
Screen Shot 8

def add(a,b):
    c = a + b
    return c

n = add(1,2)
print(n)
  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2021-08-07 12:01:58  更:2021-08-07 12:02:36 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/17 14:45:09-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码