- Sep 10 Tue 2019 08:22
-
十種文字特效,讓你的網路行銷影片獨樹一格~
- Sep 09 Mon 2019 06:39
-
不能不懂的Python基本常識3-升冪降冪,商數餘數

Python還能使用在升冪降冪上!不只這樣,還能告訴你餘數跟商數,怎麼用?繼續看下去就知道!
升冪降冪
除了加,減,乘和除等四則運算之外,Python還支援升冪、降冪的運算,只要使用兩個星號「**」,就馬上可進行冪運算。下面這例子,即是進行 2 的五次方與 9 的 1/2 次方運算。
>>> 2**5
32
>>> 9 ** (1/2)
3.0
商數與餘數的運算
Python中進行除法運算時,對於有小數點的場合時(例如 1 除以 3 得到的答案會是 0.3333333333⋯⋯),有時候我們需要忽略小數點後的所有數字。此時就可以使用 Floor Division(地板除法)來得到只有整數的商數。
Floor Division (地板除法) 在 Python 以兩個斜槓「//」表示:
>>> 20 // 6
3
呈上例,我們想要得到整除後的餘數,就使用百分比符號「%」做為運算子:
>>> 20 // 6
2
運算子「%」與「//」可綜合活用,如下例:
>>> 7%(5 // 2)
1
- Sep 06 Fri 2019 02:47
-
不能不懂的Python基本常識2-Floats浮點數介紹
- Sep 05 Thu 2019 04:49
-
不能不懂的Python基本常識1-加減乘除四則運算練習

加減乘除對Python也是小菜一碟!一起來學學該如何利用它作基本的四則運算練習!
把Python當小算盤,做出基本四則運算
大家都知道,Python具有執行計算的能力。 現在,就讓我們來練習一下,在Python輸入一個計算公式後,Python將輸出(output)答案:
>>> 1 + 1
2
>>> 2 + 4 - 3
3
除此之外,Python 還執行乘法和除法:使用星號表示乘法,使用斜杠表示除法。Python 計算的順序也是符合數學四則運算規則的:「先乘除,後加減」,括號先算。
>>> 2 * (3 + 4)
14
>>> 10 / 2
5.0
Python 的除法使用單斜杠「/」符號,答案會產生十進制數(在程式設計中稱為「浮點數」)。
Python 也可對負數進行四則運算:
>>> ( - 5 - 1) * 3
-18
另外,在 Python 中除以零,會產生錯誤。因為無法計算出答案
>>> 11 / 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
在Python中,錯誤訊息的最後一行指出錯誤的類型。工程師需要仔細閱讀錯誤消息,因為它們經常告訴您如何修復。
相關閱讀推薦:
不能不懂的Python基本常識2-Floats浮點數介紹
不能不懂的Python基本常識3-升冪降冪,商數餘數
不能不懂的Python基本常識4-Strings字串
五個月的JAVA課程,讓你自信轉職去!
暑假快到了!與其讓兒子盲目拚學測,我寧可讓他先選擇人生方向~
AI新世界 不想被機器人取代就從Python課程開始
NOWnews 今日新聞
- Sep 04 Wed 2019 08:55
-
Python基礎功不可少-dir()與help()的使用

Python課程必教的就是它的內建函數(Built-in Functions),一起來看看!
Python 中 dir()、help()的使用
想要使用Python來編寫程式碼時,經常會選擇使用 Python 中的內建函數 (Built-in Functions)或是模組。如果遇到對一些函數、模組不清楚的情形,則可以通過 dir() 和 help() 查詢說明或屬性。
help(): 用於查看屬性及方法的詳細解釋
Python 內建函數 - dir()
「dir()」的用途,是用於查詢物件的全部屬性。例如今天你想使用 dir() 函數用於查看物件「str」 的全部屬性,你就寫了:
>>>print dir(str)
執行結果如下:
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
如果現在dir() 的括弧內不帶任何參數物件,它的執行結果則會最大限度地顯示出當前範圍內的變數、方法和屬性列表。
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
Python 內建函數 - help()
help(): 用於查看函式或模組用途的詳細說明。
例如今天你想使用 help() 用於查看「dir」 的用途,你就寫了:
>>>help(‘dir’)
執行結果如下:
Help on built-in function dir in module builtins:
dir(…)
dir([object]) -> list of strings
If called without an argument, return the names in the current scope.
Else, return an alphabetized list of names comprising (some of) the attributes
of the given object, and of attributes reachable from it.
If the object supplies a method named __dir__, it will be used; otherwise
the default dir() logic is used and returns:
for a module object: the module’s attributes.
for a class object: its attributes, and recursively the attributes
of its bases.
for any other object: its attributes, its class’s attributes, and
recursively the attributes of its class’s base classes.
- Sep 03 Tue 2019 05:37
-
關於Python的練習不嫌多!這題目你會嗎?

你了解Python字串的內容的意義嗎?練習完今天的題庫就能更熟悉囉!
在本節Python的練習中,我們將使用複雜的字串 String ,來建立一系列的變數,以便更加熟悉字串的用處。
首先,我們還是要了解一下字串這個概念。所謂字串,通常就是包含你想要展示出來的內容,或是你想要從程式裡導出 "" 的一小段字符。Python 可以通過文本裡的雙引號或者單引號,識別出何為字串。如果你把單引號或者雙引號括起來的文字放到 print 後面,它們就會被Python print出來。
字串可以包含格式化字符 %s(即以 str() 函數輸出文字)。你只要將格式化的變數放到字串中,再緊跟著一個百分號 % (percent),再緊跟著變數名即可。唯一要注意的地方是:如果你想要在字串中,通過格式化字符放入多個變數的時候,你需要將變數放到( ) 圓括號(parenthesis)中,而且變數之間用, 逗號(comma) 隔開。就像你逛商店說『我要買牛奶、麵包、雞蛋、八寶粥』一樣,只不過程式設計師說的是:「(milk, eggs, bread, soup)」。
在以下的練習中,我們將用簡化的變數名稱,輸入大量的變數、字串、和格式化字符,並且將它執行出來:
x = "There are %d types of people." % 10
binary = "binary"
do_not = "don't"
y = "Those who know %s and those who %s." % (binary, do_not)
print x
print y
print "I said: %r." % x
print "I also said: '%s'." % y
hilarious = False
joke_evaluation = "Isn't that joke so funny?! %r"
print joke_evaluation % hilarious
w = "This is the left side of..."
e = "a string with a right side."
print w + e
如果上面的操作都沒問題,你便可以看到下面的結果:
There are 10 types of people.
Those who know binary and those who don't.
I said: 'There are 10 types of people.'.
I also said: 'Those who know binary and those who don't.'.
Isn't that joke so funny?! False
This is the left side of...a string with a right side.
- Sep 02 Mon 2019 08:35
-
如何在第一次用Python做小遊戲就上手?看這篇就知道!
- Aug 30 Fri 2019 21:19
-
人工智慧為麻將文化注入新氣象?一起解密!(完)
- Aug 29 Thu 2019 07:08
-
人工智慧為麻將文化注入新氣象?一起解密!(三)
- Aug 28 Wed 2019 01:04
-
人工智慧為麻將文化注入新氣象?一起解密!(二)
- Aug 27 Tue 2019 05:54
-
人工智慧為麻將文化注入新氣象?一起解密!(一)
- Aug 26 Mon 2019 09:24
-
新創業都應該要重視SEO的七個重點(7)提供實際有效的結果







