学习python的目的是:找工作需要除了Linux C/C++之外的一门语言,希望用python来写机器学习算法
开始写:
1,强制类型转换?guess=int(raw_input('enter a integer'))
2,if和else后面竟然要加冒号:,还有for也要加冒号(缩进要注意,不对应要出错)
6,关键参数赋值,也就是赋值的时候用变量名
每个模块都有一个__name__,当一个模块第一次被输入的时候,这个模块的主块将被运行,而在它被别的模块输入的时候,不运行主块。还有模块之间的调用使用(例如)import module01
在module01中:
在module3中:
12,除了基本数据类型之外,python中的“高级”数据结构:
13,编写一个python脚本
由于现在每天早上一到实验室,要敲一些命令,连上服务器,挂载磁盘,索性用python写一个做这些事情的一个脚本,然后用这个脚本每天开机,这样的话还能节约点时间,顺便学学python的脚本写法:
开始写:
1,强制类型转换?guess=int(raw_input('enter a integer'))
2,if和else后面竟然要加冒号:,还有for也要加冒号(缩进要注意,不对应要出错)
- while running:
-
guess=int(raw_input('enter a integer')) -
if guess == 2: -
print 'ok' -
elif guess == 4: -
continue -
else: -
break -
for i in range(1, guess): -
print 'ok' -
else: -
print 'the loop is over' - print 'done'
- def sayhello():
-
print 'hello def' - sayhello()
- def printmax(x,y):
-
if x > y: -
print 'x>y' -
else: -
print 'x<y' - printmax(3, 4)
- x = 9
- y = 8
- printmax(x, y)
- def say(message, times=1):
-
print message * times -
- say('hello')
- print '-----------------'
- say('hello',5)
6,关键参数赋值,也就是赋值的时候用变量名
- def printmin(x, y=1):
-
if x > y: -
print 'x>y' -
else: -
print 'x<y' - printmin(x=2, y=3)
- printmin(x=6, y=3)
- printmin(x=3)
- def printMax(x, y):
-
'''Prints the maximum of two numbers. -
The two values must be integers.''' -
x = int(x) # convert to integers, if possible -
y = int(y) -
if x > y: -
print x, 'is maximum' -
else: -
print y, 'is maximum' - printMax(3, 5)
- print printMax.__doc__
- help(printMax)
- import sys
- print 'the command line arguments are'
- for i in sys.argv:
-
print i - print '\n\n the path is',sys.path,'\n'
每个模块都有一个__name__,当一个模块第一次被输入的时候,这个模块的主块将被运行,而在它被别的模块输入的时候,不运行主块。还有模块之间的调用使用(例如)import module01
在module01中:
- if __name__=='__main__':
-
print 'this program is being run by itself' - else:
-
print 'I am being imported from another module'
- import module01
- print __name__
- print module01.__name__
在module3中:
- def sayhello():
-
print 'hello python' -
- version1 = '0.1'
- version2 = '0.2'
- version3 = 0.3
- version4 = 3
- def printType():
-
print version1.__class__ -
print version2.__class__ -
print version3.__class__ -
print version4.__class__
- from module03 import sayhello, version1, version2, version3, version4, printType
- print version1
- print version2
- print version3
- print version4
- printType()
12,除了基本数据类型之外,python中的“高级”数据结构:
13,编写一个python脚本
由于现在每天早上一到实验室,要敲一些命令,连上服务器,挂载磁盘,索性用python写一个做这些事情的一个脚本,然后用这个脚本每天开机,这样的话还能节约点时间,顺便学学python的脚本写法:
- #!/usr/bin/python
- #!/usr/bin
- # Filename: morning.py
- import os
- import time
- pon_command = "sudo pon myvpn"
- route_command = "sudo route add -net 0.0.0.0 dev ppp0"
- vpn_command = 'sudo pon myvpn
&& sudo route add -net 0.0.0.0 dev ppp0' - mount_command = 'sudo mount -t nfs 10.11.1.214:/home/zhanglei/code/OneToMany /home/casa/code/mount/OneToMany'
- if __name__=='__main__':
-
if os.system(pon_command) == 0: -
print 'your vpn is ready' -
else: -
print 'something error happen' -
time.sleep(5) -
if os.system(route_command) == 0: -
print 'your route is ready' -
else: -
print 'something error happen when route' -
if os.system(mount_command) == 0: -
print 'your mount is ready' -
else: -
print 'something error happen when mount' - else:
-
os.system('ls') -
os.system('ls') -
os.system('ls') -
os.system('ls') -
os.system('ls') -
os.system('ls')