python学习
Posted Carzy_Learner
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python学习相关的知识,希望对你有一定的参考价值。
1.全局变量写法
def func():
global x
print (‘x is‘,x)
x=2
print (‘Changed local x to‘, x)
x= 50
func()
print (‘x is still‘ ,x )
2.改变形参和不改变形参设默认值用法
def say(message,times =3 ):
print (message * times )
say (‘Hello‘)
say (‘World‘,5)
3给关键参数赋值即可
def func(a,b=5,c =10 ):
print (‘a is‘ , a , ‘and b is ‘ , b , ‘and c is ‘, c )
func (3,7)
func (25,c =24)
func (c = 50 , a= 100)
4.return 用法跳出函数返回值
def maximum (x,y):
if x>y :
return x
print (x,‘>‘,y,‘is true ‘)
else:
return y
print (maximum (3,2))
5.docstring 用法
def printMax(x,y):
‘‘‘Prints the maximum of two numbers.
The two values must be integers. ‘‘‘
x= int (x)
y= int (y)
if x>y:
print (x,‘is maximum‘)
else :
print (y,‘is maximum‘)
printMax(3,5)
print (printMax.__doc__)
6. sys模块使用
import sys
print(‘ The command line arguments are:‘)
for i in sys .argv:
print (i)
print (‘\n\n The PYTHONPATH is ‘ ,sys.path, ‘\n‘)
7.name的用法
if __name__ == ‘ __main__‘:
print (‘This program is being run by itself ‘)
else :
print (‘I am being imported from another module‘)
以上是关于python学习的主要内容,如果未能解决你的问题,请参考以下文章