Python初級 3 基本數學運算


一、 四大基本運算操作符

1 加+

print(3 + 2)

2 減-

print(3 - 2)

3 乘:*

print(3 * 2)

4 除/, //

print(3 / 2)

print(3 // 2)

5 操作符,操作數

image

 

練習:

print(3 + 2)

print(3 – 2)

print(3 * 2)

print(3 / 2)

print(3 // 2)

 

 

二、運算順序

先乘除,再加減,有括號的先計算括號內的

print(3 + 5 * 3)

print(155 – 3 * 5 + 44 / 4)

 

三、另外四個操作符:

1、指數:**

自乘為一個冪,一個數的多少次冪

print(3 * 3 * 3 * 3 * 3)

3的5次冪

print(3 ** 5)

print(4 ** 5)

print(10 ** 2)

print(10 ** 3)

print(10 ** 4)

print(10 ** 5)

print(3.2 * 10 ** 5)

2、取余 :%

print(7%2)

3、自增 +=

score = score + 1

print(score)

score += 1

print(score)

4、自減 –=

score = score – 1

print(score)

score –= score

print(score)

 

練習:

print(3 * 3 * 3 * 3 * 3)

print(3 ** 5)

score = 5

score –= 1

print(score)

 

四、非常大和非常小

 

image

 

在計算機中不好表示,e記法:

3.8e16

 

image

 

 

附5個練習程序

 
 
 
         
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# 加法
# 輸入兩個加數,然后輸出計算結果

a = float(input("請輸入加數:"))
b = float(input("請輸入加數:"))
print("a + b = {}".format(a, b, a + b))

 
 
 
 
         
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# 減法
# 輸入被減數,減數,然后輸出計算結果

a = float(input("請輸入被減數:"))
b = float(input("請輸入減數:"))
print("{} - {} = {}".format(a, b, a - b))

# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# 乘法
# 輸入乘數,然后輸出計算結果

a = float(input("請輸入乘數:"))
b = float(input("請輸入乘數:"))
print("{} * {} = {}".format(a, b, a * b))

 

# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# 除法
# 輸入被除數,除數,然后輸出計算結果

a = float(input("請輸入被除數:"))
b = float(input("請輸入除數:"))
print("{} / {} = {}".format(a, b, a / b))

 

# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# 整除
# 輸入被除數,除數,然后輸出計算結果(顯示整除結果和余數)

a = int(input("請輸入被除數(整數):"))
b = int(input("請輸入除數(整數):"))
print("{} // {} = {}……{}".format(a, b, a // b, a % b))


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM