标签:style blog http color io os ar for strong
def fun_name(): --不带参数的函数 print xxx def fun_name(name,age): --带参数的函数 print ’hi ,%s ,you are %s years old‘%(name,age)
>>> def hi(): ... print ‘hello!‘ ... >>> hi() hello! >>> >>> def hi2(name): ... print ‘hello , %s‘%name ... >>> hi2(‘lk‘) hello , lk
>>> def hi3(name=‘lk‘): ... print ‘hi,%s‘%name ... >>> hi3() hi,lk >>> hi3(‘tom‘) hi,tom
[root@likun python_scripts]# cat 11fun.py
#!/usr/bin/python def emp(a,b,c=10,d=20): return‘%s,%s,%s,%s‘%(d,c,b,a) print emp(1,2) print emp(b=2,a=1) print emp(1,2,3) print emp(1,2,d=30) [root@likun python_scripts]# python 11fun.py 20,10,2,1 20,10,2,1 20,3,2,1 30,10,2,1
[root@likun python_scripts]# cat 11fun.py #!/usr/bin/python def emp(): list=[] list.append(‘lk‘) list.append(‘tom‘) print list returnlist #函数返回值 print emp() #用来将函数赋值到变量 print ‘local var is not enable:!‘,list
[root@likun python_scripts]# python 11fun.py [‘lk‘,‘tom‘] [‘lk‘,‘tom‘] local var is not enable:!<type ‘list‘>
[root@likun python_scripts]# cat 11fun.py #!/usr/bin/python def emp(): global list list=[] list.append(‘lk‘) list.append(‘tom‘) print ‘local var:‘,list returnlist print emp() list.append(‘jack‘) print ‘global var is not enable:!‘,list
[root@likun python_scripts]# python 11fun.py local var:[‘lk‘,‘tom‘] [‘lk‘,‘tom‘] global var is enable:![‘lk‘,‘tom‘,‘jack‘]
[root@likun python_scripts]# cat 11fun.py #!/usr/bin/python import os,sys os.chdir(‘/etc/init.d‘) service_list=os.listdir(‘/etc/init.d‘) def run(servicename,action=‘status‘): #if action==‘‘: action=‘status‘ cmd =‘service %s %s‘%(servicename,action) print cmd os.system(cmd) i=0 for service in service_list: i=i+1 print service, if i%4!=0 : print (‘ ‘*(15-len(service))), #输出4列并对齐 else: print ‘‘ whileTrue: option = raw_input(‘\nPlease input your option:‘) if len(option.split())==2: #判断输入的参数个数 servicename=option.split()[0] action=option.split()[1] run(servicename,action) elif len(option.split())==1: run(option) else: print ‘wrong input!‘ continue
[root@likun python_scripts]# python 11fun.py rpcidmapd sssd lvm2-monitor halt postfix cgconfig restorecond rpcgssd kdump avahi-daemon rpcsvcgssd rpcbind ip6tables mcelogd rdisc rsyslog rhnsd ntpd mysql netconsole nslcd sandbox vsftpd single autofs zabbix_server sshd mdmonitor netfs haldaemon network messagebus oddjobd postgresql httpd crond sysstat nagios nfslock psacct ypbind iptables functions killall cgred saslauthd atd snmptrapd snmpd zabbix_agentd certmonger auditd cpuspeed microcode_ctl smartd acpid nscd nfs udev-post irqbalance abrtd ntpdate Please input your option:mysql restart service mysql restart Shutting down MySQL. [ OK ] StartingMySQL.. [ OK ] Please input your option:
标签:style blog http color io os ar for strong
原文地址:http://www.cnblogs.com/kissdb/p/4009592.html