数据类型--字符串--list

1. Python的文件类型

1. 源代码--直接由Python解析

vi 1.py  
#!/usr/bin/python
print 'hello world'

这里的1.py就是源代码
执行方式和shell脚本类似:

    1. chmod +x 后,./1.py
    1. Python 1.py

2. 字节代码

  • Python源码文件经编译后生成的扩展名为pyc的文件
  • 编译方法:
[root@t1 py]# cat 2.py 
#!/usr/bin/python
import py_compile 
py_compile.compile('1.py')  

写一个2.py脚本,执行,界面没有输出,但是看下文件列表,多了一个1.pyc

[root@t1 py]# python 2.py 
[root@t1 py]# ll
总用量 12
-rw-r--r-- 1 root root  38 12月 20 21:06 1.py
-rw-r--r-- 1 root root 112 12月 20 21:10 1.pyc
-rw-r--r-- 1 root root  66 12月 20 21:09 2.py

怎么执行?还是python 2.py。
而且,如果生成1.pyc文件后,源码文件1.py不在了,2.py照样可以执行

3. 优化代码

  • 经过优化的源码文件,扩展名为pyo
  • python –O –m py_compile 1.py
[root@t1 py]# python -O -m py_compile 1.py
[root@t1 py]# ls
1.py  1.pyc  1.pyo  2.py

执行优化代码后,生成1.pyo。执行1.pyo

[root@t1 py]# python 1.pyo
hello world

2.python的变量

  • 变量是计算机内存中的一块区域,变量可以存储规定范围内的值,而且值可以改变。

  • Python下变量是对一个数据的引用

  • 变量的命名

    • 变量名由字母、数字、下划线组成。
    • 变量不能以数字开头
    • 不可以使用关键字
    • a a1 _a
  • 变量的赋值

    • 是变量的声明和定义的过程
    • a = 1
    • id(a) #id显示a在内存的位置号
In [1]: a = 123

In [2]: id(a)
Out[2]: 25933904

In [3]: a = 456 

In [4]: id(a)
Out[4]: 33594056

In [5]: x = 'abc'

In [6]: x = abc 
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
 in ()
----> 1 x = abc

NameError: name 'abc' is not defined

上面报错的解释,默认情况下:

数字直接写表示数字   
数字带引号表示字符串   
字符带引号表示字符串  
字符不带引号表示变量

Python不需要事先声明变量的类型,自动判断

In [7]: a = 456

In [8]: type(a)
Out[8]: int

type查出a的变量类型是整数int

In [9]: a = '456'

In [10]: type(a)
Out[10]: str

type查出a的变量类型是字符串str

  • Python运算符包括

1.赋值运算符

=:     x = 3,   y = ‘abcd’      #等于
+=:    x += 2      #x=x+2
-=:    x -= 2      #x=x-2
*=:    x *= 2      #x=x*2
/=:    x /= 2      #x=x/2
%=:    x %= 2      #取余数

2.算术运算符

+
-
*
/
//
%
**

举例1:

In [20]: a = 1 ;b = 2

In [21]: a+b
Out[21]: 3

In [22]: 'a' + 'b'
Out[22]: 'ab'

ab赋值后,a+b是数字。直接加两个字符就是合在一起的字符串
举例2:

In [24]: 4 / 3
Out[24]: 1

In [25]: 4.0 / 3
Out[25]: 1.3333333333333333

4直接除3,因为默认是整数,所以结果取整数1
要想得到小数,将4变成浮点数4.0
特别的,//表示强制取整

In [26]: 4.0 // 3
Out[26]: 1.0

举例3:

In [27]: 2 ** 3
Out[27]: 8

In [28]:  2 * 3
Out[28]: 6

一个*是乘,两个**是幂

3.关系运算符

> :    1 > 2
< :    2 < 3
>=:    1 >= 1
<=:    2 <= 2
==:    2 == 2
!=:    1 != 2

In [33]: 1 > 2
Out[33]: False

In [34]: 1 < 2
Out[34]: True

成立就是true,不成立false

4.逻辑运算符

and逻辑与: True and False
or逻辑或: False or True
not逻辑非: not True

举例:

In [35]: 1 < 2 and 1 >2
Out[35]: False

In [36]: 1 < 2 or  1 >2
Out[36]: True

In [37]: not 1 > 2
Out[37]: True

运算优先顺序:
[图片上传失败...(image-17c84-1517312622928)]

  • input和raw_input
    input适合数字,raw_input适合字符
In [41]: input("input num:")
input num:123
Out[41]: 123

In [42]: input("input num:")
input num:abc
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
 in ()
----> 1 input("input num:")

 in ()

NameError: name 'abc' is not defined

In [43]: input("input num:")
input num:'abc'
Out[43]: 'abc'

In [44]: raw_input("input num:")
input num:abc
Out[44]: 'abc'

有上面可以看出在input下面,直接输入abc报错,但是raw_input正常显示。
由此可以写一个计算脚本

[root@t1 py]# cat cal.py 

#!/sur/bin/python
num1 = input("please input a num :")

num2 = input("please input a num :")

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) 

%s分别对应后面的数值
执行脚本

[root@t1 py]# python cal.py 
please input a num :5
please input a num :6
5 + 6 = 11
5 - 6 = -1
5 * 6 = 30
5 / 6 = 0

