常见脚本语言:python、perl、shell;
特点:缩短编译性语言编写,编译,链接,运行这四个阶段;只需要解释、编译;
一、基础命令
python
>>>为python解释器;
print "hello world"
#按回车即可打印
touch 1.py
vim 1.py
# 指定python解释器目录
#!/usr/bin/python
# 退出
print "hello world"
# 执行
python 1.py
# 打印文件到屏幕
cat 1.py
vim 1.py
name = 'zhangsan'
print "My name is %s" % (name)
python 1.py
vim 1.py
name = 'lisi'
print "My name is name",name
python 1.py
def print_name(name):
print "My name isname",name
print print_name('zhangsan')
print print_name('lisi')
def print_name(name):
print "My name isname",name
return ("%s,is a goodstudent" % name)
print print_name('zhangsan')
vim 2.python
#!/usr/bin/python
# 常见数据结构hashmap array set
#python: hashmap -> dict ->{} 字典
#python: array -> list ->[] 列表
#python: set -> set ->set() 集合
color ={"red":0.2,"green":0.4,"blue":0.4}
print color
print color['red']
print color['green']
color_list = ['red','green','blue','yellow']
print color_list
print color_list[2]
a_set = set()
a_set.add('1111')
a_set.add('2222')
a_set.add('3333')
print a_set
a_set.add('1111')
print a_set #结果不变
touch 3.python
#!/usr/bin/python
a = 1
if a > 0 :
print a
python 3.python
a = 1
if a > 0 :
print 'a gt 0'
elif a == 0:
print 'a eq 0'
else:
print 'a lt 0'
python 3.python
a_list = []
a_list.append('111')
a_list.append('333')
a_list.append('555')
a_list.append('222')
a_list.append('444')
for value ina_list:
print value
# 退出执行
python 3.python #会按添加顺序输出
b_dict = {}
b_dict['aaa'] = 1
b_dict['bbb'] = 2
b_dict['ccc'] = 3
b_dict['ddd'] = 4
b_dict['eee'] = 5
for value in b_dict:
print value
# 退出执行
python 3.python
# 结果入下
eee
aaa
bbb
ccc
ddd
for key,value in b_dict.items():
print key +"===>" + str(value) #value为整型需要转换为字符串后输出
# 退出执行
python 3.python
#输出不会按key的顺序,因为对key进行了hash取模
c_set = set()
c_set.add('a')
c_set.add('b')
c_set.add('c')
c_set.add('d')
c_set.add('e')
for value in c_set:
print value
# 退出执行
python 3.python
# 顺序也是被hash转化过的,所以不是正常顺序输出
for value in range(1,8):
print value
# python 3.python
# 输出结果为
# 1
# 2
# 3
# 4
# 5
# 6
# 7
sum = 0
for value in range(1,11):
sum += value
print sum
# python 3.python
# 输出结果为
# 55
cnt = 2
while cnt > 0:
print 'i love python'
cnt -=1
# python 3.python
# 结果共输出两次
# i love python
# i love python
i = 1
while i < 10:
i += 1
if i % 2 > 0:
continue
print i
# 输出
2
4
6
8
10
# python不支持 ++
# 如果continue 换成 break 则只输出2;跳出
str = 'abcdefg'
print len(str)
# 7
# 对list dict set 同样使用
print str[2:5] #从2开始,输出5-2位
print str[2:7] #从2开始,输出7-2位,没就没有了
str = 'sgsdDKKNk'
print str.lower() #妆化为小写
# try:
# pass
# except Exception, e:
# raise
# else:
# pass
# finally:
# pass
1) # eg1
try:
a = 6
b = a / 0
except Exception,e:
printException,":",e
2) # eg2
try:
print '111'
fh =open('testfile','r')
print '222'
except IOError,e:
print '333',e
else:
print '444'
fh.close()
# 抛出异常
# touch创建此文件后,不再报异常
import math
print math.pow(2,3) #求2的3次方
print math.floor(4.9) #向下取整
print round(4.9) #向上取整,round不在math模块,所以直接用
import random
items = [1,2,3,4,5,6]
random.shuffle(items) #随机化处理
print items
a = random.randint(0,3) #输出一个0-3 之间随机数
print a
s_list = random.sample('abcdefg',3)
print s_list
['a','g','f']
运用到的知识不难,但是要勤加练习!!