小奥的学习笔记

  • Home
  • Learning & Working
    • Speech Enhancement Notes
    • Programming language
    • Computer & DL
    • MOOC
  • Life
    • Life Time
    • Thinking & Comprehension
    • Volunteer
    • Plan
    • Travel
  • Footprints
  • GuestBook
  • About
    • About Me
    • 个人履历
    • 隐私策略
  1. 首页
  2. Study-notes
  3. Programming language
  4. Python
  5. 正文

Python chapter 8 learning notes

2017年10月17日 1190点热度 0人点赞 0条评论

·定义一个函数

For example,

def greet_user():#定义函数
print("Hello!")
Now, we just need to input greet_user() and Hello! will be shown on the screen.

o   向函数传递信息

其实这一点和C语言基本是一致的。这个函数其实就是一个子函数,可以通过括号中的参数向子函数传递信息。

例子:

# -*-coding: utf-8 -*-
def greet_user(username):
 
print("Hello!" + username.title())
 
name = input("Please input your name.\n")
greet_user(name)

在这里面,形参就是指子函数def greet_user(username)括号里面的,实参就是最后一行里面的name所代表的。

o   关键字实参

def describe_pets(animal_type, pet_name):
print("\nI have a " + animal_type + ".")
print("My " + animal_type + "'s name is " + pet_name.title() + ".")
describe_pets(animal_type = 'cat', pet_name = 'hurry')#关键字实参

关键字实参的顺序无关紧要。但是普通的顺序必然非常重要。

o   默认值

def describe_pets(pet_name, animal_type = 'dog'):#默认值设置
print("\nI have a " + animal_type + ".")
print("My " + animal_type + "'s name is " + pet_name.title() + ".")
#typ = input("Please input the type of your pet.\n")
#name = input("Please input its name.\n")
 
#describe_pets(typ,name)
describe_pets(pet_name = 'hurry')#关键字实参

设置了默认值之后,默认值项得在形参括号中的最后。设置默认值之后,如果该参数不再修改,那么这个参数就自动为默认值,但是如果后面对其进行了传递,则使用传递的值而不再是默认值。设置了默认值之后,实参可以少一个(本例中,实参只剩1个)

·        返回值

o   返回简单值。此处返回值与C语言中类似。

o   让实参变成可选。可以利用指定一个默认值,即为空值即可。用到就在后面实参里写上,用不着就直接用默认的空。

o   返回字典。例如,

def build_person(first_name, last_name):
person = {'first': first_name,'last': last_name}
return person
musician = build_person('jimi', 'hendrix')
print(musician)

·        传递列表

o   在函数中修改列表

For example,

def print_models(unprinted_designs, completed_models):
while unprinted_designs:
current_design = unprinted_designs.pop()
print("Printing model: " + current_design)
completed_models.append(current_design)

Finally, unprinted_designs will not have any element. But, what if we want to retain these elements? The answer is as followed:

Use slice. function_name(list_name[:]).

For example, when we call function print_models(unprinted_designs, completed_models),we need to change it to print_models(unprinted_designs[:], completed_models).

·        Pass lots of actual parameter

o   Using *name as a formal parameter, this is just like a list.. For example,

def make_pizza(*toppings):
print(toppings)
make_pizza('mushrooms', 'green peppers', 'extra cheese')

o   Using **name as a formal parameter, this is just like a dict.

·        Import functions(★★★★★)

o   Import whole function

For example,

import pizza #There is a function file called pizza.py
pizza.make_pizza(16, 'pepperoni') # We need to point out the function from which file

o   Import specific function

from module_name import function_name

If we use this, we do not need to point out the function from which file.

·         Using as to specifying aliases for functions(用as为函数指定别名)/using as to specifying aliases for module(用as为模块指定别名)/import all functions from module.

   from pizza import make_pizza as mp
   import pizza as p
   from pizza import *

本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: Python python学习
最后更新:2017年10月17日

davidcheung

这个人很懒,什么都没留下

打赏 点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

搜索
欢迎关注我的个人公众号
最新 热点 随机
最新 热点 随机
DEEPFILTERNET:一种基于深度滤波的全频带音频低复杂度语音增强框架 奥地利匈牙利九日游旅程 论文阅读之Study of the General Kalman Filter for Echo Cancellation 小奥看房之鸿荣源珈誉府 杭州往返旅途及西溪喜来登和万怡的体验报告 2022年的第一篇碎碎念
奥地利匈牙利九日游旅程论文阅读之Study of the General Kalman Filter for Echo CancellationDEEPFILTERNET:一种基于深度滤波的全频带音频低复杂度语音增强框架
Python chapter 5 learning notes WP 嘀:嘀咕的 WordPress 插件 沉重悼念舟曲特大山洪泥石流中不幸遇难的同胞 2010 S.V Beijing Travel 18:Home Ticket S.V Beijing Travel 9:Tiananmen Square 奥特曼历史介绍1:TV版
标签聚合
Java 鸟哥的linux私房菜 leetcode linux python学习 生活 学习 高中 算法 Python
最近评论
davidcheung 发布于 5 个月前(02月09日) The problem has been fixed. May I ask if you can s...
tk88 发布于 5 个月前(02月07日) Hmm is anyone else having problems with the pictur...
cuicui 发布于 9 个月前(10月20日) :wink:
niming 发布于 10 个月前(09月19日) 同级校友,能刷到太巧了
davidcheung 发布于 2 年前(08月16日) 我得找一下我之前整理的word文档看一下,如果找到了我就更新一下这篇文章。
Nolan 发布于 2 年前(07月25日) 您的笔记非常有帮助。贴图不显示了,可以更新一下吗?
davidcheung 发布于 3 年前(06月19日) 到没有看webrtc的代码。现在主要在看我们公司的代码了。。。只是偶尔看一看webrtc的东西。。。
aobai 发布于 3 年前(03月13日) gain_change_hangover_ 应该是每三个block 只能够调整一次,这样保证每帧...
匿名 发布于 5 年前(12月30日) 烫
小奥 发布于 5 年前(12月12日) webRTC里面的NS本身我记得就是在C++里面呀

COPYRIGHT © 2025 小奥的学习笔记. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

陕ICP备19003234号-1

鲁公网安备37120202000100号