找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手

5、python 中的返回值return
'''
1)在程序开发中,有时候会希望一个函数执行程序结束后,告诉 调用者一个结果,以便调用者针对具体的结果做后续的处理。
2)返回值是函数完成工作后,最后给到调用者的一个结果。
3)在函数中使用return关键字可以返回结果。
4)调用函数的一方可以使用变量来接收函数的返回结果。
'''
例子1:
def test():
num = 600
score = num/6

test()             #空
print(test())     #None
例子2:
def test():
num = 600
score = num/6
return score    #把score的结果给到函数调用处,谁拿到了这个函数谁就可以拿到这个score
#return只是把结果返回回去,return后面的代码不会执行
test()            #空
print(test())     #100.0
例子3:
def test():
num = 600
score = num/6
print(score)
return score
test()           ##100.0
print(test())    #100.0
需求:编写一个求平均分的函数,然后根据平均分判断是否优秀学生
def test():
num = 600
score = num/6
return score

def goods_stu():
value = test()    #函数的传递
if value >90:
print('优秀学生')
else:
print('不是优秀学生')

if name == 'main':
goods_stu()
需求:登录银行系统并显示余额,有两个功能第一个是登录,第二个是登录 后显示余额,
先登录然后根据登录是否成功然后是否显示余额。 分析思路:如果想查询到余额,前提必须登录,
所以现在我们用两个函数来 处理,第一个函数实现登录,第二个函数实现余额查询,
调用第一个函数得 到的结果给第二个函数,然后第二个函数根据结果进行代码处理。
登陆函数
def login():
user = input('请输入您的用户名:')
if user=='admin':
pwd = input('请输入您的密码:')
if pwd =='123456':
return '登录成功'
else:
return '密码错误'
else:
return '用户名错误'

# 查询余额
def select_amount():
m = login()       #函数的传递
if m=='登录成功':
print('您的余额是$10000000')
else:
print('请重新登录')
if name == 'main':
select_amount()
6、format()函数
format()函数是一种格式化字符串的函数 ,该函数增强了字符串格式化的功能。
基本语法:是通过 {} 来代替以前的 %
1)按顺序进行格式化
str1 = '{} {}'.format('hello','dcs',88)
print(str1)         #hello dcs
2)通过索引进行格式化输出
str1 = '{1} {0} {0}'.format('hello','dcs',88)    #大括号中的数字表示索引
print(str1)          #dcs hello hello
3)通过参数名进行格式化输出
str1 = '姓名:{name} 年龄:{age}'.format(name='xiaoduan',age='18')
print(str1)          #姓名:xiaoduan 年龄:18
4)对列表进行格式化
list1 = ['xiaoduan','18']
str1 = '姓名:{0[0]} 年龄:{0[1]}'.format(list1)  #{0[0]}第一个0是取format中第一个值
#第二个0是list1中的第一个值,
print(str1)      #姓名:xiaoduan 年龄:18
list1 = ['xiaoduan','18']
list2=[16]
str1 = '姓名:{0[0]} 年龄:{1[0]}'.format(list1,list2)
#{1[0]} 这个1表示取list2,这个0是list2中第一个值
print(str1)         #姓名:xiaoduan 年龄:16
5)对字典进行格式化
dict1 = {'name':'xiaoduan','age':18}
str1 = '姓名:{name} 年龄:{age}'.format(**dict1)    #zi'dian这里需要加*号
print(str1)       #姓名:xiaoduan 年龄:18
7、zip()函数
zip()函数用于将可迭代的对象作为参数,将对象中对应的元素打包成 一个个元组,然后返回由这些元组组成的列表,
如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同。
例子1
list1 = ['name','age','url']
list2 = ['xiaoduan',18,'www.baidu.com']
list3 = list(zip(list1,list2))
print(list3)         #[('name', 'xiaoduan'), ('age', 18), ('url', 'www.baidu.com')]
print(type(list3))   #
例子2
list1 = ['name','age','url']
list2 = ['xiaoduan',18]
list3 = list(zip(list1,list2))
print(list3)           #[('name', 'xiaoduan'), ('age', 18)]   这里url不会打印出来,打印出与最短的list2相同
例子3
list1          #{'name': 'xiaoduan', 'age': 18, 'url': 'www.baidu.com'}= ['name','age','url']
# list2 = ['xiaoduan',18,'www.baidu.com']
# list3 = dict(zip(list1,list2))
# print(list3)
8、open() 函数
open() 函数用于打开一个文件,创建一个 file 对象 语法pen(file, mode),模式有r(只读),w(写入覆盖),a(写入追加)
读 的模式  r
o = open(r'C:\my_project\dcs\dcs9\test','r')     #创建一个对象o
all= o.read()              #显示文件中所有的内容,数据类型是字符串
print(all)
print(type(all))            #
all = o.readline()          #显示文件中第一行的内容  数据类型是字符串
print(all)                  #dcs
all = o.readlines()           #显示所有的内容,并且返回一个列表
print(all)                    #['dcs\n', 'china\n', '888\n', '666']
print(type(all))              #
写的模式  w
o = open(r'C:\my_project\dcs\dcs9\test','w')
o.write('duoceshi')        #write 对原文件的内容进行覆盖,不能写整型
o.writelines('123')         #对原文件的内容进行覆盖,不能写整型
o.close()                #关闭文件
追加模式 a
o = open(r'C:\my_project\dcs\dcs9\test','a')
o.write('dcs')          #a模式对原文件进行追加,不会覆盖
o.writelines('hello\n')  #a模式对原文件进行追加,不会覆盖
o.close()
9、with open函数
with open(r'C:\my_project\dcs\dcs9\test','r') as f:     #读   f类似取个别名 即对象
all = f.read()
all1 = f.readline()
all2 = f.readlines()
print(all)print(all1)print(all2)         #['123dcshello\n', 'hello\n', '\n']
with open(r'C:\my_project\dcs\dcs9\test','w') as f:    #写
# f.writelines('acb')
f.write('efg')
f.close()
with open(r'C:\my_project\dcs\dcs9\test','a') as f:    #追加
f.writelines('acb')
f.write('efg')
f.close()

分享至 : QQ空间
收藏
您需要登录后才可以回帖 登录 | 立即注册