找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手
#coding:utf-8
#************字符串常用函数*************************

#.strip() 删除首尾字符空白
#lstrip()  指定删除左边
#rstrip()  指定删除右边
'''
name=raw_input('please print your name:')
print name.strip()    #删除字符串开头和结尾空白字符(\n\t\f)

str='abbbbbab yes no abab'
print str.strip('ab')   #yes no    首尾字符包含a/b的都会被删除

#首字符串从左往右,遇假则断,结尾字符串从右往左,遇假则断
str='abcbbbbab yes no abababba'
print str.strip('ab')    #cbbbbab yes no   (首字符串从c开始断开,结尾字符串都为真,全部删除)

'''

#endswith()  以xx结尾
'''
filename =raw_input('请输入一个文件名:')
if filename.endswith('.txt'):
    print '您输入的是一个txt文件'
else:
   print 'error'
'''



#startswith()  以xx开头
'''
name=raw_input("请输入你的姓名:")
if name.startswith('谢'):
    print '这是我们谢氏家族的成员!'
else:
    print '他是卧底'
'''



#find()   查找指定字符的位置
#rfind()  从右边开始查找
'''
myname='xiexieshitongchenqiumeixieshitongtttt'
index=myname.find('chenqiumei')
print index                              #13    开始的下标位置

index=myname.rfind('tong')               #29

count =myname.count('xieshitong')   
print count                              #  2
'''
#replace  替换      replace(old,new,count)

'''
youname='zhangsanwangwuzhaoliulisizhangsan'
myname=youname.replace('zhangsan','xieshitong')  #count为指定由左往右替换次数,不指定则全部替换
print myname             #xieshitongwangwuzhaoliulisixieshitong
'''

#*********************列表*************************
#列表是数组,是一种数据的集合

#append()尾部追加
'''
list1=[2,3,4,2,8,3,5]
list1.append(11)
print list1   #[2, 3, 4, 2, 8, 3, 5, 11]

##头部加入,可选择insert插入
list1=[2,3,4,2,8,3,5]
list1.insert(0,11)
print list1  #  [11, 2, 3, 4, 2, 8, 3, 5]
'''


#去重
'''
list1=[2,3,4,2,8,3,5]
list2=[]
for i in list1:
    if i not in list2:
        list2.append(i)

print list2   #[2, 3, 4, 8, 5]
'''

#插入  insert()
'''
list1=[2,3,4,2,8,3,5]
list1.insert(2,'88')   #第一个数为下标位置,第一个为要插入的数
print list1     #[2, 3, '88', 4, 2, 8, 3, 5]

q:怎么样在一个位置前插入多个数?
'''

#删除 remove()
#删除  del 删除指定下标值
'''
list1=[2,3,4,2,8,3,5]
# list1.remove(2)
#print list1  #[3, 4, 2, 8, 3, 5]
del list1[2]
print list1   #[2, 3, 2, 8, 3, 5]
'''
#pop()  默认移除最后一个
#pop(index) 默认移除指定下标元素
'''
list1=[2,3,4,2,8,3,5]
# list1.pop()
# print list1  #[2, 3, 4, 2, 8, 3]

list1=[2,3,4,2,8,3,5]
list1.pop(2)   #指定删除
print list1    #[2, 3, 2, 8, 3, 5]
'''

#del、pop、remove的区别:
#1、remove是针对可变列表的元素进行搜索删除,而pop和del是针对可变列表的下标进行搜索删除
#2、pop可以返回删除值,remove和del不返回删除值

#合并  extend()  
'''
list1=[2,3,4,2,8,3,5]
list2=[1,1,1,2,3,3,4,9]
list1.extend(list2)
print list1     #[2, 3, 4, 2, 8, 3, 5, 1, 1, 1, 2, 3, 3, 4, 9]

#list3=list1.extend(list2)
# print list3    #  none
'''


#排序
#sort()
#reverse()  反转
'''
list1=[8,4,3,9,11,23,7]

list1.sort()    默认升序
print list1    #[3, 4, 7, 8, 9, 11, 23]

list1.reverse()   反转
print list1  #[23, 11, 9, 8, 7, 4, 3]  
'''

#元组 tuple()
'''
t=('1','2','3')
tuple(t)
print type(t)  #元组不支持修改
'''

#-------------------字典 (键值对)-----------------------------
'''
count={}
name =raw_input("please print your name:")
password =raw_input("please print your pw:")
if name=='xieshitong' and password=='12345':
    count[name]=password      #存入字典
else:
    print '404'
print count #
print count['xieshitong']
#输出
# please print your name:xieshitong
# please print your pw:12345
# {'xieshitong': '12345'}
'''

#创建字典
# dict2={'age':'18','money':'100000'}

#增
'''
dict2={'age':'18','money':'100000'}
dict2['name']='zhangsan'
print dict2   #{'age':'18','money':'100000','name':'zhangsan'}
#删
#1、pop()
#2、del
'''
# 1、pop
dict2={'age':'18','money':'100000'}
print dict2
# dict2.pop('money')    #指定删除
# print dict2

#2、del
del dict2['age']
print dict2   #指定删除


