python学习笔记2――python文件类型、变量、数值、字符串、元组、列表、字典
一、Python文件类型
1、源代码
python源代码文件以.py为扩展名,由pyton程序解释,不需要编译
[root@localhost day01]# vim 1.py #!/usr/bin/python print 'hello world!' [root@localhost day01]# python 1.py hello world!
2、字节代码
Python源码文件经过编译后生成的扩展名为‘pyc’的文件
编译方法:
import py_compile
py_compile.compile('hello.py')
[amos@AAC-DMP-03 amos]$ vim 2.py #!/usr/bin/pyton import py_compile py_compile.compile('./1.py') [root@localhost day01]# vim 2.py #!/usr/bin/python import py_compile py_compile.compile('1.py') [root@localhost day01]# python 2.py [root@localhost day01]# ls 1.py 1.pyc 2.py [root@localhost day01]# python 1.pyc hello world!
3、优化代码
经过优化的源码文件,扩展名为“pyo”
-python -O -m py_compile hello.py
[root@localhost day01]# python -O -m py_compile 1.py [root@localhost day01]# ll total 16 -rw-r--r--. 1 root root 41 Feb 21 17:55 1.py -rw-r--r--. 1 root root 113 Feb 21 17:56 1.pyc -rw-r--r--. 1 root root 113 Feb 21 18:15 1.pyo -rw-r--r--. 1 root root 67 Feb 21 17:56 2.py [root@localhost day01]# python 1.pyo hello world!
二、Python变量
变量是计算机内存中的一块区域,变量可以存储规定范围内的值,而且值可以改变。
python下变量是对一个数据的引用
python是指向内存的另外一块区域,而C语言是对内存的一块区域的值重新赋值
变量的命名
变量名由字母、数字、下划线组成
不能以数字开头
不可以使用关键字
-a a1 _a
变量的赋值
是变量的申明和定义的过程
a = 1
id(a) //发现从新赋值a之后,变量a在内存中的地址从7601952变化为16579968
[root@localhost day01]# ipython Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56) Type "copyright", "credits" or "license" for more information. IPython 1.2.1 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details. In [1]: a=123 In [2]: print a 123 In [3]: id(a) Out[3]: 7601952 In [4]: id(a) Out[4]: 7601952 In [5]: a = 456 In [6]: id(a) Out[6]: 16579968
三、Python中的运算符与表达式
Python的运算符包括
赋值运算符 x = 3,y = 'abcd' *= /= %= x+=2 x=x+2 x-=2 x=x-2
算数运算符 + - * / % // **
关系运算符 > < >= <= == != 返回结果是bool值true或者false
逻辑运算符 and逻辑与:true and false or逻辑或false or true not逻辑非not true
定义变量不需要申明字符类型
In [17]: x=2 In [18]: type(x) Out[18]: int In [19]: x='david' In [20]: type(x) Out[20]: str
算数运算符真是简洁明了4.00 // 3=1.0表示取整,3**2=9 表示3的平方
In [29]: 'a' +'b' Out[29]: 'ab' In [30]: 3-4 Out[30]: -1 In [31]: 3*2 Out[31]: 6 In [32]: 4/3 Out[32]: 1 In [33]: 4.0 /3 Out[33]: 1.3333333333333333 In [34]: 4.0 // 3 Out[34]: 1.0 In [35]: 4.00 // 3 Out[35]: 1.0 In [36]: 4%3 Out[36]: 1 In [37]: 2**3 Out[37]: 8 In [38]: 3**2 Out[38]: 9
关系运算符
In [39]: 1>2 Out[39]: False In [40]: 1<9 Out[40]: True In [41]: 1!=9 Out[41]: True
逻辑运算符
In [42]: 1==1 and 2>1 Out[42]: True In [43]: 1==1 and 2<1 Out[43]: False In [44]: 1==1 or 2<1 Out[44]: True In [45]: not 1==2 Out[45]: True
Lambda(从上到下,优先级越来越高,同行右侧优先级更高)
逻辑运算:or
逻辑运算: and
逻辑运算:not
成员测试:in,not in
同一性测试:is,is not
比较:<,<=,>,=>,!==,==
按位或:|
按位异或:^
按位与: &
移位:<<,>>
加法与减法:+,-
乘法与除法和取余:*,/,%
正负号:+x,-x
按位翻转:~x
指数:**
例子:写一个四则运算器
要求从键盘读取数字
[root@localhost day01]# vim 3.py #!/usr/bin/python num1=input('Please input a number') num2=input('Please input a number') print "%s+%s=%s" %(num1,num2,num1+num2) print "%s-%s=%s" %(num1,num2,num1-num2) print "%s*%s=%s" %(num1,num2,num1*num2) print "%s/%s=%s" %(num1,num2,num1/num2) [root@localhost day01]# python 3.py Please input a number2 Please input a number3 2+3=5 2-3=-1 2*3=6 2/3=0
input()与raw_input()去别:
input() 接受数字和字符串
raw_input()全部解析为字符串
In [47]: input ("Please input: ") Please input: 123 Out[47]: 123 In [48]: input ("Please input: ") Please input: abc #这里报错,input输入字符串必须加'abc’引号,否则识别不了 --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-48-ae0272fccd43> in <module>() ----> 1 input ("Please input: ") <string> in <module>() NameError: name 'abc' is not defined In [49]: input ("Please input: ") Please input: 'abc' Out[49]: 'abc' In [50]: input ("Please input: ") Please input: 6789 Out[50]: 6789 In [51]: raw_input ("Please input: ") Please input: sdfsahfha Out[51]: 'sdfsahfha' In [52]: 234 Out[52]: 234 In [53]: raw_input ("Please input: ") Please input: 3242 Out[53]: '3242' In [54]: raw_input ("Please input: ") Please input: abc Out[54]: 'abc'
四、Python数据类型
数值 整形int 长整形long 浮点型float(3e+7)复数-3.14j In [4]: type(3.14j)Out[4]:complex
序列 包括:字符串、列表、元祖,主要操作是索引操作符和切片操作符,
索引操作符让我从序列中抓取一个特定项目,
切片操作符让我们能够获取序列的一个切片,即获取一部分
序列的基本操作:
1、len() 求序列的长度
2、+ 连接2个序列
3、* 重复序列元素
4、in 判断元素是否在序列中
5、max() 返回最大值
6、min() 返回最小值
7、com(x,y)比较两个序列是否相等
字符串 三种方法定义字符串,str='this is a string' str="s is a string"
str='''this is a string''' 三重引号(docstring)能定义字符串外,还可以用作注释\'
列表 列表是处理一组有序项目的数据结构,即可在列表中存储一个序列的项目
列表是可变类型的数据
创建列表:
list1=[]
list2=list()
list3=['a',1,2]
元组 元祖和列表十分相识,元组和字符串一样不可变,元组可以存一系列的值,可以索引和切片
元组在用户定义的函数能够安全的采用一组值的时候,即被使用的元组的值不会改变
字典
字符串的切片操作:
In [42]: a='abcde' In [43]: a Out[43]: 'abcde' In [44]: a[0] Out[44]: 'a' In [45]: a[1] Out[45]: 'b' In [46]: a[4] Out[46]: 'e' In [47]: a[-1] Out[47]: 'e' In [48]: a[0]+a[1] Out[48]: 'ab' In [49]: a[0:2] #取值下标注0和1,不包括最后一个2 Out[49]: 'ab' In [50]: a[:2] Out[50]: 'ab' In [51]: a[1:2] Out[51]: 'b' In [52]: a[1:] Out[52]: 'bcde' In [53]: a[:] Out[53]: 'abcde' In [54]: a[-1] Out[54]: 'e' In [55]: a[:-1] #从头开始,不包括最后一个-1对应的e Out[55]: 'abcd' In [56]: a[::1] Out[56]: 'abcde' In [57]: a[::2]#步进值为2 Out[57]: 'ace' In [58]: a[::-1]#步进值-1,从后向前取值 Out[58]: 'edcba' In [59]: a[::-2] Out[59]: 'eca' In [61]: a Out[61]: 'abcde' In [60]: a[-4:-2] Out[60]: 'bc' In [62]: a[-2:-4:-1] 从右到左,-2 d -3c -4b不取值,-1表示从右到左 Out[62]: 'dc'
序列操作:
In [74]: a=12 #12*2=24,因为a的值是整形12 In [75]: a*2 Out[75]: 24 In [72]: a='12345' #重复序列元素,因为a是字符串 In [73]: a*2 Out[73]: '1234512345' In [77]: '#'*50 Out[77]: '##################################################' In [78]: a=123 In [79]: a+4 Out[79]: 127 In [82]: a = 'abc' In [83]: a in a # in 判断元素是否在序列中 Out[83]: True In [85]: 'f' in a Out[85]: False In [86]: 'f' not in a Out[86]: True In [87]: 'f' not in a+'f' #a+'f'=abcf Out[87]: False In [90]: a Out[90]: 'abc' In [91]: max(a) #返回最大值 Out[91]: 'c' In [92]: min(a) #返回最小值 Out[92]: 'a' #com(x,y)比较两个序列是否相等 In [94]: help(cmp) #查看cmp比较命令 Help on built-in function cmp in module __builtin__: cmp(...) cmp(x, y) -> integer Return negative if x<y, zero if x==y, positive if x>y. In [96]: cmp(a,'abc') Out[96]: 0 #返回0表示 a='abc' In [97]: cmp(a,'abcd') #返回-1表示 a<'abcd' Out[97]: -1 In [98]: cmp(a,'ab') #返回1表示 a>'ab' Out[98]: 1
元组定义(通常用于接收函数的返回值)、元组的拆分
In [107]: t=('a',1,(1,)) In [108]: t Out[108]: ('a', 1, (1,)) In [116]: type(t) Out[116]: tuple In [118]: t1=(1) #默认定义(1)为int,(1,)定义为元组tuple In [119]: type(t1) Out[119]: int In [120]: t2=(1,) In [121]: type(t2) Out[121]: tuple In [122]: t3=(1,t2) In [123]: t3 Out[123]: (1, (1,)) #元组的拆分,接收元组的值 In [127]: t4=(a,'b','c') #a表示变量a In [128]: t4 Out[128]: ('abcd', 'b', 'c') In [129]: first,second,third=t4 In [130]: first Out[130]: 'abcd' In [131]: second Out[131]: 'b' In [132]: third Out[132]: 'c' In [142]: t4.count('abcd') #a='abcd' Out[142]: 1 In [144]: t4.count(a) Out[144]: 1 In [143]: t4.count('a') Out[143]: 0 Out[155]: ('abcd', 'b', 'c', 'b') In [156]: t4.index('b') #显示第一个b的位置 Out[156]: 1 In [157]: t4.index('c') Out[157]: 2
表达式是将不同的数据(包括变量、函数)用运算符好号按照一定的规则连接起来的一种式子
赋值运算符