1、再谈print语句

print('name:', 'tom') # 将会在name和tom中间插入一个空格
print('name',',','tome') # 在name和tom之间添加逗号
print('name'+','+'tom') # 可以去掉中间隔的空格
print('i', 'hava', 'a', 'dream', sep='_') # 将默认的间隔设置为下划线
print('i hava a dream', end=' ') # 设置结束符,默认为换行
print('i hava a dream')输出结果为:
name: tom
name , tome
name,tom
i_hava_a_dream
i hava a dream i hava a dream

2、再谈import

导入模块的方法有

from xxx import xxx
import xxx

当导入的两个模块包含同一个函数的时候,可以带模块名调用或者使用别名的方式导入

from module1 import open as opone
from module2 import open as optwoopone.open()
optwo.open()

3、再谈赋值

①、序列解包

# 测试赋值# 序列解包
x, y, z = 1, 2, 3
print("序列解包x y z =", x, y, z)x, y = y, x
print("序列解包x y z =", x, y, z)
values = 4, 5, 6
print("values =", values)
x, y, z = values
print("序列解包x =", x)tup1 = dict(name='tome', age=20)
# 返回的参数个数应该与接收的变量个数一致
key, value = tup1.popitem()
print("key =", key, ", value =", value)
# 可以使用*来接收多个参数
list1 = [1, 2, 3, 4, 5]
a, *b, c = list1
print(a, b, c)
*a, b, c = list1
print(a, b, c)运行结果
序列解包x y z = 1 2 3
序列解包x y z = 2 1 3
values = (4, 5, 6)
序列解包x = 4
key = age , value = 20

②、链式赋值与增强赋值

# 链式赋值
a = b = 1
print("链式赋值:", a, b)# 增强赋值 += *=这类赋值称为增强赋值

4、条件语句、循环语句

在python中允许链式比较:0< a <10相当于0 < a and 10 > a。

# 测试while for
a = 0
while a < 10:print(a, end=" ")a += 1
print()
dict1 = {"name": "tom", "age": "39", "sex": "male"}
# 使用for可以进行序列解包
for key, value in dict1.items():print(key, value)
# 测试break和continue
from math import sqrtfor n in range(99, 0, -1):root = sqrt(n)if root == int(root):print(n)break# 在循环中使用else,当for没有执行break时才会执行else
for n in range(99, 81, -1):root = sqrt(n)if root == int(root):print(n)break
else:print("没有找到")执行结果
81
没有找到

5、迭代工具

①、并行迭代

# ①并行迭代
print()
print("使用zip进行并行迭代:", end="")
names = ["tom", "james", "tony", "jack"]
ages = [12, 43, 42, 14]
for key, value in zip(names, ages):print(key, value, end=" ")# ①zip并行迭代,当其中一个列表“用完”后,停止对两个列表的“缝合”
print()
print("使用zip进行并行迭代:", end="")
names = ["tom", "james", "tony", "jack"]
ages = [12, 43, 42, 14, 17, 18]
for key, value in zip(names, ages):print(key, value, end=" ")输出结果
简单的迭代:tom 12 james 43 tony 42 jack 14
使用zip进行并行迭代:tom 12 james 43 tony 42 jack 14
使用zip进行并行迭代:tom 12 james 43 tony 42 jack 14 

②、迭代时需要记录索引位置

print()
# 迭代时获取索引,使用index的方式
strings = ['hello', 'local', 'location', 'lol', 'tom']
print(strings)
index = 0
for string in strings:if "lo" in string:strings[index] = "None"index += 1
print("使用index的方式", strings)print()
# 迭代时获取索引,使用enumerate的方式
strings = ['hello', 'local', 'location', 'lol', 'tom']
print(strings)
for index, string in enumerate(strings):if "lo" in string:strings[index] = "None"
print("使用enumerate的方式", strings)输出结果
['hello', 'local', 'location', 'lol', 'tom']
使用index的方式 ['None', 'None', 'None', 'None', 'tom']['hello', 'local', 'location', 'lol', 'tom']
使用enumerate的方式 ['None', 'None', 'None', 'None', 'tom']

③、排序与反向迭代

# 使用sorted函数与reversed函数进行排序与反向迭代
print("使用sorted进行迭代", sorted("hello world"))
print("使用reversed进行迭代", list(reversed("hello world")))输出结果为
使用sorted进行迭代 [' ', 'd', 'e', 'h', 'l', 'l', 'l', 'o', 'o', 'r', 'w']
使用reversed进行迭代 ['d', 'l', 'r', 'o', 'w', ' ', 'o', 'l', 'l', 'e', 'h']

6、列表推导

其相当于创建列表的一种方式

# 测试简单推导# 列表推导
# 计算0~10每个数的平方
list1 = [x * x for x in range(10)]
print(list1)
# 计算100以内能够被3整除的数
list2 = [x for x in range(100, 0, -1) if x % 3 == 0]
print(list2)运行结果
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
[99, 96, 93, 90, 87, 84, 81, 78, 75, 72, 69, 66, 63, 60, 57, 54, 51, 48, 45, 42, 39, 36, 33, 30, 27, 24, 21, 18, 15, 12, 9, 6, 3]

但是不能使用()来创建元组

7、pass、del、exec、eval

