这是在学习Python的时候做的笔记,有些时间了,大概是按照一本挺实用的入门书籍学的,我学习编程的思路一般是掌握基础的变量类型,语法-分支结构 函数调用 类创建 结构体定义,记录一些简单的实例,剩下的就是需要用什么百度现学。
对我来说python的优势是,没有类型要求,不用声明,没有指针,万能数组,库很强大。
我这个顺序有点差,应该先说函数定义的,这是最基础的,关于如何定义函数,参数和返回值
代码
# 简单的定义函数
# region
def greet():
print('hello')
# 参数
def greet_parameter(name):
print('welcome '+name+'!!!')
# 带有默认值的
def describe(name, type = 'dog'):
print('you have a ' + type + ' name ' + name)
describe('123', 'cat')
describe('lilei') # 采用默认参数
# 用于返回值
def getfullname(firstname, secondname):
temp = firstname + ' ' + secondname
return temp.title()
print(getfullname('song', 'yu'))
# 可选参数 带有默认 放在参数最后
def getnamechoose(first_name, last_name, caps_look = 0):
full_name = first_name + ' ' + last_name
if caps_look == 0:
return full_name.lower()
else:
return full_name.title()
# 传递列表参数
def printname(namelist):
for name in namelist:
print('welcome ' + name + '!!')
usernames = ['hannah', 'ty', 'margot']
printname(usernames)
# 禁止修改的列表
# 调用时输入列表usernames可以在函数中修改 输入usernames[:]只能修改副本
# 传入任意数量的参数
# 讲所有参数封装到元组中
def make_pizza(*toppings):
print(toppings)
s = ' '
for topping in toppings:
s = s + topping + '_'
print(s)
make_pizza('pepperoni')
make_pizza('mushrooms', 'green peppers', 'extra cheese')
# 混合位置与任意关键字实参
def meg(name, password, **userinfo):
info = {}
info['username'] = name
info['userpassword'] = password
for key, value in userinfo.items():
info[key] = value
return info
test = meg('songyu', '123', agent = 'moliz', age = '22')
print(test)
# endregion