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

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 optwo

opone.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 sqrt

for 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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值