Mosh的Python教程练习2

加减乘除计算
print(10+8)
print(10-8)
print(10*8)
print(10/8)
print(10//8) #取整
print(10%8)
print(10**3) #乘方 x**y==x的y次方

#打印:
18
2
80
1.25
1
2
1000

x=10 #扩展赋值运算符
print(x+=3)
#打印: x=13
#加减乘除运算顺序
1 exponentiation 指数幂
2 multiplication or division 乘除法
3 addition or substraction 加减法

x=10+2*2**3
print(x)
打印:26
#四舍五入函数round()
x=2.9
print(round(x))
#绝对值函数abs()
x=-2.9
print(abs(x))
#导入模块并使用模块中的一些函数
import math #导入math module
print(math.ceil(2.9)) #向上进位 打印:3
print(math.floor(2.9)) #向下进位 打印:2
#更多math module使用看(搜索python 3 math module)
# https://docs.python.org/3/library/math.html
#if-else的双分支
is_hot=True
if is_hot:
    print("It's a hot day.")
else:
    print("It's a cold day.")
print("Enjoy your day")
#if-else的多分支
is_hot=False
is_cold=True
if is_hot:
    print("It's a hot day.")
elif is_cold:
    print("It's a cold day.")
else:
    print("It's not hot and not cold day.")
print("Enjoy your day")
price=1000000
has_good_credit=True
if has_good_credit:
    down_payment=0.1*price
else:
    down_payment=0.2*price
print(f"Down Payment: ${down_payment}")

//Down Payment: $100000.0
#逻辑运算符 and 的使用
has_high_income=True
has_good_credit=True
if has_high_income and has_good_credit:
    print("Eligable for loan.")
#逻辑运算符 or 的使用
has_high_income=True
has_good_credit=False
if has_high_income or has_good_credit:
    print("Eligable for loan.")
#逻辑运算符 not 的使用
has_good_credit = True
has_criminal_record = False
if has_high_income and not has_criminal_record:
    print("Eligable for loan.")
#比较运算符
temperature=30

if temperature > 30:
    print("It's a hot day.")
else:
    print("It's not a hot day.")

if temperature == 30: #和赋值运算符=不一样
    print("It's a hot day.")
else:
    print("It's not a hot day.")

你可能感兴趣的:(Python学习)