Python的条件控制
is_hot = False
is_cold = False
if is_hot:
print("It's a hot day")
print("Drink plenty of water")
elif is_cold:
print("It's a cold day")
print("Wear warm clothes")
else:
print("It's a lovely day")
print("Enjoy your day")
if - elif - else 结构
练习
购买一所房子100万,信用好交10%定金,信用差交20%定金
price = 1000000
has_good_credit = True
if has_good_credit:
down_payment = price * 0.1
else:
down_payment = price * 0.2
print(f"Down payment: ${down_payment}")
逻辑运算符 and or not
a = 1
b = 1
c = 0
if a and b:
print("a 和 b 都为1")
if a or c:
print("a和c中至少有一个1")
if not c:
print("c为0")
补充 :忘了比较运算符
https://www.runoob.com/python3/python3-basic-operators.html
Python的循环结构
while循环
i = 1
while(i <= 5):
print(i)
i += 1
print("Done")
#break 跳出循环
--------------输出结果----------------------
1
2
3
4
5
while-else
练习:猜数
guess_count = 0
guess_limit = 3
while guess_count < guess_limit:
guess = int(input('Guess: '))
guess_count += 1
if guess == 9:
print("You win")
break
else:
print("I'm sorry,you failed")
---------------------输出结果---------------------
Guess: 1
Guess: 9
You win
-------------------------------------------------
Guess: 1
Guess: 2
Guess: 3
I'm sorry,you failed
for循环
str = "Gpower"
for item in str:
print(item+" ",end="")
print()
for item in [0,1,2,3,4,5,6]:
print(item,end=" ")
print()
for item in range(10):
print(item,end=" ")
print()
for item in range(0,9):
print(item,end=" ")
print()
for item in range(0,9,2):
print(item, end=" ")
---------------------输出结果---------------------------
G p o w e r
0 1 2 3 4 5 6
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8
0 2 4 6 8
补充:print默认换行 print(item,end=" ")修改end参数即可
for循环的好基友 range() 有多种写法上面举例说明了
双重循环
for x in range(3):
for y in range(4):
print(f"(x,y): ({x},{y})")
列表
names = ['zhangsan','lisi','wangwu']
print(names)
我暂且先把它理解为一个数组来看待
names = ['zhangsan','lisi','wangwu','zhaosi','xiaoming']
print(names[0])
names[0] = '张三'
print(names[1])
print(names[2])
print(names[-1])
print("---------------")
print(names[0:])
print(names[:])
print(names[2:])
print(names[2:4])
--------------------------输出结果
zhangsan
lisi
wangwu
xiaoming
---------------
['张三', 'lisi', 'wangwu', 'zhaosi', 'xiaoming']
['张三', 'lisi', 'wangwu', 'zhaosi', 'xiaoming']
['wangwu', 'zhaosi', 'xiaoming']
['wangwu', 'zhaosi']
练习 找出列表中的最大值
numbers = [1,3,15,1,5,6,8,2,0,33]
max = numbers[0]
for item in numbers:
if item > max:
max = item
print(f"max: {max}")
二重列表
matrix = [
[1,2,3],
[4,5,6],
[7,8,9]
]
matrix[0][1] = 20
print(matrix[0][1])
# 双重列表的遍历
for row in matrix:
for item in row:
print(item,end=" ")
print()
-------------------------输出结果
20
1 20 3
4 5 6
7 8 9
睡觉了,熬不住了
列表方法
numbers = [1,3,5,2,4,8,1,1,1]
#在结尾添加一项
numbers.append(14)
print(numbers)
#移除值
numbers.remove(5)
print(numbers)
#在索引位置添加
numbers.insert(0,10)
print(numbers)
#移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
number = numbers.pop()
print(number)
#从列表中找出某个值第一个匹配项的索引位置
number = numbers.index(8)
print(number)
#统计某个元素在列表中出现的次数
print(numbers.count(1))
#排序 升序和降序
numbers.sort()
print(numbers)
numbers.reverse()
print(numbers)
#复制一个列表
numbers2 = numbers.copy()
print(numbers2)
#移除所有项
numbers.clear()
print(numbers)
-----------------------------------------输出结果
[1, 3, 5, 2, 4, 8, 1, 1, 1, 14]
[1, 3, 2, 4, 8, 1, 1, 1, 14]
[10, 1, 3, 2, 4, 8, 1, 1, 1, 14]
14
5
4
[1, 1, 1, 1, 2, 3, 4, 8, 10]
[10, 8, 4, 3, 2, 1, 1, 1, 1]
[10, 8, 4, 3, 2, 1, 1, 1, 1]
[]
列表的更多方法:https://www.runoob.com/python3/python3-list.html
元组
元组与列表类似,但元组不能修改。即不可变
# 元组的定义 用括号() 方法与列表类似
numbers = (1,2,3,1,1,1)
print(numbers.index(3))
print(numbers.count(1))
print(numbers[0])
python 的解压缩特性
# 元组与列表都可以
numbers = [1,2,3]
'''
x,y,z = numbers 相当于
x = numbers[0]
y = numbers[1]
z = numbers[2]
'''
x,y,z = numbers
print(f"x->{x}\ny->{y}\nz->{z}\n")
--------------------------------输出结果
x->1
y->2
z->3
字典
#键值对 key大小写敏感
student = {
"name": "zhangsan",
"age":30,
"is_verified": True
}
student["birthday"] = "1999 01 01"
print(student["name"])
# 有两种方法获得值 一种[] 一种get方法,get方法可以设置默认值,如果值不存在就返回默认值
print(student["birthday"])
print(student.get("birthday","1111111111"))
print(student.get("bbbb","1111111111"))
练习 输入数字返回它的英文
输入:1234
返回: one two three four
numbers = {
1 : 'one',
2 : 'two',
3 : 'three',
4 : 'four',
5 : 'five',
6 : 'six',
7 : 'seven',
8 : 'eight',
9 : 'nine',
0 : 'zero',
}
number = input("Phone: ")
for item in number:
print(numbers.get(int(item)),end=" ")
练习 表情转换
message = input(">")
words = message.split(' ')
emojis = {
":)": "😊",
":(": "😂"
}
output = ""
for word in words:
output += emojis.get(word,word) + " "
print(output)
-----------------------------输出结果
>good morning :)
good morning 😊