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

python基础2

时间:2018-07-08 12:37:04      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:pytho   print   元组   嘻嘻   字母   clear   center   只读   变量   

1.列表元组

列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储、修改等操作.

定义列表:

1 # yu
2 
3 names=["啦啦","嘿嘿",鱼鱼]

我们通过列表下标进行数据的提取:

1 # yu
2 names=["啦啦","嘿嘿",鱼鱼]
3 print(names)
4 print(names[0])#第一个数据从0开始
5 print(names[-1])#倒过来取值

切片:

技术分享图片
1 # yu
2 names=["lala","heihei",yuyu,hehe,haha]
3 print(names[1:3])#取下标1 ,不包含下标3 [‘heihei‘, ‘yuyu‘]
4 print(names[1:-1])#取下标1至-1的值,不包括-1
5 print(names[:3])#如果是从头开始取,0可以忽略
6 print(names[3:])#取到最后一个值
7 print(names[3:-1])#取不到最后一个值
8 print(names[0::2])#后面的2是代表,每隔一个元素,就取一个
View Code

追加:

技术分享图片
1 # yu
2 names=["lala","heihei",yuyu,hehe,haha]
3 names.append("嘻嘻")
4 print(names)
5 #结果[‘lala‘, ‘heihei‘, ‘yuyu‘, ‘hehe‘, ‘haha‘, ‘嘻嘻‘]
View Code

插入:

技术分享图片
1 # yu
2 names=["lala","heihei",yuyu,hehe,haha]
3 names.insert(2,从2插入)
4 print(names)
5 #结果[‘lala‘, ‘heihei‘, ‘从2插入‘, ‘yuyu‘, ‘hehe‘, ‘haha‘]
View Code

修改:

技术分享图片
1 # yu
2 names=["lala","heihei",yuyu,hehe,haha]
3 names[1]=修改下标为1
4 print(names)
5 #结果[‘lala‘, ‘修改下标为1‘, ‘从2插入‘, ‘yuyu‘, ‘hehe‘, ‘haha‘]
View Code

删除:

技术分享图片
1 names=["lala","heihei",yuyu,hehe,haha]
2 #names.clear()#清空列表
3 print(names)#返回[]
4 #del names[1]#删除指定下标的值
5 print(names)#[‘lala‘, ‘yuyu‘, ‘hehe‘, ‘haha‘
6 #names.remove("yuyu")#删除指定的值
7 print(names)
8 names.pop()#默认删最后一个,也可以加入你祥删除的下标
9 print(names)
View Code

扩展:

技术分享图片
1 # yu
2 names=["lala","heihei",yuyu,hehe,haha]
3 b=[1,2,3]
4 names.extend(b)
5 print(names)#[‘lala‘, ‘heihei‘, ‘yuyu‘, ‘hehe‘, ‘haha‘, 1, 2, 3]
View Code

拷贝(浅拷贝与深拷贝的区别):

技术分享图片
1 names=["Young","Jon",["Tom","Jerry"],Jems,Sunny]
2 names2 = names.copy()
3 names[1]="浅copy"
4 names[2][0]="深copy"
5 print(name1:,names)
6 print(name2:,names2)
7 #结果
8 #name1: [‘Young‘, ‘浅copy‘, [‘深copy‘, ‘Jerry‘], ‘Jems‘, ‘Sunny‘]
9 #name2: [‘Young‘, ‘Jon‘, [‘深copy‘, ‘Jerry‘], ‘Jems‘, ‘Sunny‘]
View Code

统计:

技术分享图片
1 # yu
2 names=["lala","heihei",yuyu,hehe,haha,haha]
3 print(names.count(haha))#结果2
View Code

排序:

技术分享图片
1 # yu
2 names=["1lala","3heihei",5yuyu,4hehe,2haha,1haha]
3 names.sort()
4 print(names)
View Code

反转:

技术分享图片
1 names=["1lala","2heihei",3yuyu,4hehe,5haha,6haha]
2 names.reverse()
3 print(names)
4 #结果[‘6haha‘, ‘5haha‘, ‘4hehe‘, ‘3yuyu‘, ‘2heihei‘, ‘1lala‘]
View Code

获取下标:

技术分享图片
1 # yu
2 names=["0lala","1heihei",2yuyu,3hehe,4haha,5haha]
3 print(names.index("2yuyu"))#2
View Code

元组

元组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表

定义:

names=("lala","heihei",yuyu,hehe,haha,haha)

它只有2个方法,一个是count,一个是index.

 

2.字符串操作

特性;不可修改.

# yu
name=young
 #首字母大写
print(name.capitalize() )
# name.casefold()   大写全部变小写
print(name.center(50,"-") ) #输出----------------------young-----------------------
# name.count(‘lex‘) 统计 lex出现次数
# name.encode()  将字符串编码成bytes格式
# name.endswith("g")  判断字符串是否以g结尾
#  "Young\tY".expandtabs(10) 输出‘Young      Y‘, 将\t转换成多长的空格
print(name.find(o))  #查找A,找到返回其索引, 找不到返回-1
技术分享图片
 1 #format
 2 cata = "my name is {}, and age is {}"
 3 print(cata.format("young",23))#结果my name is young, and age is 23
 4 cata = "my name is {}, and age is {}"
 5 print(cata.format("23",young))#my name is 23, and age is young
 6 cata = "my name is {name}, and age is {age}"
 7 print(cata.format(age=23,name=young))
 8 
 9 #format_map
10 cata = "my name is {name}, and age is {age}"
11 print(cata.format_map({name:young,age:23}))
View Code
技术分享图片
cata = "my name is {name}, and age is {age}"
print(cata.index(a))
print(9aA.isalnum() )
print(9.isdigit() )#是否整数)
name=Hello
print(name.isnumeric  )
# name.isprintable
# name.isspace
# name.istitle
# name.isupper
print("|".join([lala,hihi,wow]))
View Code
技术分享图片
#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)
print(trantab)
str = "this is string example....wow!!!"
print(str.translate(trantab))
#     ‘th3s 3s str3ng 2x1mpl2....w4w!!!‘
View Code
技术分享图片
1 print(cata.partition(is))#(‘my name ‘, ‘is‘, ‘ {name}, and age is {age}‘)
2 print(cata.swapcase())#大小写互换
3 print(cata.zfill(40))
4 n=hello world
5 print(n.ljust(40, "-"))
6 print(n.rjust(40, "-"))
7 b="ddefdsdff_哈哈" 
8 b.isidentifier() #检测一段字符串可否被当作标志符,即是否符合变量命名规则
View Code

 

python基础2

标签:pytho   print   元组   嘻嘻   字母   clear   center   只读   变量   

原文地址:https://www.cnblogs.com/Young111/p/9279591.html

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