python函数入门基础
1、type(x)
返回x的变量类型
2、int()、float()和str()是python的内建函数。
3、自定义函数和函数调用:
def print_lyrics():
print('你好')
print('你吃饱了吗')
print_lyrics()
先定义,再调用,顺序不可颠倒。
4、形式参数:
#!/usr/bin/python
# -*-coding.utf-8 -*-
def print_twice(text):#形参
print(text)
print(text)
print_twice('学习天天,好好向上')
里面的text是形式参数,在调用的时候,可以用别的参数代替,如本例的'学习天天,好好向上'。
5、全局变量和局部变量:
# -*- coding: utf-8 -*- #先写一下编码格式
#定义俩全局变量
ahead = '欢迎你'
showList = []
#做三个自定义函数:
def printAhead():
print (ahead)
def printOther():
city = '北京' #这里,city是局部变量
print (city + ahead)
#此时运行不会有任何返回。想要得到结果,需要调用函数:
printAhead()
printOther()
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
阅读量:44
阅读量:35
阅读量:67
阅读量:63
阅读量:96