关于python中的函数的def
def 的定义参数:形参和实际参数
例如下面中的songer和songname为形参而“alan warker,play crime,tobu为实际参数”
关于参数顺序如果没有特殊说明则按照顺序来
def love_song_message(songer,songname): "定义喜欢歌手的姓名和曲目" print("\ni love called ".title() + songer.title() + "!") print(songer.title() + " song have called " + songname.title()) love_song_message("alan warker","play") love_song_message("tobu","crime")」‘‘‘
#这俩个代码表示相同def love_song_message(songer,songname): "定义喜欢歌手的姓名和曲目" print("\ni love called ".title() + songer.title() + "!") print(songer.title() + " song have called " + songname.title()) love_song_message(songer="alan warker",songname="play") love_song_message(songname="crime",songer="tobu")#如果没有特别说明则系统默认顺序为一一对应。否则按照实际参数与形式参数对应
当然如果这里含默认值则如下
#要注意位置。要表示相同 def love_song_message(songname,songer="alan worker"): """定义喜欢歌手的姓名和曲目""" print("\ni love called ".title() + songer.title() + "!") print(songer.title() + " song have called " + songname.title()) love_song_message(songname="play") love_song_message(songname="torb") 运行:
I Love Called Alan Worker!
Alan Worker song have called Play
I Love Called Tobu!
Tobu song have called Cirme
def love_song_message(songname,songer="alan worker"): """定义喜欢歌手的姓名和曲目""" print("\ni love called ".title() + songer.title() + "!") print(songer.title() + " song have called " + songname.title()) love_song_message("play")#love_song_message(songname = "play")这是等同的一个利用位置关系,另一个使用参数调用实际参数赋值给形式参数 love_song_message(songname="cirme",songer= "tobu")#love_song_message(songer="tobu",songname="crime")这两个等同 运行:
I Love Called Alan Worker!
Alan Worker song have called Play
I Love Called Tobu!
Tobu song have called Cirme
这两个运行值相同,可以更改默认值。关于这个有很多
下面这是关于字典
alien_0 = {"color":"green","points":"5"} print(alien_0["color"]) print()#为了空行 print(alien_0["points"]) new_points = alien_0["points"] print ("\n you".title() + " " + ‘just earned ‘ + new_points +" "+ "points !")
alien_0 = {"x_position": 5, "y_position": 50 ,"speed": "fast" } print("\noriginal ".title() +" "+ "x_position:" + str(alien_0["x_position"])) #定义开始的外星人的位置和速度 #打印开始的方位 if alien_0["speed"] == "slow": x_increment = 1 elif alien_0["speed"] == "medium": x_increment = 2 else : x_increment = 3 alien_0["x_position"] = alien_0["x_position"] + x_increment print("\nnew".title()+" "+ "x_position:"+str(alien_0["x_position"]))
字典无非将列表中的元素表示出来这里我要说的字典使用用{} 而将列表使用[]
skill = [‘program‘, ‘ cook‘, ‘know very knowledge‘] len(skill)#规定元素个数 print("\n\t I skill") print(skill)
programe_languages = [‘python‘,‘c++‘,‘java‘,‘javascipt‘,‘ago‘,‘php‘,‘ruby‘] print("\t\n I learn programe language:" ) for programe_language in programe_languages:#for中program_language是遍例program_languages中的元素
print(programe_language.title())
我们约定一般表示元素的为复数
大概如此
原文地址:https://www.cnblogs.com/luzhi0324/p/11623063.html
时间: 2024-11-06 03:41:41