表達式
表達式(Expression)是運算符(operator)和操作數(operand)所構成的序列
>>> 1 + 1
2
>>> a = [1,2,3]
>>> 1 + 1 + 1 + 1
4
>>> 1 + 2 * 3
7
>>> 1 * 2 + 3
5
>>> a = 1 + 2 * 3
>>> a = 1
>>> b = 2
>>> c = a and b or c
>>> c = int('1') + 2
運算符優先級
同級的運算符的優先級還是有區別的 比如邏輯運算符裏的and的優先級大於or
>>> a = 1
>>> b = 2
>>> c = 3
>>> a + b * c
7
>>> 1 or 2
1
>>> 1 and 3
3
>>> a or b and c
1
>>> a or (b and c)
1
>>> a or 3
1
>>> (a or b) and c
3
>>> (a or b) and (c + 1) //兩個括號同級,左結合
4
>>> a = 1
>>> b = 2
>>> c = a + b //出現賦值符號時,右結合
>>> print(c)
3
>>> c = a or b
>>> print(c)
1
>>> a = 1
>>> b = 2
>>> c = 2
>>> not a or b + 2 == c
False
>>> ((not a) or ((b + 2) == c)) //優先級:not > and > or
False
在文本文件中編寫Python代碼
python腳本是後綴名爲.py的文件,通過命令行“python filename.py”執行
推薦的IDE:PyCharm、vsCode,大型工程適合用PyCharm,學習適合用vsCode,vsCode中推薦的插件:python、Terminal、Vim、vscode-icons
註釋
單行註釋用#
多行註釋用```
流程控制語句
主要有條件控制(if else)、循環控制(for while)、分支
條件控制(if else)
# encoding: utf-8
mood = False
if mood :
print('go to left')
# print('back away')
# print('back away')
else :
print('go to right')
a = 1
b = 2
c = 2
# if後面不僅可以是布爾值,還可以是表達式
if a or b + 1 == c :
print('go to left')
# print('back away')
# print('back away')
else :
print('go to right')
# encoding: utf-8
"""
一段小程序
"""
# constant 常量 建議全大寫
ACCOUNT = 'hughie'
PASSWORD = '123456'
# python變量建議都用小寫,用下劃線分隔單詞,不用駝峯命名
print('please input account')
user_account = input()
print('please input password')
user_password = input()
if ACCOUNT == user_account and PASSWORD == user_password:
print('success')
else:
print('fail')
# encoding: utf-8
# snippet 片段
if condition:
pass
else:
pass
a = True
if a:
# pass 空語句/佔位語句
pass
else:
print('')
if True:
pass
if False:
pass
# 嵌套分支
if condition:
if condition:
pass
else:
pass
else:
if condition:
pass
else:
pass
# 代碼塊
if condition:
code1
code11
code22
code333
code444
code5555
code6666
code2
code3
else:
code1
code2
code3
改寫爲elif
# encoding: utf-8
"""
a = x
a = 1 print('apple')
a = 2 print('orange')
a = 3 print('banana')
print('shopping')
"""
a = input()
print('a is' + a)
if a == 1:
print('apple')
else:
if a == 2:
print('orange')
else:
if a == 3:
print('banana')
else:
print('shopping')
# 改寫爲elif
a = input()
print(type(a))
print('a is ' + a)
a = int(a)
if a == 1:
print('apple')
elif a == 2:
print('orange')
elif a == 3:
print('banana')
else:
print('shopping')
循環(while for)
# encoding: utf-8
# 循環
# 循環語句
# while for
# CONDITION = True
# while CONDITION:
# print('I am while')
counter = 1
# 遞歸常用while
while counter <= 10:
counter += 1
print(counter)
else:
print('EOF')
# encoding: utf-8
# 主要是用來遍歷/循環 序列或者集合、字典
# a = ['apple', 'orange', 'banana', 'grape']
# for x in a:
# print(x)
# a = [['apple', 'orange', 'banana', 'grape'], (1, 2, 3)]
# for x in a:
# for y in x:
# # print(y, end='')
# print(y)
# else:
# print('fruit is gone')
# a = [1, 2, 3]
# for x in a:
# if x == 2:
# # break 遇到x==2的時候終止,打印出1
# # break
# # continue 遇到x==2的時候跳過,打印出1,3
# continue
# print(x)
# else:
# print('EOF')
a = [['apple', 'orange', 'banana', 'grape'], (1, 2, 3)]
for x in a:
# if 'banana' in x:
# break
for y in x:
if y == 'orange':
# 內部循環跳出後,外部循環還在執行
break
print(y)
else:
print('fruit is gone')
# encoding: utf-8
# for (i=0; i<10; i++){}
# 以上的for循環用python實現
# for x in range(0, 10):
# # range(0,10) 表示從0開始的10個數字,並不包含10
# print(x)
# for x in range(0, 10, 2):
# # range(0,10,2) 2表示步長
# print(x, end=' | ')
# # 打印結果:0 | 2 | 4 | 6 | 8 |
for x in range(10, 0, -2):
print(x, end=' | ')
# 打印結果:10 | 8 | 6 | 4 | 2 |
# encoding: utf-8
a = [1, 2, 3, 4, 5, 6, 7, 8]
# for i in range(0, len(a), 2):
# print(a[i], end=' | ')
# 1 | 3 | 5 | 7 |
b = a[0:len(a):2]
print(b)
# [1, 3, 5, 7]