3. Python的数值和字符串

数值类型:

  • [ ] 整形int
    整型int可以存储2^32个数字,范围-2,147,483,648到2147483647
    例如:0,100,-100

  • [ ] 长整型long
    Long的范围很大,几乎可以说任意大的整数均可以存储。
    为了区分普通整型,需要在整数后加L或l。
    例如: 2345L,0x34al

  • [ ] 浮点float
    例如:0.0,12.0,-18.8,3e+7等

  • [ ] 复数型complex
    Python对复数提供内嵌支持,这是其他大部分软件所没有的。
    复数例子:- 3.14j,8.32e-36j

  • [ ] 字符串 string
    有三种方法定义字符串类型
    • str = ‘this is a string’ #普通字符串
    • str = “this is a string” #能够解析\n等特殊字符
    • str = ‘’‘this is a string’‘’ #可以略去\n
      三重引号(docstring)除了能定义字符串还可以用作注释。

举例:

In [3]: a = '''hello
   ...: world'''

In [4]: a
Out[4]: 'hello\nworld'

In [5]: print a
hello
world
  • 字符串索引,0开始,-1表示最后一个,-2倒数第二个,类推
In [6]: a = 'abcde'
In [7]: a[0:2]
Out[7]: 'ab'

a[]表示取索引指定的字符,[0:2]可以类比数学中的0<=a<2,即0<=a<=1,就是取第一个和第二个,不包括第三个

In [8]: a[:2]
Out[8]: 'ab'
In [9]: a[:]
Out[9]: 'abcde'

0或者-1可以省略

  • 字符串切片
In [11]: a[0:3:2]
Out[11]: 'ac'

只取a和c,首先应该是a[0:3],但是这样的结果是abc,a[0:3:2]的2表示步进2个,跳过b。同理,如果是a[0:3:3]表示步进3。
如果要将整个字符串倒过来,需要用-1

In [17]: a[::-1]
Out[17]: 'edcba'

可以类比下面的

In [16]: a[:-1]
Out[16]: 'abcd'

来个更加直观的

In [13]: a[-2:-4:-1]
Out[13]: 'dc'

取倒数第2个和倒数第三个,而且倒序显示

输出字符串推荐格式:

print("{0}".fomat(string))

4. 列表

1.定义

列表(list)是处理一组有序项目的数据结构,即可以在列表中存储一个序列的项目。
列表是可变类型的数据
创建列表:
list1 = []
list2 = [('xxx',),]
list3 = [‘a’,1,2]

2. 列表操作

  • 取值
    切片和索引
In [44]: list3[0]
Out[44]: 'a'        #取索引0

In [45]: list3[5]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
 in ()
----> 1 list3[5]

IndexError: list index out of range
  • 追加
    list.append()
In [47]: list3.append(list2)

In [48]: list3
Out[48]: ['a', 1, 2, [('xxx',)]]

在list3后面追加一个元素,即序列2.追加只能在最后。

  • 插入
    list.insert(索引号,要插入的)
 In [49]: list3.insert(0,list2)

In [50]: list3
Out[50]: [[('xxx',)], 'a', 1, 2, [('xxx',)]]
  • 删除
    del list[]
    list.remove(list[])
In [51]: del list3[0]    #0是索引---del删除索引

In [52]: list3
Out[52]: ['a', 1, 2, [('xxx',)]]
In [54]: list3.remove(list3[3])        #这里的list3[3]可以换成元素---remove删除元素

In [55]: list3
Out[55]: ['a', 1, 2]

可以对比两张删除,其中list.remove是自带的

  • 修改
    list[] = x
In [56]: list3[2] = 888

In [57]: list3
Out[57]: ['a', 1, 888]
  • 查找
    var in list
In [58]: 888 in list3
Out[58]: True
In [60]: 999 in list3
Out[60]: False

更多操做见下面:


In [61]: list3.
list3.append   list3.extend   list3.insert   list3.remove   list3.sort     
list3.count    list3.index    list3.pop      list3.reverse  
  • 排序sort +反转reverse
In [8]: list3
Out[8]: ['a', 1, 2]

In [10]: list3.sort()

In [11]: list3
Out[11]: [1, 2, 'a']

In [12]: list3.reverse()

In [13]: list3
Out[13]: ['a', 2, 1]
  • pop默认删除最后一个

In [13]: list3
Out[13]: ['a', 2, 1]

In [14]: list3.pop()
Out[14]: 1

In [15]: list3
Out[15]: ['a', 2]

如果指定元素,就删除指定的

In [16]: list3.pop(0)
Out[16]: 'a'

In [17]: list3
Out[17]: [2]

现在删除的是0号

  • extend可迭代追加
In [18]: list3.extend(range(3))        #追加0到3以内的数字

In [19]: list3
Out[19]: [2, 0, 1, 2]

In [20]: list3.extend('qwert')        #追加字符串的单个字母

In [21]: list3
Out[21]: [2, 0, 1, 2, 'q', 'w', 'e', 'r', 't']

In [22]: list3.extend(('a','s',999)    #追加元祖的每个元素
   ....: )

In [23]: list3
Out[23]: [2, 0, 1, 2, 'q', 'w', 'e', 'r', 't', 'a', 's', 999]

你可能感兴趣的:(数据类型--字符串--list)