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 (' The PYTHONPATH is ' ,sys.path, ' ')

7.name的用法


if __name__ == ' __main__':
print ('This program is being run by itself ')
else :
print ('I am being imported from another module')

原文地址:https://www.cnblogs.com/dadaozhijian22/p/7856784.html