多语言展示
当前在线:780今日阅读:176今日分享:34

python函数入门基础

本文,跟着教师一起学习一下,python的基本用法。
工具/原料
1

电脑

2

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()

注意事项

培训直播就看了一半,所以,就写了一半出来。

推荐信息