python基础任务task4-2019.5.16

定义函数

关键字

import keyword
print(keyword。kwlist)

函数的定义

def greet_user():
    """显示简单的问候语"""
    print('Hello!')
greet_user()

(打的时候少了个冒号也是可以了)

def告诉python我要定义一个函数了,后面是函数名加括号,必须以冒号结尾,下面一行是缩进的,这些缩进就是它的内容啦。那个注释是指函数的作用。第四行吧,就是调用,直接用就行了。
最后结果是打印出了hello!
接下来难点,就是向函数传递信息。

def greet_user(username):
    """显示简单的问候语"""
    print('Hello!'+username.title()+'!')
greet_user('jesse')

这个就是定义的时候里面加上东西,调用的时候括号里是人名,结果是
Hello!Jesse!

函数的参数

在上面的例子中,username是一个形参,而调用的时候,jesse是个实参,在实际处理中呢,是将jesse这个实参传给了形参。(很多人分不清,所以有人说错了也没关系,但是我现在看来这个还是挺简单的)

传递实参

位置实参

def describe_pet(animal_type,pet_name):
    """显示宠物信息"""
    print('\nI have a '+animal_type+'.')
    print('My '+animal_type+"'s name is "+pet_name.title()+".")
describe_pet('hamster','harry')

调用的时候,需要按照顺序给出个动物类别和动物的名字。结果

I have a hamster.
My hamster's name is Harry.

需要调用多次就直接调用就行。
关键字实参
就是在实参中指定了形参极其对应的值。

def describe_pet(animal_type,pet_name):
    """显示宠物信息"""
    print('\nI have a '+animal_type+'.')
    print('My '+animal_type+"'s name is "+pet_name.title()+".")
describe_pet(animal_type='hamster',pet_name='harry')

默认值
就是在形参那里给指定了一个值,就不用每次都设置实参了。

def describe_pet(pet_name,animal_type='dog'):
    """显示宠物信息"""
    print('\nI have a '+animal_type+'.')
    print('My '+animal_type+"'s name is "+pet_name.title()+".")
describe_pet(pet_name='harry')

注意这个形参的排列顺序必须修改,而调用的时候,直接就可以直接写上name就行了。
调用的话,就都可以随意调用。

函数的返回值

让实参变成可选的
举个例子,并不是所有人都有中间名,现在就是让中间名变成可选的。

def get_formatted_name(first_name,last_name,middle_name=''):
    """返回整洁的姓名"""
    if middle_name:
        full_name=first_name+' '+middle_name+' '+last_name
    else:
        full_name=first_name +' '+last_name
    return full_name.title()
musician=get_formatted_name('jimi','hendrix')
print(musician)
musician=get_formatted_name('john','hooker','lee')
print(musician)

这个代码忘记敲+,报错来着。结果

Jimi Hendrix
John Lee Hooker

返回字典
函数可以返回任何值,包括列表和字典等复杂的数据结构。

def build_person(first_name,last_name):
    """返回一个字典,其中包含有关一个人的信息"""
    person={'first':first_name,'last':last_name}
    return person
musician=build_person('jimi','hendrix')
print(musician)

返回:

{'first': 'jimi', 'last': 'hendrix'}

File

打开文件方式

with open('scratch_1.txt') as file_object:
    contents=file_object.read()
    print(contents)

这个没运行出来,不知道哪里出错,不过这个讲解还是要有的,关键字with在不需要访问文件后将其关闭,函数open()将文件打开,不管干嘛都得打开,(知道错误了,这个文件必须和py文件同一目录才可以运行,真是坑人啊
当然上述问题是有办法解决的,就是文件路径
windows中是这么着的:

file_path='C:/users/ehmatthes/other_files/text_files/filename.txt'
with open(file_path) as file_object:

逐行读取

file_path='/home/ehmatthes/other_files/text_files/filename.txt'
with open(file_path) as file_object:
    for line in file_path:
    print(line)

filename = 'scratch_1.txt'
with open(filename, 'w') as file_object:
    file_object.write('I love programming.')

注意的就是open里面加了个w,下面不是print了,是.write。而且这个w的话,就会清空以前的内容。
python只能将字符串写到文本文件中,要是储存数值数据,就必须使用str()将其转换为字符串格式。
a是附加模式,就是说不会清除里面的内容。

OS

os模块提供了多数操作系统的功能接口函数.当os模块被导入后,它会自适应于不同的操作系统平台。

Datetime模块

datatime模块重新封装了time模块,提供更多接口,提供的类有:date,time,datetime,timedelta,tzinfo。
https://www.jianshu.com/p/9408261049a6参考。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值