python模块简单学习
import sys
import sys #利用import语句输入sys模块,sys模块包含了与python解释器和它的环境有关的函数
print ('The command line arguments are:')
for i in sys.argv:
print (i)
print ('\n\n The python path is',sys.path,'\n')
#当Python执行import sys语句的时候,将在sys.path变量中所列目录中寻找sys.py模块。如果找到了这个文件
#这个模块的主块中的语句将被运行,这个模块即可使用。
模块基本上就是包含了所有定义的函数和变量的文件,为了在其他程序中重用模块,模块的文件名必须以.py为扩展名。
使用模块
theFirstPro.py
def sayhi():
print('Hi,this is mymodule speaking.')
version='0.1'
调用自定义的模块
theSecondPro.py
import theFirstPro
theFirstPro.sayhi()
print('Version',theFirstPro.version)
使用列表 list是一种有序的集合,可以随时添加和删除其中的元素。
- 定义一个list,使用[],用len()函数可以获得list元素的个数;
- 用索引来访问list中每一个位置的元素,索引从0开始;
- 若要获取最后一个元素,除了计算索引位置外,还可以用-1做索引,直接获取最后一个元素;
- list是一个可变的有序表,可以在list中追加元素到末尾(listname.append('value'));也可以把元素插入到指定的位置(listname.insert(1,'value'));
- 若要删除list末尾的元素,用pop()方法(listname.pop());
- 要删除指定位置的元素(pop(i),i为索引位置);
- 要把某个元素替换成别的元素,可以直接赋值给对应的索引位置(listname[1]='value');
- list里面的元素的数据类型也可以不同(listname=['apple',22,false]);
- list元素也可以是另一个list,(如 listname=['233','abd',listname2]);
- list中一个元素也没有,空的list,长度为0.
#使用列表
shopList=['apple','mango','carrot','banana']
print('I have',len(shopList),'item to purchase.')
print('There items are:')
for item in shopList:#在列表各项目间递归
print(item)
print('\n I also have to buy rice')
shopList.append('rice')#添加一个项目
print('My shopping list is now',shopList)
print('I will sort my list now')
shopList.sort()#使用sort方法对列表排序,影响的是列表而不是返回一个修改后的列表,跟字符串工作的方式不同
#列表可变,字符串不可变。
print('Sort shopping list is',shopList)#打印列表
print('The first item I will buy is',shopList[0])
olditem=shopList[0]
del shopList[0] #删除列表中的项目
print('I bought the',olditem)
print('My shopping list is now',shopList)
使用元组(tuple)
tuple与list非常类似,但是tuple一旦初始化就不能修改。无法使用append() insert()方法。因为tuple不可变,所以代码更安全。定义t=(1),是定义了一个数1,t=(1,),定义了元组,有一个元素为1。tuple每个元素,永远指向不变。若有个元素为list,list的内容可以改变,指向list不可变。
#元组和列表类似,跟字符串一样是不可变的,不能修改。元组用来使语句或用户定义的函数能够安全使用一组值。
zoo=('wolf','elephant','penguin')
print('Number of animals in the zoo is',len(zoo))
new_zoo=('monkey','dolphin',zoo)
print('Number of new zoo is ',len(new_zoo))#len获得元组的长度
print(new_zoo[2],'\n',new_zoo[2][2])#访问元组中的某个项目
#0个项目的元组
myempty=()
#1个项目的元组,必须在第一个项目(唯一的项目)后跟一个逗号,用来区分元组和表达式中一个带圆括号的对象
singleton=('one',)
#打印
name='Join'
age=24;
print('%s is %d years old'%(name,age))#使用跟着%符号的项目元组的字符串
print('His name is %s'%name)
#encoding=utf-8
#元组和列表类似,跟字符串一样是不可变的,不能修改。元组用来使语句或用户定义的函数能够安全使用一组值。
zoo=('wolf','elephant','penguin')
#注意以下三种print方式的显示不同
print('Number of animals in the zoo is ',len(zoo))
print('Number of animals in the zoo is %d'%len(zoo))
print'Number of animals in the zoo is',len(zoo)
new_zoo=('monkey','dolphin',zoo)
print('Number of new zoo is ',len(new_zoo))#len获得元组的长度
print(new_zoo[2],'\n',new_zoo[2][2])#访问元组中的某个项目
#0个项目的元组
myempty=()
#1个项目的元组,必须在第一个项目(唯一的项目)后跟一个逗号,用来区分元组和表达式中一个带圆括号的对象
singleton=('one',)
#打印
name='Join'
age=24;
print('%s is %d years old'%(name,age))#使用跟着%符号的项目元组的字符串
print('His name is %s'%name)
print 'His name is %s'%name
![](https://i-blog.csdnimg.cn/blog_migrate/55583527a16fe15e8782208e87f68d21.png)
使用字典
#键值对 d={key1:value1,key2:value2}
addBook={'A':'Abandom','L':'larry','V':'Vivi','S':'Speaking'}
print('A\'s address is %s'%addBook['A'])
addBook['C']='Common' #增加一个键值对
del addBook['L']#删除一个键值对
print('\n There are %d contacts in the addBook\n'%len(addBook))
for name,address in addBook.items():#使用items,返回元组列表
print('Contact %s at %s'%(name,address))
if 'S' in addBook: #in操作符检验一个键/值对是否存在
print('\n S\'s address is %s'%addBook['S'])
序列
#列表、元组、字典都是序列
str1='hello'
print(str1[-1])# 'o' 索引为负数时,位置是从序列尾开始计算的。
print(str1[1:4]) #'ell' 切片操作符,第一个(冒号之前)表示切片开始的位置,第二个数(冒号之后)表示切片到哪里结束
#注意返回的序列从开始位置开始,刚好在结束位置之前结束,即开始位置是包含在序列切片中的,而结束位置被排斥在切片外
print(str1[1:-2])#'el' 负数用在从序列尾开始计算的位置
引用
#当创建一个对象并给它赋一个变量时,这个变量仅仅引用那个对象,而不是表示这个对象本身,即变量名指向计算机中
#存储那个对象的内存,称为名称到对象的绑定
shopList=['apple','mango','carrot','banana']
myList=shopList #新的变量名,两个名称都引用同一个对象
del shopList[1]
print('shopList is ',shopList)
print('myList is ',myList)
print('Copy by making a full slice')
myList=shopList[:]#复制列表
del myList[1]
print('shopList is',shopList)
print('myList is ',myList)
字符串简单操作
name='hello,world'
if name.startswith('hello'):#测试字符串是否以给定字符串开始
print('Yes')
if 'l' in name:#检验一个给定字符串是否为另一个字符串的一部分
print('OK')
if name.find('world'):#找出给定字符串在另一个字符串中的位置,找不到子字符串返回-1
print('contain')
delimiter='-*_'
myList=['HEL','bed','vie','col']
print(delimiter.join(myList))#用delimiter作为分隔符,将列表中的项目连接起来,生成一个大字符串