Python入门练习笔记【2022-3-12】

练习

myfunc.py
#-*- coding:utf-8 -*-
#It will return a tuple type date,because the function need to return two objects.                                             #defining a function and seting a default value which must be in the end of the function paramaters.
def hello(s,d1='default value one',d2='default value two'):
    return 'a',1,s,d1,d2                                                                                                       
#defining a function that includes a variable parameter as tuple or list to use.
def mysum(*nums):
    s=0
    for temp in nums:
        s+=temp

    return s
#defining a function that includes a variable paramater so called "the key Parameter" as dict to use.
def printInform(name,age,**key):
    print(name,age,key,sep='\n')
def printInformAndCheckKeyParameters(name,age,**key):
    if 'IQ' in key:
        print("I have IQ")
    print(name,age,key,sep='\n')
#Habit and IQ are key paramter and are already restricted its key name by the char of '*'. If restricted key parameter already
had a default value, you can overlook it in performing function.
def printInformAndRestrictKeyParameters(name,age,*,Habit='sport&coding',IQ):
    print(name,age,Habit,IQ,sep='\n')
#If the parameter series already had a variable parameter before restricted key parameters, you can overlook the char of '*'.
def printInformAndRestrictKeyParametersInAnotherWay(name,age,*nums,Habit,IQ):                                                      print(name,age,sum(nums),Habit,IQ,sep='\n')
#The definition order:basic parameters > default parameters by user definition > variable parameters > key parameters > restric
ted key parameter that are already named.
2_lesson.py
#!/usr/bin/env python3
#-*- coding:utf-8 -*-

# It will extend the function of hello from the file of 2_lesson_func.py
from myfunc import hello
from myfunc import mysum
from myfunc import printInform
from myfunc import printInformAndCheckKeyParameters                                                                            from myfunc import printInformAndRestrictKeyParameters
from myfunc import printInformAndRestrictKeyParametersInAnotherWay                                                             #from myfunc import *                                                                                                          
#performing this function and observing its outcome.
print(hello('Jason'))
print(hello('Jason Liu',d2='I changed the default value two.'))                                                                #performing function
ls=[1,2,3,4,5]
t=(1,2,3,4)                                                                                                                    print(mysum(ls[0],ls[1],ls[2],ls[3],10))
print(mysum(*ls))
print(mysum(*t))

mp={'Habit':'sport&coding','IQ':99999}
print(printInform("Jason",22,Habit='sport&coding',IQ=99999))
print(printInform("Jason",22,**mp))
print(printInformAndCheckKeyParameters("Jason",22,**mp))
#---dict: dictionary is a key-value type date like Java's Map List. Its key is implemented by Hash algorithm and its key must b
e a constant type.
dic={'one':'Jsaon','two':123,"three":True}
print(f"{dic['one']}")
print("{0}".format(dic['two']))
#recover value
dic['one']="Jason Liu"
print("%s" % dic["one"])
print(dic.get('one'))
#Getting element and return a default value when the objec is not found.
print(dic.get('five','Not found'))                                                                                             
                                                                                                                               #---set: a list of that each element is unique and it will filter repetitive elements.                                         #It need a list type data.
s=set([1,1,2,3,'hello'])
print(s)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员杰森

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值