找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手
"""函数 函数也称为方法:
登录功能---函数我可以在一个模块当中重复引用--我还可以在其他模块进行调用他的功能
python自带的叫做内建函数:print input str list count split 那这些函数都是python提供给我们能够
重复使用的(组织好)的方法

python第二种函数  叫做自定义函数  是由我们使用者程序员进行组织的方法--->也是可以进行重复组织的"""
"""线性脚本 没有进行组织封装的"""
"""在函数的使用   那么是通过函数名称进行调用函数里面的功能"""
#
# def login():
#     username = input("请输入")
#     if username == "admin":
#         print("ok")
#     else:
#         print("no")
# login()






"""函数的命名规则以及注意点:
1.函数是以def关键字进行开头,然后():结尾
2.函数的括号()用来接受函数需要的值
3.函数的第一行一般是这个函数的注释内容
4.函数体部的内部逻辑
5.调用这个函数
6.函数它遵循的是缩进的语法  需要一个tab键位"""

#
# def function():#函数头部信息
#     """此方法用来输出打印hello word"""
#     print("hello word")
# #直接通过函数名称调用
# #function()
# #通过主函数调用
# if __name__ == '__main__':
#   function() #调用组织好的函数
#   print("123")

"""函数当中的参数 形式参数:在定义函数时,设置函数变量名称
实际参数:实在我实际调用这个函数是给函数的值"""
#
# def function(name):   #形式参数
#     #形式参数可以在体部引用
#     print(name+"狗狗")
#
# function(name="玉桂")
# function(name="妍妍")

"""python 形式参数与实际参数的区别
形式参数:它只是意义上定义的一个参数名称,他是不会被分配内存地址
# 实际参数:实际存在的参数,它是占用内存地址的"""
#
#
# def function(name):
#     print(name,"hubei")
#
# function(name=[1,2,3])





# def function(name,where,action):
#     print(name,where,action)
# function(name="遇鬼狗狗",where="骑自行车",action="大家")

# def login(user,password):
#     username = input("请输入你的用户名:")
#     if username == user:
#         password = input("请输入你的密码:")
#         if password == password:
#             print("登录成功")
#         else:
#             print("登录失败")
#     else:
#         print("账号错误")
#
# login("user","123456")




"""默认参数"""
#
# def function(name="滚球兽",where="东京",action="进化"):
#     print(name+where+action)
# function()
"""函数的变量他是分为全局变量 以及局部变量
全局  作用于当前变量下的所有内容
局部  在某一区域内可以使用当前的变量"""

# number3 = 1000
#
# def func():
#     number = 100
#     number +=100
#     number += number3
#     print(number)
# func()


"""申明局部变量为全局变量"""
# def func():
#     global number
#     number = 100
#     number += 300
#     print(number)
#
# func()
#
#
# def func2():
#     num = 100
#     num +=number
#     print(num)
# func2()
#


# """return:返回的作用 return是用来结束这个函数的,"""
#
# def func():
#     print("hello word")
# print(func()) #没有使用return 打印这个函数名称 这个函数没有值的
#
# def func2():
#     return "hello word" #hello word这个结果返回给func2 func2有值
#
# print(func2())

#
# def number():
#
#     num1 =100
#     return num1
# print(number())
#
# def number1():
#     num2 = number()/2
#     return num2
#
# print(number1())

""":return函数的返回值是由return进行决定的,return后面写什么 他就返回什么
如果函数中没有return 那么函数的默认返回值为none
如果return后面没有内容  也为none
如果想要返回多个内容 那么需要用逗号进行隔开 返回的是一个元组的序列
return 用来结束函数的运行--->一旦碰到return语句立马返回结果->结束函数的运行,renturn后面的代码将不会
被执行"""


"""可变长元组:
name= adimin
在不确定实际参数的个数时,那么就可以进行使用可变长元组
*:可变长的参数
"""

