python脚本语言初步学习
来源:互联网 发布:新浪微博程序员 编辑:程序博客网 时间:2024/05/20 17:27
不错的参考:http://blog.csdn.net/hitlion2008/article/details/9285785
前言:python目前有2.0,3.0两个版本,其中2.0版本用的比较多,两个版本开发出的代码不一样,比较常见的脚本语言还有shell,perl等。脚本语言主要是为了缩短传统的编写、编译、链接、运行(c语言,java语言),脚本语言不需要走这四个过程,只需要调用的时候解释或编译,脚本语言一般都有简单易学易用等特点,python代码后缀以py结尾。
接写来进入python脚本语言的初步学习:
首先进入master节点,创建一个python_test文件夹
#ls
#cd python_test
#python//进入python环境,其中>>>代表命令解释器
//退出python解释器用quit()或者Ctrl+D
#touch 1.py //在python_test下创建一个新的python文件
#cat 1.py
#vim 1.py
1)字符串的输出,这是一个类似C语言printf和Java中的String.format()的操作符,它能格式化字串,整数,浮点等类型:语句是:
formats % (var1, var2,....)
它返回的是一个String.
#!/usr/bin/python //指定python解释器在哪里,指定目录print "hello world"name='zhangsan'#print "bigdata study!,my name is %s" %(name)print "bigdata study!,my name is", name
Esc :wq Enter//保存退出
#python 1.py //执行python 文件
#cat 1.py
输出结果为:
bigdata study!,my name is zhangsan
2)函数
如何定义函数
def function_name(args):
function_body;
调用函数的方式function_name(formal_args):
#!/usr/bin/pythondef print_name(name): print "bigdate study!your name is", nameprint_name('hahaha')print_name('pig')
输出结果为:
bigdate study!your name is hahaha
bigdate study!your name is pig
3)常用的数据结构
在java中常用的有hashmap,array(数组),set(集合)
在python中对应的分别为:
hashmap->dict->{}
array->list->[]
set->set->set()
①dict
相当于Java中的HashMap,用于以Key/Value方式存储的容器.创建方式为{key1: value1, key2: value2, ….}, 更改方式为dict[key] = new_value;索引方式为dict[key]. dict.keys()方法以List形式返回容器中所有的Key;dict.values()以List方式返回容器中的所有的Value
#!/usr/bin/pythoncolor={"red":0.2,"green":0.4,"black":0.9}print colorprint color["red"]
输出结果为:
{‘black’: 0.90000000000000002, ‘green’: 0.40000000000000002, ‘red’: 0.20000000000000001}
0.2
②list
#!/usr/bin/pythoncolor_list=['red','white','blue']print color_listprint color_list[2]
输出为:
[‘red’, ‘white’, ‘blue’]
blue
③set
#!/usr/bin/pythona_set=set()a_set.add('a')a_set.add('b')print a_set
输出结果为:
set([‘a’, ‘b’])
4)分支语句
其中逻辑表达式可以加上括号(),也可以不加.但如果表达式里面比较混乱,还是要加上括号,以提高清晰.但整体的逻辑表达式是可以不加的
格式为:
if expression:
blocks;
elif expression2:
blocks;
else:
blocks;
#!/usr/local/bina=1if a>0: print 'a gt 0'elif a==0: print 'a et 0'else: print 'a lt 0'
输出结果为:
a gt 0
5)for循环
与Java中的foreach语法一样, 遍历List:
for var in list:
blocks;
① list的for循环
#!/usr/local/binfor_list=[]for_list.append('111')for_list.append('222')for_list.append('333')for_list.append('444')for value in for_list: print value
输出结果为:
111
222
333
444
②dict的for循环
#!/usr/local/binfor_dict={}for_dict['aaa']=1for_dict['bbb']=2for_dict['ccc']=3for value in for_dict: print value
输出结果为:
aaa
bbb
ccc
由输出结果可见,上述只能输出键而不能输出值,如何输出键和值呢,修改代码如下:
#!/usr/local/binfor_dict={}for_dict['aaa']=1for_dict['bbb']=2for_dict['ccc']=3for_dict['eee']=4for key,value in for_dict.items(): print key+'======>'+str(value)
输出结果为(显然输出结果不是按照key值进行输出的):
eee======>4
aaa======>1
bbb======>2
ccc======>3
③针对set的for循环
#!/usr/local/binfor_set=set()for_set.add('a')for_set.add('b')for_set.add('c')for_set.add('d')for_set.add('e')for value in for_set: print value
输出结果为:
c
b
e
d
复杂点的for循环
#!/usr/local/binsum=0for value in range(1,11): sum=sum+value//sum+=valueprint sum
输出结果为:
55
6)while循环
#!/usr/python/bincnt=2while cnt>0: print 'I love python' cnt-=1
输出结果为:
I love python
I love python
#!/usr/python/bini=1while i<10: i+=1//python不支持i++ if i%2>0: continue print i
输出结果为:
2
4
6
8
10
7)字符串string
字符串就是一个字符的数组,List的操作都可以对String直接使用.
①len(str)和截取字符串
#!/usr/python/binstr='abvdefg'print len(str)print str[3:5]print str[3:7]
输出结果为:
7
de
defg
②小写转大写
#!/usr/python/binstr='aBCFvdefg'print str.lower()
输出结果为:
abcfvdefg
8)异常处理Exception ,try….catch
#!/usr/python/bintry: a=6 b=a/0except Exception ,e: print Exception,":",e
输出结果为:
<type 'exceptions.Exception'> : integer division or modulo by zero
IOError
#!/usr/python/bintry: print '1111' fs=open('testFile','r')except IOError,e: print 'The File does not exist',eelse: print 'The File exists' fs.close()
输出结果为:
1111
The File does not exist [Errno 2] No such file or directory: ‘testFile’
对于这个结果,那我们创建一个新文件testFile//touch testFile
再执行以上代码输出结果为:
1111
The File exists
9)import module
#!/usr/python/binimport mathprint math.pow(2,3)//2的3次方print math.floor(4.9)//取整数print round(4.9)//四舍五入
输出结果为:
8.0
4.0
5.0
#!/usr/python/binimport randomitems=[1,2,3,4,5,6]random.shuffle(items)print items
输出结果为:
[1, 4, 3, 2, 5, 6]
#!/usr/python/binimport randoma=random.randint(0,3)print a
输出结果为:1
#!/usr/python/binimport randoma_list=random.sample('abcdefg',3)//在abcdefg里面随机抽样print a_list
输出结果为:[‘g’, ‘e’, ‘f’]
- python脚本语言初步学习
- 从零开始学习Python脚本语言
- Python脚本语言学习
- 学习python脚本语言笔记(1)
- Python 脚本语言学习(二)- Hello World
- [python] 初步学习
- python初步学习教程
- 初步学习python
- Python编程学习初步
- python 初步学习
- python初步学习
- python学习初步
- Python初步学习
- python脚本语言
- python脚本语言
- python--脚本语言
- python脚本语言
- 脚本语言学习
- Java面向对象设计的7大原则
- 语法练习
- mac sublime 配置c++ 做个笔记给以后的自己
- jq判断复选框是否被选中的3中方法
- Android:Swipe与ScrollView的滑动冲突问题
- python脚本语言初步学习
- javascript 全选和全不选效果
- 最长单增子序列的打印
- SSM POI execle 导入篇 (含工具类)
- BZOJ 1565: [NOI2009]植物大战僵尸
- MPI入门
- js实现倒计时效果
- java acm 输入输出io问题
- Reshape the Matrix(leetcode)