码迷,mamicode.com
首页 > 编程语言 > 详细

python基础二

时间:2016-07-11 00:53:13      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

主要是列表
 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 #字符串的格式化输出
 4 name = "han"
 5 print("i am %s"%name)
 6 
 7 #列表
 8 namee = ["a","b","c",345,name,"love"]
 9 print(namee)
10 print (namee[1])
11 
12 #切片
13 print(namee[:3])
14 print(namee[-3:])
15 print(namee[-3:][-1][0])
16 
17 #修改一个字符串
18 namee[5] = "hani"#把love改成hani
19 print(namee)
20 
21 #插入一个字符,在第一个位置
22 namee.insert(1,"woshi")
23 print(namee)
24 #在结尾追加一个字符串
25 namee.append("wahaha")
26 print(namee)
27 
28 #移除一个字符串
29 namee.remove("a")
30 print(namee)
31 #------------------------------------------------------------
32 nam = ["alex","jack","peter","aaron","fiona","kai"]
33 nam.insert(-1,"abc")
34 print(nam)
35 #步长的概念
36 print("----------------",nam[::2])
37 
38 #del删除内存中的数据
39 del nam[4:6] #连续删除两个字符串
40 print(nam)
41 
42 nam2 = nam[2:6]
43 print(nam2)
44 
45 num = ["alex","jack","peter","aaron","fiona","kai",3,34,45,56,76,87,78,45,3,3,3,3,3,3,44,5,55,65,565767,87686,98,76]
46 #print(78 in num)
47 if 3 in num:
48     print("3 in num")#如果存在3打印3in num
49 
50     num_of_ele = num.count(3)#统计存在多少个3
51     pos_of_ele = num.index(3)#查看3的坐标
52 
53     num[pos_of_ele] = 67
54     print(num)
55     print("[%s] 3 are in num,pos[%s]"%(num_of_ele,pos_of_ele))
56 print("===============================")
57 for i in range(num.count(3)):#循环3出现的次数
58     ele_index = num.index(3) #取出3的坐标
59     num[ele_index]="3a"  #替换所有的3为3a
60 print(num)

 

python基础二

标签:

原文地址:http://www.cnblogs.com/hanxiaobei/p/5658976.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!