#del与clear 的区别,clear只是清除内容,留下一个空字典,而del是删除整个字典,原字典将不存在
'''

#update()  利用一个字典更新另一个字典(合并)
'''
dict1={'name':'xieshitong'}
dict2={'age':'18'}
dict1.update(dict2)
print dict1   #{'age': '18', 'name': 'xieshitong'}
'''

#查
'''
dict2={'age':'18','money':'100000'}
print dict2    #{'age':'18','money':'100000'}   直接打印字典
print dict2['age']  # 18   根据键取值
'''
#取值方法的区别 dict[]  、get()
'''
dict2={'age':'18','money':'100000'}
print dict2.get('age1')  #get() 取不到值时返回none
print dict2['age1']     #dict[key]取不到值时报异常
'''

'''
##关于中文字典的那点事(因编码问题无法正常打印,不建议用中文)
dict1={'星期一':'大扫除','星期二':'去逛街','星期三':'好好学习'}
# print dict1['星期一']    #大扫除    取值

dict1 ['星期四']='上课'

print dict1['星期四']   #上课    通过键取值

print str(dict1).decode('string_escape')   #打印,输出的是字符串    {'星期四': '上课', '星期二': '去逛街', '星期三': '好好学习', '星期一': '大扫除'}
'''

#copy  深拷贝与浅拷贝的区别
浅拷贝:拷贝了最外围的对象本身,内部的元素都只是拷贝了一个引用而已。也就是,把对象复制一遍,但是该对象中引用的其他对象我不复制
深拷贝:外围和内部元素都进行了拷贝对象本身,而不是引用。也就是,把对象复制一遍,并且该对象中引用的其他对象我也复制

不可变对象:一旦创建就不可修改的对象,包括字符串、元组、数字
可变对象:可以修改的对象,包括列表、字典。

对于不可变对象,不管是深拷贝还是浅拷贝,地址值和拷贝后的值都是一样的
对于可变对象:
=浅拷贝: 值相等,地址相等
copy浅拷贝:值相等,地址不相等
deepcopy深拷贝:值相等,地址不相等

'''
import  copy
#可变对象
a=[1,2,3]
b=copy.copy(a)
c=copy.deepcopy(a)
print id(a)   #40385032   
print id(b)   #40410824   浅拷贝,值相同,地址不同
print id(c)   #40384008   深拷贝,值相同,地址不同

#不可变对象
A=(1,2,3)
B=copy.copy(A)
C=copy.deepcopy(A)
print id(A)  #40397488
print id(B)  #40397488   浅拷贝,值相同,地址相同
print id(C)  #40397488   浅拷贝,值相同,地址相同
'''


#遍历字典
'''
dict1={'zhangsan':'18','lisi':'21','wangwu':'50'}

for i in dict1.keys():
    print i    #返回所有的键

for i in dict1.values():
    print i     #返回所有的值

for i in dict1.items():
    print i  #以元组的形式返回所有的键值对

for i in dict1:
    print 'the name is %s,the age is %s'%(i,dict1)
'''

#--------------集合---------------
集合是无序的,元素不可重复的
可变集合:set
不可变集合:frozenset
'''
list1=[1,3,3,3,4,5]
list1=set(list1)  #将列表转成集合
print (list1,type(list1))    #(set([1, 3, 4, 5]), <type 'set'>)

#增方法一 add()
list1.add(100)
print list1       #set([1, 3, 4, 5, 100])

#增方法二 update()
list1.update(['101','102'])
print list1        #set([1, 3, 4, 5, '102', 100, '101'])

#删除方法一
list1.pop()
print list1        #set([3, 4, 5, '102', 100, '101'])   默认删除了首元素

#删除方法二
list1.discard(100)
print list1         #set([3, 4, 5, '102', '101'])  指定删除元素


list2=[1,3,2,213,12,23]
list2=set(list2)
print list2

#取交集
list3= list1.intersection(list2)
print (list3,type(list3))   #(set([1, 3]), <type 'set'>)
#取并集
list4=list1.union(list2)
print list4  #  set([1, 2, 3, 4, 5, 12, 213, 23])
'''


#------------------------------------循环----------------------------
# if elif else
'''
money =input('请输入你的财富(万元):')
if money>=100000:
    print '富甲一方'
elif money>=10000:
    print "亿万富翁"
elif money>=1000:
    print "千万富翁"
elif money>=100:
    print "百万富翁"
else:
    print '继续搬砖!'
'''


# 三目运算(等价于if else)
'''
name=raw_input('请输入你的名字:')
result ='你被录用了' if name=='xiaoming' else '你回等通知吧'
print result

'''
#while循环
'''
i=0
sum=0

while  i<=100:
    sum+=i
    i+=1
print sum

'''


#for 循环
'''
sum=0
for i in range(101):
    sum+=i
print sum
'''
--------------------练习----------------------
# 九九乘法表
'''
for i in range(1,10):
    for j in range(1,i+1):
        print j,'x',i,'=',j*i,'\t',
    print '\n'
   
   
    list1=['1','11','21']
for l in list1:
    print l,    #如果要打印在同一行,要加,

'''
#取100以内的偶数之和
'''
i =0
sum=0
for i in range(101):
    if i%2==0:
        sum+=i
        i+=1
    else:
        pass
print sum

'''
#2000年-2020年的闰年
#条件:年份可以整除4,但不能整除100,或者可以整除400
'''
year =0
for year in range(2000,2020):
    if (year%4==0   and year %100 !=0)or year %400==0 :
        print year,
'''























































分享至 : QQ空间
收藏

0 个回复

您需要登录后才可以回帖 登录 | 立即注册