# def function(*name):
#     print(name)
#     print(type(name))
# function("admin","yanyan")


"""在不确定我我传入字典的个数时,使用可变长字典**"""
#
# dict1 = {"湖北":"武汉"}
# dict2 = {"长沙":"妍妍"}
# # def function(**dict):
#     print(dict)
#     print(type(dict))
#
# # function(name="hb",address="wh",user="gougou")
# function(**dict1,**dict2)
# function(**{"湖北":"武汉"})



"""同时使用元组和字典 有顺序关系 元组前字典后
可变长字典 合并传输的字典  转入字典的时候 一定要确认是否有重复的键"""
#
# def function(*tuple,**dict):
#     print(tuple,dict)
# function("admin","wuhan",**{"湖北":"武汉"})

# def login():
#     username = input("输入银行账号")
#     if username == "admin":
#         password = input("请输入密码")
#         if password =="123456":
#             return ("登录成功")
#         else:
#             return ("登录失败")
#     else:
#         return ("账号错误")
#
# def money():
#     list=["登录成功","登录失败","账号错误"]
#     result = login()
#     if result == list[0]:
#         print("你的余额为100000000")
#     elif result ==list[1]:
#         print("登陆失败,密码错误")
#     else:
#         print("账号错误")
# money()



"""python内置函数
format 格式化输出方式{}:"""


# print("{}{}".format("name","age"))
# print("{1}{1}{1}".format("1","2","3"))#根据索引位置去参数值

#参数名称的传递
# print("{name}今年{age}".format(name="hao",age=18))

#list方式
# list1 =["小王",18]
# print("{0[0]} 今年 {0[1]}".format(list1))

#格式化输出字典  也需要加**

# dict = {"name":"滚球兽","where":"tokyo"}
# print("{name}{where}".format(**dict))

"""zip函数 用于打包合并"""
# list1 = ["name","where"]
# list2 = ["滚球兽","totyo","18"]
# list3 = list(zip(list1,list2))
#
# print(list3)

"""list3 是一个多维数组 可以转换为字典,两个list长度取最短的值相同的进行打包合并"""

"""open函数 用来打开文件   通过文件地址读取"""

# file = open(r"C:/Users/szyc/PycharmProjects/wuhan10/demo/o",encoding="utf-8",mode="a")
# # print(file.read())
#
#
# print(file.readlines())

"不要重复使用readlines"




# file = open(r"C:/Users/szyc/PycharmProjects/wuhan10/demo/o",encoding="utf-8",mode="w")
# file.write("123456") #会覆盖原文件
# file.writelines("妍妍baobao")

#
# file = open(r"C:/Users/szyc/PycharmProjects/wuhan10/demo/o",encoding="utf-8",mode="a")
# file.writelines("12333333333333333333")
"""想要追加内容 用mode a 模式 """
# file.close() #写完手动关闭文件
"""with open 自动关闭 as命名"""
# with open(r"C:/Users/szyc/PycharmProjects/wuhan10/demo/o",encoding="utf-8",mode="a")as f:
#     f.writelines("nigouzidaochu dabaoge")


# def read_write(file_name,encoding,mode,str):
# #     a = open(file_name,encoding=encoding,mode=mode)
# #     a.write(str)
# #     a.close()
# # read_write(r"C:/Users/szyc/PycharmProjects/wuhan10/demo/o","utf-8","a","12")


"""已存在一个文件 这个文件有01 work 02 file 03 work 04 file 实现函数封装 统计这个里面值出现的次数
以及file出现多少次"""

""""""

# str = ""
# with open(r"C:/Users/szyc/PycharmProjects/wuhan10/demo/o", encoding="utf-8",
#               mode="r")as f:
#       for i in f.readlines():
#           i = i.strip()
#           str += i
# print(str.count("work"))
# print(str.count("file"))

分享至 : QQ空间
收藏

0 个回复

您需要登录后才可以回帖 登录 | 立即注册