当前位置:网站首页>(2) Sequence structures, Boolean values of objects, selection structures, loop structures, lists, dictionaries, tuples, sets
(2) Sequence structures, Boolean values of objects, selection structures, loop structures, lists, dictionaries, tuples, sets
2022-08-02 04:00:00 【stealth rookie】
目录
顺序结构
程序从上到下顺序地执行代码,中间没有任何的判断和跳转,直到程序结束
对象的布尔值
python一切皆为对象,所有对象都有一个布尔值
获取对象的布尔值,需使用内置函数bool()
以下对象的布尔值为false:
false;数值();none;空字符串;空列表;空元组;空字典;空集合;
print(bool(False)) #False
print(bool(0)) #False
print(bool(0,0)) #False
print(bool(None)) #False
print(bool('')) #False
print(bool("")) #False
print(bool([])) #空列表
print(bool(list())) #空列表
print(bool(())) #空元组
print(bool(tuple())) #空元组
print(bool({})) #空字典
print(bool(dict())) #空字典
选择结构
单分支结构:
if 条件表达式 :
条件执行体
双分支结构:
if 条件表达式 :
条件执行体1
else:
条件执行体2
多分支结构:
if 条件表达式1 :
条件执行体1
elif 条件表达式2 :
条件执行体2
elif 条件表达式N :
条件执行体N
[else:]
条件执行体N+1
嵌套if:
if 条件表达式1 :
if 内层条件表达式 ;
内层条件执行体1
else:
内层条件执行体2
else:
条件执行体
pass语句:
语句什么也不做,只是一个占位符,用在语法上需要语句的地方
(Used when you don't know what code to write,so that the statement does not report an error)
在哪些地方用:if的条件执行体;for -in的循环体;定义函数时的函数体
循环结构
range()函数:
用于生成一个整数序列
优点:不管range对象表示的整数序列有多长,所有range对象占用的内存空间都是相同的,因为仅仅需要存储start、stop、step,只有当用到range对象时,才会去计算序列中的相关元素.
while循环:
while 条件表达式:
条件执行体(循环体)
#选择结构if与循环结构while的区别:
if是判断一次,条件为ture执行一次
while是判断n+1次,条件为ture执行n次
for-in循环:
in,表达从(字符串、序列等)中依次取值,又称遍历
for-in遍历的对象必须是可迭代对象
for 自定义的变量 in 可迭代对象:
循环体
#循环体内不需要访问自定义变量.可以将自定义变量替代为下划线
for _ in range(10)
print('python')
break、continue与else语句:
嵌套循环:
外层循环执行一次,The inner loop executes a full round
列表
变量可以存储一个元素,而列表是一个大容器,可以存储N多个元素,The program can facilitate the overall operation of these data.(Lists are equivalent to arrays in other languages)
列表特点:
创建列表:
#第一种方法,创建列表,使用[]
lst=['hello','world',123]
#第一种方法,创建列表,使用内置函数list()
lst=list(['hello','world',123])
列表查询:
列表元素增加:
列表元素删除:
列表元素修改:
1.为指定索引的元素赋予一个新值 lst[2]=123
2.为指定的切片赋予一个新值 lst[1:3]=[12,13,14,15]
列表元素排序:
1.方法sort()
调用sort(),All elements in the list are in ascending order by default,可以指定reverseSort descending ascending
#通过指定关键字参数,将列表中的元素进行排序
lst.sort() #默认升序
lst.sort(reverse=Ture) #reverse=Ture 表示降序排序
lst.sort(reverse=False) #reverse=False 表示升序排序
2.内置函数sorted()
调用内置函数sorted(),可以指定reverse=Ture,进行降序排序,原列表不发生改变
new_list=sorted(lst) #默认为升序
desc_list=sorted(lst,reverse=Ture) #降序排列
列表生成式:
生成列表的公式
字典
以键值对的方式存储数据,字典是一个无序的序列(Like lists, they are mutable sequences)
字典的实现原理与查字典类似,python中字典是根据key(hash)查找value所在位置
字典特点:
•字典中的所有元素都是一个 key-value对,key不允许重复, value可以重复
•字典中的元素是无序的
•字典中的key必须是不可变对象
•字典也可以根据需要动态地伸缩
•字典会浪费较大的内存,是一种使用空间换时间的数据结构
创建字典:
#第一种,使用花括号{}
scores={'小明':98,'小米':87,'小白':86}
#第二种,使用内置函数dict()
dict(name='alan',age=20)
获取字典元素:
字典常用操作:
字典生成式:
内置函数zip():
用于将可迭代的对象作为参数,Packs the corresponding elements in the object into a tuple,然后返回由这些元组组成的列表
name=['小明','小米','小白']
age=[16,18,20]
lst=zip(name,age)
print(list(lst))
元组
python内置的数据结构之一,是一个不可变序列
不可变序列与可变序列:
不可变序列:字符串、元组 (没有增、删、改的操作)
可变序列:列表、字典 (可以对序列执行增删改操作,对象地址不发生更改)
元组常用操作:
注意事项:元组中存储的是对象的引用
1.如果元组中的对象本身是不可变对象,则不能再引用其他对象
2.If the objects in the tuple are mutable objects,则可变对象的引用不允许改变,但数据可以改变
集合
python语言提供的内置数据结构,与列表、字典一样都属于可变类型的序列
集合是没有value的字典
集合常用操作:
集合间的关系:
1.两个集合是否相等
使用运算符==或!=判断 s1!=s2
2.一个集合是否是另一个集合的子集
调用issubset进行判断 s1.issubset(s2)
3.一个集合是否是另一个集合的超集
调用issuperset进行判断 s1.issuperset(s2)
4.两个集合是否没有交集
调用isdisjoint进行判断 s1.isdisjoint(s2)
列表、字典、元组、集合总结
边栏推荐
- CTF入门之md5
- [league/flysystem]一个优雅且支持度非常高的文件操作接口
- What are the killer super powerful frameworks or libraries or applications for PHP?
- (2) 顺序结构、对象的布尔值、选择结构、循环结构、列表、字典、元组、集合
- Scrapy爬虫遇见重定向301/302问题解决方法
- 关于tp的apache 的.htaccess文件
- Alibaba Cloud MySQL 5.7 installation and some major problems (total)
- 批量替换文件字体,简体->繁体
- [campo/random-user-agent] Randomly fake your User-Agent
- SQL: DDL, DML, DQL, DCL corresponding introduction and demonstration
猜你喜欢
随机推荐
13. JS output content and syntax
(8) requests, os, sys, re, _thread
(1)Thinkphp6入门、安装视图、模板渲染、变量赋值
(5) Modules and packages, encoding formats, file operations, directory operations
c语言用栈实现计算中缀表达式
The roll call system and array elements find maximum and minimum values for sorting of objects
Several interesting ways to open PHP: from basic to perverted
When PHP initiates Alipay payment, the order information is garbled and solved
PHP反序列化漏洞
(2) 顺序结构、对象的布尔值、选择结构、循环结构、列表、字典、元组、集合
Multithreading (implementing multithreading, thread synchronization, producer and consumer)
CTF之xxe
Xiaoyao multi-open emulator ADB driver connection
16. JS events, string and operator
hackmyvm: kitty walkthrough
Add a full image watermark to an image in PHP
PHP8.2将会有哪些新东西?
Eric target penetration test complete tutorial
GreenOptic: 1 vulnhub walkthrough
Phonebook