Python基础4(函数和类)

本文深入探讨Python的基础知识,包括函数的使用,类和对象的概念,以及继承在面向对象编程中的应用。
摘要由CSDN通过智能技术生成

Python基础4(函数和类)

文章目录

函数

#函数  返回字典
def build_person(first_name,last_name):
    person={
   'first':first_name,'last':last_name}
    return person

musician=build_person('s','zq')
print(musician)

{
   'first': 's', 'last': 'zq'}

def build_person(first_name,last_name,age=''):
    person={
   'first':first_name,'last':last_name}
    if age:
        person['age']=age
    return person

musician=build_person('s','zq',age=23)
print(musician)

{
   'first': 's', 'last': 'zq', 'age': 23}


#用户输入姓名,输入q时退出
def get_name(first_name,last_name):
    full_name=first_name +  ' ' + last_name
    return full_name.title()

while True:
    print("\nplease tell your name:")
    print("enter 'q' at any time to quit")
    

    f_name=input("First name:")
    if f_name=='q':
        break
        
    l_name=input("Last name:")
    if l_name=='q':
        break
    
    name=get_name(f_name,l_name)
    print("\nHello, "+name+"!")


please tell your name:
enter 'q' at any time to quit
First name:s
Last name:zq

Hello, S Zq!

please tell your name:
enter 'q' at any time to quit
First name:q
#使用函数修改列表
def print_models(unprinted_designs,completed_models):
    """模拟打印每个设计,直到没有未打印的为止
       打印每个设计后,将其移到completed_models中
    """
    
    while unprinted_designs:
        current_design=unprinted_designs.pop()
        print("Printing model:"+ current_design)
        completed_models.append(current_design)
        
def show_completed_models(completed_models):
    print("\nThe following models have been printed :")
    for completed_model in completed_models:
        print(completed_model)
        
unprinted_designs=['iphone','
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值