零基礎(chǔ)Python教程-如何用Python進(jìn)行數(shù)學(xué)運(yùn)算
今天和大家分享一下零基礎(chǔ)Python教程—如何用Python進(jìn)行數(shù)學(xué)運(yùn)算,計(jì)算機(jī)本來(lái)就是用來(lái)進(jìn)行數(shù)學(xué)計(jì)算的,而且無(wú)論是C語(yǔ)言還Python,它都會(huì)被轉(zhuǎn)換成二進(jìn)制數(shù)字,來(lái)讓電腦識(shí)別,所以今天我們呢就學(xué)習(xí)一下如何用Python進(jìn)行數(shù)學(xué)運(yùn)算。
要學(xué)習(xí)數(shù)學(xué)計(jì)算然少不了運(yùn)算符了,我們這就先來(lái)認(rèn)識(shí)一下Python中的運(yùn)算符。
名字如下:
? + plus 加號(hào) ? - minus 減號(hào) ? / slash 斜杠 ? * asterisk 星號(hào) ? % percent 百分號(hào) ? < less-than 小于號(hào) ? > greater-than 大于號(hào) ? <= less-than-equal 小于等于號(hào) ? >= greater-than-equal 大于等于
有沒(méi)有注意到以上只是些符號(hào),沒(méi)有運(yùn)算操作呢?寫完下面的練習(xí)代碼后,再回到上面的列表,寫出每個(gè)符號(hào)的作用。例如 + 是用來(lái)做加法運(yùn)算的。
1 print "I will now count my chickens:"
2
3 print "Hens", 25 + 30 / 6
4 print "Roosters", 100 - 25 * 3 % 4
5
6 print "Now I will count the eggs:"
7
8 print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
9
10 print "Is it true that 3 + 2 < 5 - 7?"
11
12 print 3 + 2 < 5 - 7
13
14 print "What is 3 + 2?", 3 + 2
15 print "What is 5 - 7?", 5 - 7
16
17 print "Oh, that's why it's False."
18
19 print "How about some more."
20
21 print "Is it greater?", 5 > -2
22 print "Is it greater or equal?", 5 >= -2
23 print "Is it less or equal?", 5 <= -2
通過(guò)上面代碼的運(yùn)行,你將得到下面的結(jié)果,自己動(dòng)手操作一下是不是正確呢?
$ Python ex3.py
I will now count my chickens:
Hens 30 Roosters 97
Now I will count the eggs:
7
Is it true that 3 + 2 < 5 - 7?
False
What is 3 + 2? 5 What is 5 - 7? -2
Oh, that's why it's False. How about some more.
Is it greater? True Is it greater or equal?
True Is it less or equal? False
$
以上就是一個(gè)用Python寫的運(yùn)算實(shí)例,大家課下可以照著源碼多多練習(xí)一下。
好啦,今天的分享到這里就結(jié)束了,希望大家能夠持續(xù)關(guān)注馬哥教育的官網(wǎng),每天都會(huì)有大量?jī)?yōu)質(zhì)的內(nèi)容與大家分享歐!
聲明:文章來(lái)源于網(wǎng)絡(luò),侵刪!