源自:http://www.cnblogs.com/wupeiqi/articles/4906230.html
一、接收执行参数
sys.argv 接收执行参数的函数
#!usr/bin/env python #-*- coding:utf-8 _*- """ @author:Administrator @file: sysargv.py@time: 2017/11/19 """import sysprint (sys.argv)
执行结果
[[email protected] ~]# python index.py 8000
[‘index.py‘, ‘8000‘]
[[email protected] ~]# python index.py runserver
[‘index.py‘, ‘runserver‘]
列表,元祖:
区别,列表可以被修改,元祖不能被修改,列表包含元祖
元祖的元素不能被修改,元祖的元素的元素可以被修改。如下程序所示
#!usr/bin/env python #-*- coding:utf-8 _*- """ @author:Administrator @file: tuple.py@time: 2017/11/19 """t1 = (1,2,{‘k1‘:‘v1‘})#t1[2][‘k1‘] = 2#print(t1)t1[2] = 123print(t1)
执行报错:
Traceback (most recent call last):
File "C:/Users/Administrator/PycharmProjects/S12/2017-11-19/tuple.py", line 11, in <module>
t1[2] = 123
TypeError: ‘tuple‘ object does not support item assignment
#!usr/bin/env python #-*- coding:utf-8 _*- """ @author:Administrator @file: tuple.py@time: 2017/11/19 """t1 = (1,2,{‘k1‘:‘v1‘})t1[2][‘k1‘] = 2print(t1)
执行结果:
(1, 2, {‘k1‘: 2})
Process finished with exit code 0
对于python,一切事物皆是对象,对象基于类创建
字符串常用功能:
- 移除空白 strip
- 分割 slipt
- 长度 len
- 索引 index
- 切片 [1:] [2:5] [:-1]
4、列表
创建列表:
1 2 3 |
name_list = [ ‘alex‘ , ‘seven‘ , ‘eric‘ ]
|
基本操作:
- 索引 index
- 追加 append
- 删除 del
- 长度 len
- 切片 [1:] [2:5] [:-1]
- 循环 for while
- 包含 in
5、元祖
创建元祖:
1 2 3 |
ages = ( 11 , 22 , 33 , 44 , 55 )
|
基本操作:
- 索引 index
- 切片 [1:] [2:5] [:-1]
- 循环 for while
- 长度 len
- 包含 in
6、字典(无序)
创建字典:
1 2 3 |
person = { "name" : "mr.wu" , ‘age‘ : 18 }
|
常用操作:
- 索引
- 新增
- 删除 del pop remove
- 键、值、键值对 keys values items
- 循环 for while
- 长度 len
PS:循环,range,continue 和 break
int常用功能介绍:
age = int(18)
shift + 鼠标指向int,查看内置函数
求商和余数,执行结果得到两个数的元祖
#!usr/bin/env python #-*- coding:utf-8 _*- """ @author:Administrator @file: divmod.py@time: 2017/11/19 """all_list = 95pager = 10result = all_list.__divmod__(10)print(result)
执行结果:
(9, 5)
字典:
#!usr/bin/env python #-*- coding:utf-8 _*- """ @author:Administrator @file: dic.py@time: 2017/11/19 """dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘}print(dic[‘k1‘])print(dic[‘k2‘])#print(dic[‘k3‘])print(dic.get(‘k3‘,‘default‘))
for循环取k,v值
#!usr/bin/env python #-*- coding:utf-8 _*- """ @author:Administrator @file: dic.py@time: 2017/11/19 """dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘,‘k3‘:‘v3‘}print(dic[‘k1‘])print(dic[‘k2‘])print(dic[‘k3‘]) for k in dic.keys(): print(k)for v in dic.values(): print(v)for k,v in dic.items(): print(k,v)
执行结果:
v1
v2
v3
k1
k2
k3
v1
v2
v3
k1 v1
k2 v2
k3 v3
pop参数,必须指定一个值,因为字典是无序的,接上文截图
result = dic.pop(‘k3‘)print(result)
#!usr/bin/env python #-*- coding:utf-8 _*- """ @author:Administrator @file: dictionary.py@time: 2017/11/19 """‘‘‘有如下集合[11,22,33,44,55,66,77,88,99,110,121,132,143,154,165,176,187],将所有大于66的值保存在字典的第一个key中,将小于66的值保存在第二个key的值中即:{‘k1‘:大于66,‘k2‘:小于66}‘‘‘dic = {}all_list = [11,22,33,44,55,66,77,88,99,110,121,132,143,154,165,176,187]for i in all_list: if i > 66: if "k1" in dic.keys(): dic["k1"].append(i) else: dic[‘k1‘] = [i,] else: if "k2" in dic.keys(): dic["k2"].append(i) else: dic[‘k2‘] = [i,]print(dic[‘k1‘])print(dic[‘k2‘])