從今天開始, 小白我將帶領大家學習一下 Python 零基礎入門的內容. 本專欄會以講解 + 練習的模式, 帶領大家熟悉 Python 的語法, 應用, 以及代碼的基礎邏輯.
def assignment():
# a. assign x the value 8
# b. assign both x and y the value 9 in one line of code
# c. assign x the value 10 and y the value 12 using one line of code
# d.
# run the program
assignment()
預期輸出:
8
9 9
10 12
7
def even_odd(x):
# your code
# define some sample data
a=45
# run the program
even_odd(a)
預期輸出:
odd
def check_max(lst):
# your code
# define some sample values
nums=[3, 41, 12, 9, 74, 15]
# run the program
check_max(nums)
預期結果:
74
# your code
check_balance(1000,1400)
check_balance(1000,800)
check_balance(1000,1200)
預期輸出:
your account balance has increased by $ 400
your account balance has decreased by $ 200
No action is required at this time
def assignment():
# a. assign x the value 8
x=8
print(x)
# b. assign both x and y the value 9 in one line of code
x=y=9
print(x,y)
# c. assign x the value 10 and y the value 12 using one line of code
x,y=10,12
print(x,y)
# d.
x=4
x+=1
x+=1
x+=1
print(x)
# run the program
assignment()
def even_odd(x):
# your code
if x%2==0:
print('even')
else:
print('odd')
# define some sample data
a=45
# run the program
even_odd(a)
def check_max(lst):
# your code
Max_num = None
for num in lst:
if (Max_num is None or num > Max_num):
Max_num = num
print(Max_num)
# define some sample values
nums=[3, 41, 12, 9, 74, 15]
# run the program
check_max(nums)
# your code
def check_balance(num1, num2):
# 判斷
if (num2 > num1 * 1.25):
print("your account balance has increased by $", num2 - num1)
elif (num2 < num1 * 0.98):
print("your account balance has decreased by $", num1 - num2)
else:
print("No action is required at this time")
# 調用
check_balance(1000,1400)
check_balance(1000,800)
check_balance(1000,1200)
輸出結果:
your account balance has increased by $ 400
your account balance has decreased by $ 200
No action is required at this time