当前位置: 首页 > news >正文

网站开发的职业技术方面青岛网站推广系统

网站开发的职业技术方面,青岛网站推广系统,网站直播间怎么做,用户体验设计方法复习python从入门到实践——函数function 函数是特别难的,大家一定要好好学、好好复习、反复巩固。函数没学好,会为后面造成很大困扰。 教科书中函数举例会稍微有点复杂。在此章复习中,我将整理出容易疏漏和混淆的知识点,并用最简…

复习python从入门到实践——函数function

函数是特别难的,大家一定要好好学、好好复习、反复巩固。函数没学好,会为后面造成很大困扰。
教科书中函数举例会稍微有点复杂。在此章复习中,我将整理出容易疏漏和混淆的知识点,并用最简单的代码辅助大家理解。
本章涉及:定义函数、实参与形参、return返回值、传递列表与切片、将函数导入模块。

文章目录

  • 复习python从入门到实践——函数function
    • 1. 定义 def函数
      • Syntax:
    • 2.实参与形参:
      • 传递实参的方法:
      • 可选实参:
      • 通过*传递任意数量实参
      • 涉及字典 **可变关键字参数
    • 3.return返回
      • Syntax:
      • Principle:
      • 注意:
    • 4. 传递列表
      • [:]切片含义:
    • 5.将函数导入模块

1. 定义 def函数

Syntax:

def 函数():函数的具体表现(函数体)
函数() #调用

区分函数和变量

  1. 函数 add_numbers():
    接受输入的参数(a,b),并且要有返回值return。后面会介绍。
def add_numbers(a, b):# 定义一个函数return a + bresult = add_numbers(3, 5)#使用函数
print("结果:", result)

2.变量x,y
依靠=储存各种数据。

# 定义两个变量
x = 10
y = 20sum_of_variables = x + y # 使用变量
print("变量之和:", sum_of_variables)

2.实参与形参:

形参类似于中文里概括性质的类别,比如“同学”
实参是具体的例子,比如“小明”属于”同学“,”小明“是实参。

def great_user(username):"""显示问候语:""" #文本注释,三引号,描述函数做什么。print(f"Hello {username.title()}!") #函数的工作
great_user('Ashley') 

username 形参
Ashley 实参

传递实参的方法:

(1)对应位置

def animal_lists(category,name):print(f"My {category}'s name is {name.title()}.")
animal_lists('pig','feifei')

(2)关键词,用’='连接

def animal_lists(category,name):print(f"My {category}'s name is {name.title()}.")
animal_lists(name='feifei',category='pig')

(3)最后一项是默认值

def animal_lists(name, category='dog',):#把默认值放到最后。print(f"My {category}'s name is {name.title()}.")
animal_lists(name='feifei')

可选实参:

把可选可不选的参数放到最后一个
middle_name=’ ’

def formatted_name(first_name,last_name,middle_name=' '):if middle_name:formatted_name = f'{first_name} {middle_name} {last_name}'else:formatted_name = f'{first_name} {last_name}'return formatted_name.title()
students = formatted_name('Haifei',"Wang")
print(students)
students = formatted_name('Haifei','Wang','Pig')#最后一个是中间名
print(students)

通过*传递任意数量实参

def feifei_behaviors(*behavior):#通过加星号调用多个实参print(f'飞飞正在:{behavior}')
feifei_behaviors('拉粑粑')
feifei_behaviors('吃粑粑','吃屎','被茵茵看着拉屎')

涉及字典 **可变关键字参数

def build_profile(first,last,**user_info):#**可变关键字参数,除了First和last"""创建一个字典,其中包含我们知道的有关用户的一切"""profile = {}profile['first_name'] = firstprofile['last_name'] = lastfor key,value in user_info.items():profile[key] = valuereturn profile
user_profile = build_profile('albert','einstein',location='princeton',field='physics')
print(user_profile)

3.return返回

Syntax:

def function_name():具体的statementreturn statement中的函数

