python学习笔记02

一.模块

模块使用import导入使用

1.sys模块:

sys模块是用c语言写的,所以在lib下是不会有sys.py这个文件存在

1 import sys
2 print(sys.path)   #打印环境变量
3 print(sys.argv)   #打印相对路径

 

2.os模块

os模块可以执行系统命令。

1 # author:"Jason lincoln"
2 import os
3 #cmd_res=os.system("dir") #只执行命令,不保存结果
4 cmd_res = os.popen("dir").read()
5 print("-->",cmd_res)
6 os.mkdir("new_dir") #创建一个目录

 

二.数据类型与数据运算 

基本与C语言相同。需要注意的是以下几种运算符与C语言的区别。

1.赋值运算

==

+=   c+=a等效于c=c+a

-=    c-=a等效于c=c-a

*=    c*=a等效于c=c*a

%=    c%=a等效于c=c%a

//=    c//=a等效于c=c//a

**=    c**=a等效于c=c**a

2.逻辑运算

and 布尔“与” ,x和y同时为真即x and y为真,否则为假

or   布尔“或”,x和y只要有一个为真即 x or y 为真 ,X和y同时为假则x or y为假

not  布尔“非”,x为真则返回为假,x为假则返回为真

3.成员运算

in         如果在指定序列中找到值返回ture,否则返回Fasle.

in not   如果在指定的序列中没有找到值就返回ture,否则返回false

4.身份运算

is        is是判断两个标识符是不是引用自一个对象

is not  is not 是判断两个标识符是不是引用自不同对象

 

5.三元运算

 

1 # author:"Jason lincoln"
2 
3 a,b,c=1,3,5
4 d=a if a>b else c
5 print(d)

输出“5”

 

6.encode和decode的转换

三.列表

1.列表是最常用的Python数据类型,它可以作为一个方括号内的逗号分隔值出现。列表的数据项不需要具有相同的类型创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可。

# author:"sun"

names=["Zhangyang","GuYun","Xuliangchen","xiangpeng"]

 

2.列表操作

不设置数字时切片冒号前默认为0

 1  1 #print(names[1:3])           #顾头不顾尾 从第一个开始取到第三个不包括第三个
 2  2 #print(names[3])
 3  3 print(names[-2:])            #从左往右数
 4  4 ##['Xuliangchen', 'xiangpeng']
 5  5 print(names[0:3])
 6  6 ##['Zhangyang', 'GuYun', 'Xuliangchen']
 7  7 print(names[:3])
 8  8 ##['Zhangyang', 'GuYun', 'Xuliangchen']
 9  9 names.append("Leihaidong")
10 10 print(names)
11 11 ##['Zhangyang', 'GuYun', 'Xuliangchen', 'xiangpeng', 'Leihaidong']
12 12 names.insert(1,"chenronghua")
13 13 print(names)
14 14 ##['Zhangyang', 'chenronghua', 'GuYun', 'Xuliangchen', 'xiangpeng', 'Leihaidong']

删除操作

1 ames.remove("chenronghua")
2 del names[1]
3 names.pop()  #如果不输入默认删除最后一个
4 names.pop(1) #和del names[1]效果一样

列表修改

1 print(names)
2 print(names.index("GuYun"))  
3 print(names[names.index("GuYun")])

统计

统计值一样的个数

1 print(names.count("chenronghua"))

清空

names.clear()

反转

将整个列表翻转

names.reverse()

 

排序

names.sort

合并

names.extend()

删变量

del 变量名

3.深copy与浅copy

浅copy修改原来的列表从被copy的列表也会改变

复制代码
1 names=["Zhangyang","GuYun",["alex","jack"],"xiangpeng","Xuliangchen"]
2 name2=names.copy()
3 print(names)
4 print(name2)
5 names[3]="向鹏"  
6 names[2][0]="ALEX"  #浅copy,只copy内存地址,同一块内存
7 print(names)
8 print(name2)
复制代码

 

深copy则是完全的复制。用copy.deepcopy()
复制代码
 1 # author:"sun"
 2 import copy
 3 names=["Zhangyang","GuYun",["alex","jack"],"xiangpeng","Xuliangchen"]
 4 #name2=copy.copy(names)  #和浅copy一样
 5 name2=copy.deepcopy(names) #深copy
 6 print(names)
 7 print(name2)
 8 names[3]="向鹏"
 9 names[2][0]="ALEX"  #浅copy,只copy内存地址
10 print(names)
11 print(name2)
复制代码

 浅copy

 1 # author:"sun"
 2 
 3 import copy
 4 
 5 person=['name',['a',100]]
 6 p1=copy.copy(person) #浅copy
 7 p2=person[:]         #浅copy
 8 p3=list(person)      #浅copy
 9 