# 测试pass del
x = 0
if x < 10:# pass 表示什么都不做pass
else:print("x >= 10")x = ["hello", "world", "python", "java", "python"]
y = x
print(x)
print(y)
# 使用del只能删除x变量,不能删除其变量的值,但如果其值没有被引用,python会自动将其回收
del x
print(y)
# 会报错
# print(x)输出结果
['hello', 'world', 'python', 'java', 'python']
['hello', 'world', 'python', 'java', 'python']
['hello', 'world', 'python', 'java', 'python']from math import sqrt
# exec用于执行python语句
exec("print('hello world')")
exec("print(sqrt(4))")
exec("sqrt=1")
# 下面会报错,因为修改了sqrt的功能,解决办法是为exec指定命名空间
# exec("print(sqrt(4))")输出结果
hello world
2.0# 使用命名空间解决exec的问题
scope = {}
exec("sqrt=1", scope)
exec("print(sqrt(4))")
print(scope['sqrt'])
输出结果
2.0
1# eval用于字符串计算,也可以指定命名空间,即所有的操作都仅限于该命名空间
scope = {'x': 1, 'y': 2}
print(eval('x * y', scope))
输出结果
2

python笔记-05(条件、循环及其他语句)相关推荐

  1. 【python笔记】 for循环和while循环,break和continue语句

    目录 循环结构: while语句: 可迭代对象: break语句: continue语句: 循环结构中的else子句: 特殊循环---列表解析 循环结构: 循环结构是满足一个指定的条件,每次使用不同的 ...

  2. 【20212121】Python基础 05条件控制语句

    05 条件控制语句 条件判断语句(if语句) 单分支结构: # Σ(っ °Д °;)っ if可以没有else if <条件>:<语句块> *<条件>是一个或多个条件 ...

  3. python笔记05: 程序结构

    程序结构 三大结构 顺序 分支 循环 分支 单路分支 分支的基本语法 if 条件表达式:语句1语句2语句3...... 条件表达式就是计算结果必须为布尔值的表达式 表达式后面的冒号不能少 注意if后面 ...

  4. python笔记之while循环

    While 循环 特点:提高代码的复用性 语法: while 判断条件: 条件满足时执行代码块 实例: count = 0 while count<10:print(count,end=&quo ...

  5. 笨方法“学习python笔记之条件控制

    python支持if条件控制,其主要有以下几种形式: 1: if 条件语句,条件判断语句后面紧跟冒号:具体格式如下: if condition:statement_block 使用范例: people ...

  6. Python入门5_条件循环语句

    1 , 赋值操作: >>> x,y,z = 1,2,3 #等同于x = 1,y = 2, z = 3 >>> x,y = y,x #交换x,y的值 >> ...

  7. python基础之条件循环语句

    前两篇说的是数据类型和数据运算,本篇来讲讲条件语句和循环语句. 0x00. 条件语句 条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语 ...

  8. python笔记之for循环

    for循环 语法: for 临时变量 in 可迭代的对象: 满足条件所执行的代码块 实例1 打印1-10的奇数 , 默认步长为1,下面是将步长设置为2,所以打印结果为奇数. for i in rang ...

  9. 【python笔记】选择结构:if语句详解

    目录 if语句 表达式: 语句序列: if语句的执行流程: else子句: else 子句执行流程 三元运算符: elif语句: elif执行流程 嵌套的if语句: 例:符合函数: if语句 表达式: ...

最新文章

  1. 高并发系统数据库设计
  2. 算法总结之 一行代码求两个数的最大公约数
  3. SpringCloud工作笔记089---SpringBoot中Mybatis使用Condition_Criteria如何筛选日期类型数据
  4. UVa 10935 - Throwing cards away I
  5. 走进JavaScript
  6. 【渝粤题库】陕西师范大学600000 仪器分析 作业(专升本)
  7. paip.输入法编程---增加码表类型
  8. Java链表的常用算法原理
  9. 瑞斯康达olt排查故障的常用命令
  10. 诗词教育不过是老虎嘴上的胡子
  11. Cisco 实现路由防火墙 双机热备(项目记录)
  12. 视频:青春期(青春痘1)
  13. 利用Photoshop对证件照换底且抠出头发丝
  14. TXSQL:云计算时代数据库核弹头——云+未来峰会开发者专场回顾 1
  15. 会动的博物馆?广州华锐互动3D展示技术实现空间复刻
  16. 微信小程序模拟车位选择功能(简陋版本)
  17. cfa的pv怎么用计算机算,【干货】CFA考试计算器最佳设置和计算技巧
  18. 操作体验极度舒适的多功能软件卸载工具 - iObit Uninstaller PRO
  19. SaaS服务:虽霸主未成,但不乏强者
  20. 小学计算机台账模板,小学实验室各类台帐样本.doc

热门文章

  1. 开关量模块在报警系统中的应用
  2. 红绿灯代码 摘抄抖音 渡一前端的
  3. 比心app源码,携带对象参数跳转页面
  4. Leetcode 1647. Minimum Deletions to Make Character Frequencies Unique
  5. SMTP命令 发送邮件 DOS命令
  6. 计算机上的ip地址在哪查,电脑的ip地址在哪里查
  7. 一款RS485电表的调试与上位机通讯过程
  8. python中图片转base64,再转html保存方法
  9. 华为P40系列手机camera特性分析
  10. windows密钥查看器ProduKey1.82汉化