python基础学习之循环,列表
来源:互联网 发布:linux交叉编译环境 编辑:程序博客网 时间:2024/05/22 06:56
字符串换行的三种表示:
#coding=utf-8
if__name__ == '__main__':
#第一种:三个单引号
print '''我是一个程序员
我刚开始学习python'''
#第二种:三个双引号
print"""我是一个程序员
我刚开始学习python"""
#第三种:\结尾
print"我是一个程序员,\
我刚开始学python"
str = ‘………’ 要转义字符
str=”……”不需要’的转义字符
原始字符串: str = r’….’最后不能有一个\
非要打印\就这样写:>>>str =r'C:\Program Files\FishC\Good''\\'
else if 缩写为elif
python可以有效避免“悬挂else” ,因为缩进,很明了
三元操作符: small = x if x<y else y
断言(assert)assert (条件) 确保条件为真
python循环:
while循环
for循环:
for目标 in 数组:
循环体
例子:
favor = ‘chenduyu’
for i in favor:
print(i,end = ‘ ‘)
member= [‘aaa’,’sdff’,’a’]
for each in member:
print (each,len(each))
range():是个BIF
>>>range(5)
range(0,5) #说明range默认开始为0
>>>list(range(5))
[0,1, 2, 3, 4] #到5之前 不包含5
>>>list(range(2,5))
[2, 3, 4]
>>> for i inrange(3,7) #注意后面有冒号
SyntaxError: invalid syntax
>>> for i inrange(3,7):
print(i)
3
4
5
6
>>>list(range(4,-2,-1))
[4, 3, 2, 1, 0, -1]
>>>list(range(4,1,1))
[] #空
break和continue语句:
break:调出本层循环
continue:结束本次循环,调到循环开始的地方后开始下一轮循环
列表
python没有数组,有更强大的列表,可以包含:
int float string ....
数组只能存放一种类型,而列表可以存放各种类型的数据,甚至可以存放子列表
1. 创建普通列表
>>> member = ['andy','cindy','amy']
>>> member
['andy', 'cindy', 'amy']
>>> print(member)
['andy', 'cindy', 'amy']
>>> mem1= [1,2,3,2]
>>> mem1
[1, 2, 3, 2]
2.创建混合列表
>> mix =[22,'cindy',[12,56,78]]
>>> mix
[22, 'cindy', [12, 56, 78]]
3.创建空列表
>>> empty=[]
>>> empty
[]
4.向列表里添加元素
追加到列表末尾
append方法
>>> member.append('cdy')
>>> member
['andy', 'cindy', 'amy', 'cdy']
append() takes exactly one argument
只能添加一个元素,那添加两个用extend
extend也只有一个参数,参数类型为列表
>>> empty.extend([12,34,'sss'])
>>> empty
[12, 34, 'sss']
5.插入列表,append和extend追加在末尾
insert方法
>>> member.insert(2,'lyf')
>>> member
['andy', 'cindy', 'lyf', 'amy', 'cdy']
6.从表中获取元素
>>> member
['andy', 'cindy', 'lyf', 'amy', 'cdy']
>>> member[0]
'andy'
怎么调换顺序?
>>> member
['andy', 'cindy', 'lyf', 'amy', 'cdy']
>>> temp = member[0]
>>> member[0]=member[1]
>>> member[1]=temp
>>> member
['cindy', 'andy', 'lyf', 'amy', 'cdy']
7.从列表中删除元素
remove属于列表对象的方法
必须知道参数值
>>> member
['cindy', 'andy', 'lyf', 'amy', 'cdy']
>>> member.remove('cdy')
>>> member
['cindy', 'andy', 'lyf', 'amy']
del 是一个语句,不是函数
可以删除整个列表
可以删除一个元素
>>> member
['andy', 'cindy', 'lyf', 'amy', 'cdy']
>>> del member[0]
>>> member
['cindy', 'lyf', 'amy', 'cdy']
>>> del member
>>> member
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
member
NameError: name 'member' is not defined
pop方法,可以返回删除的值:
>>> member
['cindy', 'lyf', 'amy', 'cdy']
>>> member.pop()
'cdy'
>>> member
['cindy', 'lyf', 'amy']
加上参数:
>>> member
['cindy', 'lyf', 'amy']
>>> member.pop(1)
'lyf'
>>> member
['cindy', 'amy']
8.列表分片
>>> member
['andy', 'cindy', 'lyf', 'amy', 'cdy']
>>> member[1:4]
['cindy', 'lyf', 'amy']
>>> member
['andy', 'cindy', 'lyf', 'amy', 'cdy']
只是得到了原列表的拷贝,原列表没有发生变化,只是切片在member[4]之前,不包含member[4]
>>> member[1:]
['cindy', 'lyf', 'amy', 'cdy']
>>> member[:2]
['andy', 'cindy']
>>> member[:]
['andy', 'cindy', 'lyf', 'amy', 'cdy']
利用member[:]可以得到列表的拷贝但是不会更改原来的列表
list1=list0
list3=list0[:]
如果改变了list0
则list3不会改变
而list1会跟着list0改变
相当于list3是拷贝了list0的值
直接用等号,只是加了一个指向list0的标签
要拷贝列表一定要用list[:]来分片
9.列表中的操作符
>>> list1 = [123,234]
>>> list2 = [234,234]
>>> list1<list2
True
比较第一个元素的大小
>>> list3 = [123,234]
>>> list1 == list3
True
>>> list4 = list1 + list2
>>> list4
[123, 234, 234, 234]
拼接元素
>>> list1 + 'aaa'
Traceback (most recent call last):
File "<pyshell#43>", line 1, in <module>
list1 + 'aaa'
TypeError: can only concatenate list (not"str") to list
不能用+追加元素可用append
>>> list3
[123, 234]
>>> list3*3
[123, 234, 123, 234, 123, 234]
>>> list3
[123, 234]
没有改变list3
>>> list3*=3
>>> list3
[123, 234, 123, 234, 123, 234]
>>> 123 in list3
True
>>> 1 not in list3
True
用in /not in来判断是否在列表中
>>> list5 = [123,['a','b'],456]
>>> 'a' in list5
False
>>> 'a' in list5[1]
True
嵌套列表
>>> list5[1][1]
'b'
访问嵌套列表的元素
count方法
>>> list3.count
<built-in method count of list object at0x0000000003144788>
>>> list3.count(123)
3
#有3个123
index方法
>>> list3.index(123)
0
#第一个123所在的位置
>>> list3.index(123,1,4)
2
#从1~4范围中第一个123所在的位置
reverse方法
>>> l1 = [1,2,3,4,5,6]
>>> l1.reverse()
>>> l1
[6, 5, 4, 3, 2, 1]
sort用指定的方式对列表中的元素排序,默认是从小到大排序,默认归并排序的
>>> list1
[2, 4, 6, 1, 3, 2, 0]
>>> list1.sort()
>>> list1
[0, 1, 2, 2, 3, 4, 6]
从大到小排序
>>> list1.sort(reverse=True)
>>> list1
#默认reverse=false
[6, 4, 3, 2, 2, 1, 0]
指定的方法:
sort(func,key)
- python基础学习之循环,列表
- Python零基础入门之三---(for循环+列表)
- Python 基础学习 列表
- python基础学习--列表
- python基础之列表
- Python学习之循环
- python学习之列表
- Python新手学习基础之循环结构练习
- python基础学习之——条件判断和循环
- Python新手学习基础之数据结构-列表1
- Python 基础学习之序列及列表操作小结
- python基础之循环语句
- 学习Python语言-列表和for循环
- python学习.3循环遍历整个列表
- python学习(5)python基础、循环
- 【Python基础学习篇】Python列表
- Python新手学习基础之循环语句——While循环
- python 学习笔记之(四)while循环处理列表和字典
- JAVA——从小白开始
- 长URL转短连接的简单设计与实现
- Linux下 curl 定时访问多参数url的问题
- Power of Matrix UVA
- 洛谷 P3092 [USACO13NOV]没有找零No Change
- python基础学习之循环,列表
- Java注解(2)-自定义注解、注解处理器
- 九度OJ题目1153:括号匹配问题
- 获得容器的绝对路径
- python日志管理
- iOS UISearchBar使用全解 搜索条使用大全 swift3
- 使用LitePal操作数据库
- Java常用类
- webpack.config.js配置文件