10 p1[0]='alex'
11 p2[0]='fengjie'
12 print(p1)
13 print(p2)

 

四.元组

Python的元组与列表类似,不同之处在于元组的元素不能修改。元组使用小括号,列表使用方括号。元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。

1 names=('jack','alex')
2 names.index 3 names.count

五.字符串

 

字符串是 Python 中最常用的数据类型。我们可以使用引号('或")来创建字符串。

 

创建字符串很简单,只要为变量分配一个值即可。例如:

#! user/bin/env python
# -*- coding:utf-8 -*-
# Author:Sun

str1 = 'Hello world'
print(str1)

1.字符串操作

要注意字符串是不可修改的。

name.capitalize()  #首字母大写
name.casefold()   #大写全部变小写
name.center(50,"-")  #以--补齐50位,字符串在中间
name.count('lex') #统计 lex出现次数
name.encode()  #将字符串编码成bytes格式
name.endswith("Li")  #判断字符串是否以 Li结尾
 "sun\tc".expandtabs(10) #输出'sun      c', 将\t转换成多长的空格 
 name.find('A')  #查找A,找到返回其索引, 找不到返回-1 
#feomat制表函数
format :
    >>> msg = "my name is {}, and age is {}"
    >>> msg.format("alex",22)
    'my name is alex, and age is 22'
    >>> msg = "my name is {1}, and age is {0}"
    >>> msg.format("alex",22)
    'my name is 22, and age is alex'
    >>> msg = "my name is {name}, and age is {age}"
    >>> msg.format(age=22,name="ale")
    'my name is ale, and age is 22'
format_map
    >>> msg.format_map({'name':'alex','age':22})
    'my name is alex, and age is 22'


msg.index('a')  #返回a所在字符串的索引
'9aA'.isalnum()   #True

'9'.isdigit() #是否整数
name.isnumeric  #是否只有数字在其中
name.isprintable #是否可打印
name.isspace #判定是否为空格
name.istitle #判定是否为标题
name.isupper #把小写字母变大写
 "|".join(['alex','jack','rain']) #将 |插入每个元素之中
'alex|jack|rain'


str.maketrans()#将两段字符串一一对应。 
    >>> intab = "aeiou"  #This is the string having actual characters. 
    >>> outtab = "12345" #This is the string having corresponding mapping character
    >>> trantab = str.maketrans(intab, outtab)
    >>> 
    >>> str = "this is string example....wow!!!"
    >>> str.translate(trantab)
    'th3s 3s str3ng 2x1mpl2....w4w!!!'

 msg.partition('is')   #输出 ('my name ', 'is', ' {name}, and age is {age}') 

 >>> "alex li, chinese name is lijie".replace("li","LI",1)
     'alex LI, chinese name is lijie'

 msg.swapcase #大小写互换


 >>> msg.zfill(40)
'00000my name is {name}, and age is {age}'



>>> n4.ljust(40,"-")
'Hello 2orld-----------------------------'
>>> n4.rjust(40,"-")
'-----------------------------Hello 2orld'


>>> b="ddefdsdff_哈哈" 
>>> b.isidentifier() #检测一段字符串可否被当作标志符,即是否符合变量命名规则
True

六.字典

字典是另一种可变容器模型,且可存储任意类型对象。

字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下所示:

d = {key1 : value1, key2 : value2 }

1.创建字典

可以直接赋值,也可以使用dict函数创建字典

item1 = {'key1' : 'value1', 'key2' : 'value2' }
item2 = [('1','hello'),('2','world')]
item2 = dict(item2)

2.字典特性

  • 字典是无序的
  • 关键字必须是唯一的

3.字典操作

增加

 

item2['3'] = '!'

修改

item2['3'] = '!'
print(item2)
item2['3'] = '!!!'
print(item2)

删除

删除有pop和del两种。

item2.pop('3')
print(item2)
del item2['2']
print(item2)

查找

查找有三种方法

in 是 标准用法,返回ture或false用.get()查找键返回值,或者直接输出键查找。但是直接查找如果一个key不存在,就报错,get不会,不存在只返回None

i1 = '1' in item2
print(i1)
i2 = item2.get('1')
print(i2)
i3 = item2.get('5')
print(i3)

4.多级字典嵌套及操作

类似于C++的多维数组操作

a = dict()
a['b'] = {'c': 2}
print(a['b']['c'])

 

posted @ 2017-05-17 02:06  打包餐巾纸  阅读(122)  评论(0)    收藏  举报