云学编程的第14天—【微软官方python入门教程 P29-P30笔记】2021-11-14 函数Function

本文介绍了如何通过创建函数来提高代码的可读性和维护性,包括减少重复代码、传递参数以显示不同消息以及返回函数值来实现更灵活的功能。通过示例展示了如何使用datetime库,并展示了如何根据任务完成情况定制打印消息。此外,还探讨了从用户输入获取首字母并返回的方法,强调了函数在代码优化中的重要性。
摘要由CSDN通过智能技术生成

P29 函数


​​​​​​​

 

Sometime we copy and paste our code 

Little trick i using to try and debug my code, figure our what was taking a lot of time.

first_name = 'Susan'
print('task completed')
print(datetime.datetime.now())
print()

for x in range(0,10):
    print(x)
print('task completed')
print(datetime.datetime.now())
print()

Use functions instead of repeating code

import datetime 
#Print the current time
def print_time():
    print('task completed')
    print(datetime.datetime.now())
    print()
first_name = 'Susan'
print_time()

for x in range (0,10):
    print(x)
print_time()

In the datetime library call the datetime object call the now function of that datetime object.  -----It's pretty clunky

By moving the code to a function, you reduce rework and the chance of introducing bugs when you change the code you hand copied.

#Import the datetime class from datetime library
from datetime import datetime 
#Print the current time
def print_time():
    print('task completed')
    #Now I don't need the extra datetime prefix 
    print(datetime.now())
print_time()

What if I want a different message displayed?

from datetime import datetime 
#print timestamps to see how long sections of code
#take to run

first_name = 'Susan'
print('first name assigned')
print(datetime.now())
print()

for x in range(0,10):
    print(x)
print('loop completed')
print(datetime.now())
print()

Pass the task name as a parameter

from datetime import datetime 
# Print the current time and task name 
def print_time(task_name):#make up you own parameter names
    print(task_name)
    print(datetime.now())
first_name = 'Susan'
print_time('first name assigned')
for x in range(0,10):
    print(x)
print_time('loop completed')

parameters allow you a lot more flexibility when you're creating your functions.

Here's another example where the code looks different but we are doing the same logic over and over

first_name = input('Enter your first name: ')
fitst_name_initial = first_name[0:1]
last_name = input('Enter your last name: ')
last_name_initial = last_name[0:1]

print('Your initials are: ' + fitst_name_initial + last_name_initial)

I can still use a function, but this time my function returens a value

def get_initial(name):
    initial = name[0:1]
    return initial
first_name = input('Enter your first name: ')
first_name_initial = get_initial(first_name)

last_name = input('Enter your last name: ')
last_name_initial = get_initial(last_name)

print('Your initials are: ' + first_name_initial + last_name_initial)

If you need to change something you only have to change it in one place

def get_initial(name):
    initial = name[0:1].upper() #change
    return initial
first_name = input('Enter your first name: ')
first_name_initial = get_initial(first_name)

last_name = input('Enter your last name: ')
last_name_initial = get_initial(last_name)

print('Your initials are: ' + first_name_initial + last_name_initial)

Functions that return values allow clever code, but you might trade readability for less code

def get_initial(name):
    initial = name[0:1].upper()
    return initial
first_name = input('Enter your first name: ')
last_name = input('Enter your last name: ')

print('Your initials are: '+get_initial(first_name)+get_initial(last_name))

Because they return values you can call them in different ways, You can either call them and take the value that you receive and return it into a variable or you can actually call it directly inside statements.

Functions make you code more readable and easier to maintain

Always add comments to explain the purpose of your functions

Functions must be declared before the line of code where the function is called

P30 实操函数

import datetime
first_name = 'Susan'
print('task completed')
print(datetime.datetime.now())
print()
for x in range(0,10):
    print(x)
print('task completed')
print(datetime.datetime.now())
print()

(又学到12点多了呜呜呜QAQ,没到天亮还算14号吧!)

如何把datetime.datetime.now()简化 
把import datetime改为 form datetime import datetime 后面就只用写datetime.now了

·Now waht if I decide to display custom messages如果想显示自定义信息怎么办,So a different message depending on what task just completed执行不同的任务有不同的信息。

给定义的函数加参数 def print_time(task_name) 并在后面调用的时候设置对应的值print_time('printed first name')  print_time('completed for loop')

import datetime
def print_time(task_name):
    print(task_name)
    print(datetime.datetime.now())
    print()
    
first_name = input('your name: ')
print(first_name)
print_time('名字')

for x in range(0,10):
    print(x)
print_time('数字')

效果如下: 

复杂一点的:

#This function will take a name and return the first letter of the name
#first letter of the name
def get_initial(name):
    initial = name[0:1].upper()
    return initial
#Ask for someone's name and return the initials 
first_name = input('Enter your first name: ')
middle_name = input('Enter your middle name: ')
last_name = input('Enter your last name: ')

print ('Your initials are: ' + get_initial(first_name) + get_initial(middle_name) + get_initial(last_name))

今天搞太晚了,大家不要学我熬夜

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值