Principle:

== a. The statements after the return() statement are not executed. 在return()语句之后的语句不会被执行。
b. return() statement can not be used outside the function. return()语句不能在函数外部使用。
c. If the return() statement is without any expression, then the NONE value is returned. 如果return()语句没有任何表达式,则返回NONE值。==

举例:

def formatted_name(first_name,last_name):formatted_name = first_name+' '+ last_namereturn formatted_name()
students = formatted_name('Haifei',"Wang")
print(students)

结果: Haifei Wang

如果没有return语句

def formatted_name(first_name,last_name):formatted_name = first_name+' '+ last_name
students = formatted_name('Haifei',"Wang")
print(students)

结果是None

注意:

调用你定义的函数,而不是变量

def city_country(city, country):full_name = f'{city},{country}'return full_namewhile True:print("Please inter the city and country:")city = input('city:')country = input('country:')new_name = city_country(city, country)#调用定义变量print(new_name)	

4. 传递列表

普通方法:
def+循环+发送列表+调用列表

def greating_lists(names):#建立函数 # 问候 '''给列表中的用户打招呼'''for name in names:print(f'Hello! {name.title()}.')
usernames=['feifei','ashley','tony']# 发送列表
greating_lists(usernames)#调用列表-实参

[:]切片含义:

基本:
[start:stop:step]
用数学集合可以表示为:[ )
举例:
[1:4] 从索引1(包含)到索引4(不包含)也就是索引1 2 3 这几个元素。

重要的容易忽略:
表示列表序列
创建副本

original_list = [10, 20, 30, 40, 50]# 使用切片创建副本
copied_list = original_list[:]# 修改副本
copied_list[0] = 100print("Original List:", original_list)
print("Copied List:", copied_list)

原始列表 original_list 并没有被修改。这是因为切片创建了一个新的列表对象,而不是直接引用原始列表。

5.将函数导入模块

Syntax:
from 文件 import 要导入的东西
import 原来的东西名字 as 你想改的名字
from 文件 import *(所有函数)

http://www.ds6.com.cn/news/1676.html

相关文章:

  • 怎么注册一个属于自己的网站注册网站平台
  • wordpress情侣网站源码六年级下册数学优化设计答案
  • 免费下载asp.net4.0动态网站开发基础教程营销计划
  • 网站怎么才能被搜到站长工具seo综合查询权重
  • 医院网站和公众号建设方案网络广告是什么
  • 服务一流的做网站郑州seo技术
  • 怎么用APdiv做网站导航栏可以免费打开网站的软件
  • 什么编程语言做网站安全淄博网站推广
  • 浦口区网站建设售后保障上海哪家优化公司好
  • 网站开发违约责任天津百度seo代理
  • 做研究的网站班级优化大师免费下载安装
  • 天猫商城官网登录佛山seo培训机构
  • 做网站需要懂代码么营销渠道策略有哪些
  • vs2015网站开发美国最新消息今天 新闻
  • 做网站的IDE怎么推广自己的网站?
  • 网站开发还是做数据库开发网站优化外包推荐
  • 公司网站开发实施方案网站设计制作一条龙
  • 做英语翻译赚钱的网站搜索引擎优化排名优化培训
  • 在自己网站上做销售在工商要办什么手续seo外贸公司推广
  • 做套现网站免费收录链接网
  • 网站404页面怎么做百度网络优化推广公司
  • 给网站做压力测试微信营销号
  • 搜狐一开始把网站当做什么来做semir森马
  • 做国外网站的公证要多少钱如何做网页链接
  • 企业网站建设有几种新手怎样做网络推广
  • 网站备案要拍照大家怎么做的啊写一篇软文多少钱
  • 科技通信网站模板下载培训网络营销的机构
  • 龙岗外贸网站建设2023国内外重大新闻事件10条
  • 网站建设需要哪些功能搜索广告是什么意思
  • 深圳有做网站公司百度知道问答首页