Python 使用 matplotlib 畫圖

本篇要介紹如何使用 python 搭配 matplotlib 模組將資料數據用圖形的方式呈現出來,用 matplotlib 來 plot 畫圖算是數據分析很常用到的輔助工具, 一定要學會,之後便用 python 行雲流水!

使用範例

下面為基本的畫圖範例,一組資料是水平的時間資料,另外一組資料是數據資料,
之後就可以使用 plot() 畫出這些數據,grid()是背景顯示格子狀,show() 是將圖顯示出來。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt

def main():
time = [1, 2, 3, 4]
data = [1, 4, 9, 16]
plt.plot(time, data, label='x')
plt.xlabel('time [second]')
plt.grid()
plt.legend()
plt.show()

pass

if __name__ == '__main__':
main()

畫出來的結果如下圖:

相關主題
Python 使用 numpy 讀取 csv 資料再畫圖

Ubuntu adb bash completion 自動完成

Ubuntu 16.04 安裝 bash-completion 以後, 很多指令都可以按 tab 自動完成,
但是有些指令就沒有支援到, 例如:adb
使得在 Android 開發上很不方便,
以下步驟為 ubuntu 安裝 adb 指令的自動完成:

  1. 安裝 bash-completion

    1
    sudo apt-get install bash-completion
  2. 下載 adb script

  3. 複製 android 腳本到 /etc/bash_completion.d 目錄下

    1
    sudo cp android /etc/bash_completion.d
  4. 重啟你的 shell, 就可以使用 adb 按 tab 自動完成了!

Android 安裝 Busybox (adb shell 使用 vi)

Android shell 裡預設沒有 vi 指令的工具, Android 系統去除了標準 Linux 的大多數工具,導致我們 Android 開發上與除錯時特別麻煩, 如果你的 android device 有 root 權限的話,
可以把編譯好的 busybox 推進去使用, 開發上會方便許多, 而且會節省很多時間。

以下內容將開始介紹如何在 Android 安裝 busybox,如果想要了解 busybox 的基本用法可以看這篇

busybox binaries 可以從 https://busybox.net/downloads/binaries/ 下載

查看 Android CPU 架構

可以藉由下列指令查看你的 Android CPU 架構然後下載對應的 busybox binary。

1
adb shell 'cat /proc/cpuinfo' | grep Processor

在 Android 安裝 busybox

我是下載 https://busybox.net/downloads/binaries/1.31.0-defconfig-multiarch-musl/busybox-armv8l,點下去如果沒有下載的話請滑鼠右鍵另存新檔來下載,
安裝前確認 Android 裡有無 /system/xbin 目錄, 本範例是把 busybox 安裝到 /system/xbin

1
2
3
4
5
6
adb root
adb remount

adb push busybox-armv8l /system/xbin/busybox
adb shell 'chmod 755 /system/xbin/busybox'
adb shell '/system/xbin/busybox --install /system/xbin/'

接下來就可以在 adb shell 裡使用 vi 指令了!!!
另外 find, grep, tee, sed, wget 這些工具也都包在裡面可以使用囉~

另外想查看 busybox 有支援什麼其他指令工具的話,可以直接輸入 busybox 指令看看結果。

在 Android 執行 busybox

以 top 指令為例,在 adb shell 下輸入 top 是用原本 Android 的 top (也就是 /system/bin/top),要改使用 busybox 的 top 話請改輸入 busybox top (也就是/system/xbin/top) 即可。

相關主題
BusyBox 基本用法教學
Android adb 基本用法教學

[PyQt5] button 按鈕

使用 pyqt5 寫 python 視窗程式時,button 按鈕是最基本最常用的功能, 一定要學起來, 以下介紹如何在 pyqt5 的一個視窗裡建立 button 按鈕。

基本範例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QLineEdit, QMessageBox)
from PyQt5.QtCore import QCoreApplication

class MyWidget(QWidget):

def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
self.setWindowTitle('Button example')
self.setGeometry(300, 300, 300, 200)

self.btn = QPushButton('Button', self)
self.btn.move(10, 10)
self.btn.clicked.connect(self.onClick)

btn_quit = QPushButton('Quit', self)
btn_quit.move(10, 50)
btn_quit.clicked.connect(QCoreApplication.instance().quit)

self.textbox = QLineEdit(self)
self.textbox.move(10, 90)
self.textbox.resize(160, 30)

self.show()

def onClick(self):
buttonReply = QMessageBox.question(self, 'Message', "Do you like PyQt5?",
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if buttonReply == QMessageBox.Yes:
print('Yes clicked.')
self.textbox.setText("Yes clicked.")
else:
print('No clicked.')
self.textbox.setText("No clicked.")

if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyWidget()
sys.exit(app.exec_())

結果如下圖:

[PyQt5] menu 選單

使用 pyqt5 寫 python 視窗程式時,menu 選單是最基本常用的功能, 以下介紹如何在 pyqt5 的一個視窗裡新增 menu 選單。

基本範例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget,
QPushButton, QAction, QMessageBox)
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import QCoreApplication

class MyMainWindow(QMainWindow):

def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
self.setWindowTitle('Menu example')
self.setGeometry(300, 300, 300, 200)

# main menu
mainMenu = self.menuBar()

# file menu
fileMenu = mainMenu.addMenu('File')

openButton = QAction('Open', self)
openButton.triggered.connect(self.onOpenFile)
fileMenu.addAction(openButton)

exitButton = QAction('Exit', self)
exitButton.setShortcut('Ctrl+Q')
exitButton.triggered.connect(self.close)
fileMenu.addAction(exitButton)

# help menu
helpMenu = mainMenu.addMenu('Help')
aboutButton = QAction('About', self)
aboutButton.triggered.connect(self.onAbout)
helpMenu.addAction(aboutButton)

aboutQtButton = QAction('AboutQt', self)
aboutQtButton.triggered.connect(self.onAboutQt)
helpMenu.addAction(aboutQtButton)

self.show()

def onOpenFile(self):
QMessageBox.information(self, 'Info', 'Open file ...')

def onAbout(self):
QMessageBox.about(self, 'About', 'This is about message.')

def onAboutQt(self):
QMessageBox.aboutQt(self)

if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyMainWindow()
sys.exit(app.exec_())

結果如下圖:

Python dict 字典用法與範例

本篇 ShengYu 要介紹 Python dict 字典用法與範例,dict 字典是一個 key-value 對應的容器,能用鍵(key)來查詢對應的值(value),所以一個字典裡的 key 是不會重複的具有唯一性,dict 是 python 中很好用且常用的資料結構,dict 字典它可以動態地新增與刪除資料,字典 dict 裡的資料儲存沒有順序性,以下為 Python dict 用法介紹。

以下 Python dict 用法與範例分為這幾部份,

  • Python 初始化 dict 字典
  • 建立空 dict 字典
  • 插入與更新 dict 字典裡的元素
  • 遍歷印出 dict 字典裡所有元素
  • 刪除 dict 字典裡指定的元素
  • 清空 dict 字典裡所有的元素
  • 刪除整個 dict 字典
  • 測試 key 有沒有存在 dict 字典裡

Python 初始化 dict 字典

我們以學生編號與姓名為例,學生的編號(整數)為 key,value 的部分則是填入學生的姓名(字串),
Python 中 {} 大刮號表示 dict,
我們可以在 dict 初始化時給予起始資料,初始化 dict 的寫法如下,

1
2
3
4
5
6
7
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

student_dict = {1:'tom', 2:'john', 3:'alice'}
print(type(student_dict))
print(len(student_dict))
print(student_dict)

結果輸出是這樣,

1
2
3
<class 'dict'>
3
{1: 'tom', 2: 'john', 3: 'alice'}

要根據 key 來取得 value 的話,如果 dict 帶入不存在的 key 的話執行時會出現錯誤,

1
2
3
4
5
student_dict = {1:'tom', 2:'john', 3:'alice'}
print(student_dict[1])
print(student_dict[2])
print(student_dict[3])
# print(student_dict[4]) # error

輸出如下,

1
2
3
tom
john
alice

那我們換個方式以學生姓名(字串)當作 key,學生編號(整數)當作 value 來試試看,

1
2
3
4
5
student_dict = {'tom':1, 'john':2, 'alice':3}
print(student_dict['tom'])
print(student_dict['john'])
print(student_dict['alice'])
# print(student_dict['jack']) # error

輸出如下,

1
2
3
1
2
3

建立空 dict 字典

如果想建立一個空字典 dict 寫法為下,這時 student_dict 裡面是沒有任何資料的,

1
2
3
4
5
student_dict1 = {}
# 或者這樣寫
student_dict2 = dict()
print(student_dict1)
print(student_dict2)

輸出如下,

1
2
{}
{}

插入與更新 dict 字典裡的元素

這邊我們反過來,以學生姓名(字串)為 key,學生的編號(整數)當 value,

1
2
3
4
5
6
7
8
9
student_dict = {}
student_dict['Tom']=100
student_dict['John']=102
student_dict['Alice']=101

print(student_dict['Tom'])
print(student_dict['John'])
print(student_dict['Alice'])
print(student_dict)

輸出結果為

1
2
3
4
100
102
101
{'John': 102, 'Alice': 101, 'Tom': 100}

遍歷印出 dict 字典裡所有元素

印出 dict 字典裡所有元素最簡單的方式可以這樣寫

1
2
student_dict = {'Tom':100, 'John':102, 'Alice':101}
print(student_dict)

輸出結果為

1
{'Tom': 100, 'John': 102, 'Alice': 101}

或是這樣

1
2
student_dict = {'Tom':100, 'John':102, 'Alice':101}
print(student_dict.items())

輸出結果為

1
dict_items([('Tom', 100), ('John', 102), ('Alice', 101)])

或者用 for 迴圈迭代,像這樣

1
2
3
4
5
6
7
8
9
10
11
12
13
14
student_dict = {'Tom':100, 'John':102, 'Alice':101}

for key in student_dict:
print(key)

for key in student_dict.keys():
print(key)

for value in student_dict.values():
print(value)

# 迭代印出 key 與 value
for key, value in student_dict.items():
print(key + ': ' + str(value))

結果如下,

1
2
3
4
5
6
7
8
9
10
11
12
Tom
John
Alice
Tom
John
Alice
100
102
101
Tom: 100
John: 102
Alice: 101

如果不想按照 key 排序,想照 value 排序的話可以使用 python 的 sorted 函式,
這之前在 Python sort 排序 這篇介紹過了,有興趣的可以回去複習,
下面直接使用,

1
2
student_dict = {'Tom':100, 'John':102, 'Alice':101}
print(sorted(student_dict.items(), key=lambda x:x[1]))

結果如下,

1
[('Tom', 100), ('Alice', 101), ('John', 102)]

這邊要注意的是 sorted 會回傳一個新的 list,所以如果要後續處理的話,
要用一個 list 去接,像下面這樣寫,印出來的結果跟上面一樣

1
2
3
4
student_dict = {'Tom':100, 'John':102, 'Alice':101}
student_list = [] # 宣告一個空的dict,可不寫
student_list = sorted(student_dict.items(), key=lambda x:x[1])
print(student_list)

刪除 dict 字典裡指定的元素

dict 字典使用 del 來移除刪除元素,

1
2
3
4
student_dict = {'Tom':100, 'John':102, 'Alice':101}

del student_dict['Tom']
print(student_dict)

結果如下,

1
{'John': 102, 'Alice': 101}

清空 dict 字典裡所有的元素

要清空 dict 字典裡所有的元素,使用 clear(),清空後這個字典裡面就空空了,也就變成一個空字典了,

1
2
3
4
student_dict = {'Tom':100, 'John':102, 'Alice':101}

student_dict.clear()
print(student_dict)

結果如下,

1
{}

刪除整個 dict 字典

如果想毫不留情的刪除整個字典,之後再也不需要它的話,可以用 del,刪除後再去存取 dict 會執行錯誤唷!

1
2
3
4
student_dict = {'Tom':100, 'John':102, 'Alice':101}

del student_dict
# print(student_dict) # error

測試 key 有沒有存在 dict 字典裡

剛剛前述例子可以發現如果 dict 帶入不存在的 key 去存取 value 的話執行時就會出錯,但是假如今天我就是有這個需求想要試試看 dict 裡面有沒有這個 key,如果 key 有存在的話就回傳該 value,如果 key 沒有存在的話就回傳預設值,沒有給定預設值的話會回傳 None,

1
2
3
4
5
6
7
8
9
student_dict = {'Tom':100, 'John':102, 'Alice':101}
value1 = student_dict.get('Jack')
print(value1)
value2 = student_dict.get('Jack', 103)
print(value2)
value3 = student_dict.get('Alice')
print(value3)
value4 = student_dict.get('Alice', 104)
print(value4)

輸出結果如下,

1
2
3
4
None
103
101
101

下一篇將介紹 tuple 元組的用法

以上就是 Python dict 字典用法與範例介紹,
如果你覺得我的文章寫得不錯、對你有幫助的話記得 Facebook 按讚支持一下!

其它相關文章推薦
如果你想學習 Python 相關技術,可以參考看看下面的文章,
Python 檢查 dict 字典是否為空
Python 新手入門教學懶人包
Python str 字串用法與範例
Python list 串列用法與範例
Python set 集合用法與範例
Python tuple 元組用法與範例

Python list 串列用法與範例

本篇 ShengYu 要介紹 python list 串列用法與範例,list 串列是個簡單好用的東西,python 常常使用到,一定要學起來,list 串列它可以動態地新增與刪除資料,以下為 list 串列的基本範例。

以下 Python list 內容分為這幾部份,

  • Python 初始化 list 串列
  • 建立空 list 串列
  • 計算 list 串列長度
  • 讀取串列的元素,串列索引
  • 串列索引值為 -1 或 -n
  • 串列切片
  • 添加元素到 list 串列
  • 修改串列元素的內容
  • for 迴圈遍歷巡訪串列裡的元素
  • 刪除 list 串列裡的元素
  • 清空 list 串列裡所有的元素
  • 刪除整個 list 串列

Python 初始化 list 串列

python list 初始化的寫法如下,你可以初始化一個串列裡面都存放整數,也可以都放字串,也可以混合整數跟字串一起放,還可以放其他串列,用 type() 可以檢查這個變數的類型,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
list1 = [1, 2, 3]
print(list1)
print(type(list1))

list2 = ['apple', 'banana', 'orange', 'tomato']
print(list2)
print(type(list2))

list3 = ['hello', 'world', 123, 456]
print(list3)
print(type(list3))

list4 = [list1, list2, list3]
print(list4)
print(type(list4))

輸出如下,

1
2
3
4
5
6
7
8
[1, 2, 3]
<class 'list'>
['apple', 'banana', 'orange', 'tomato']
<class 'list'>
['hello', 'world', 123, 456]
<class 'list'>
[[1, 2, 3], ['apple', 'banana', 'orange', 'tomato'], ['hello', 'world', 123, 456]]
<class 'list'>

建立空 list 串列

如果想建立一個空串列 list 寫法為下,這時 list 裡面是沒有任何資料的,

1
2
3
4
5
6
7
list1 = []
# 或者這樣寫
list2 = list()
print(list1)
print(list2)
print(type(list1))
print(type(list2))

輸出如下,

1
2
3
4
[]
[]
<class 'list'>
<class 'list'>

計算 list 串列長度

python 要計算 list 串列的長度的話可以使用 len() 來計算,

1
2
list1 = [1, 2, 3, 4, 5]
print(len(list1))

輸出如下,

1
5

讀取串列的元素,串列索引

利用串列索引值 index 來取得元素,索引值從 0 開始,第一個元素的索引值為 0,第二個元素的索引值為 1,依此類推,

1
2
3
4
list1 = [1, 2, 3 ,4 ,5]
print(list1[0])
print(list1[1])
print(list1[2])

輸出如下,

1
2
3
1
2
3

串列索引值為 -1 或 -n

串列索引值是 -1 表示串列的最後一個元素,索引值是 -2 表示串列的倒數第二個元素,索引值是 -n 表示串列的倒數第 n 個元素,依此類推,

1
2
3
4
list1 = [1, 2, 3 ,4 ,5]
print(list1[-1])
print(list1[-2])
print(list1[-3])

輸出如下,

1
2
3
5
4
3

串列切片

使用串列切片 slicing 來取得指定範圍內元素,更多串列切片 slicing 的教學範例請看這篇

1
2
3
4
5
6
list1 = [1, 2, 3 ,4 ,5]
print(list1[0:3]) # 串列索引值0到索引值3
print(list1[2:7]) # 串列索引值2到索引值7
print(list1[2:]) # 串列索引值2到最後
print(list1[:4]) # 串列前4個元素
print(list1[:]) # 串列所有元素

輸出如下,

1
2
3
4
5
[1, 2, 3]
[3, 4, 5]
[3, 4, 5]
[1, 2, 3, 4]
[1, 2, 3, 4, 5]

添加元素到 list 串列

python 要將元素添加到 list 串列裡的話要使用 append()

1
2
3
4
5
6
7
8
9
10
list1 = [1, 2, 3]
print(list1)
list1.append(4)
list1.append(5)
print(list1)

list2 = []
list2.append(1)
list2.append(2)
print(list2)

輸出如下,

1
2
3
[1, 2, 3]
[1, 2, 3, 4, 5]
[1, 2]

修改串列元素的內容

要修改串列元素的內容可以利用索引值來修改,方式如下,

1
2
3
4
5
6
7
8
9
10
list1 = [1, 2, 3 ,4 ,5]
print(list1)
list1[2] = 6
list1[4] = 7
print(list1)

list2 = ['apple', 'banana', 'orange']
print(list2)
list2[0] = 'tomato'
print(list2)

輸出如下,

1
2
3
4
[1, 2, 3, 4, 5]
[1, 2, 6, 4, 7]
['apple', 'banana', 'orange']
['tomato', 'banana', 'orange']

for 迴圈遍歷巡訪串列裡的元素

用 for 迴圈來遍歷巡訪串列 tuple 裡的所有元素並印出來,

1
2
3
list1 = ['hello', 'world', 123, 456]
for i in list1:
print(i)

輸出如下,

1
2
3
4
hello
world
123
456

更多 for 迴圈用法請看這篇

刪除 list 串列裡的元素

python 要刪除 list 串列裡的指定元素的話要使用 del,

1
2
3
4
5
6
list1 = [1, 2, 3, 4, 5]
print(list1)
del list1[0]
print(list1)
del list1[1]
print(list1)

輸出如下,

1
2
3
[1, 2, 3, 4, 5]
[2, 3, 4, 5]
[2, 4, 5]

清空 list 串列裡所有的元素

要清空 list 串列裡所有的元素可以使用 clear(),清空後這個 list 裡面就空空了,也就變成一個空 list 串列了,

1
2
3
4
list1 = ['apple', 'banana', 'orange', 'tomato']
print(list1)
list1.clear()
print(list1)

輸出如下,

1
2
['apple', 'banana', 'orange', 'tomato']
[]

刪除整個 list 串列

要刪除整個 list 串列的寫法如下,

1
2
3
4
list1 = [1, 2, 3, 4, 5]
print(list1)
del list1
# print(list1) # error

輸出如下,

1
[1, 2, 3, 4, 5]

下一篇將介紹 set 集合的用法

以上就是 Python list 串列用法與範例介紹,
如果你覺得我的文章寫得不錯、對你有幫助的話記得 Facebook 按讚支持一下!

其它相關文章推薦
如果你想學習 Python 相關技術,可以參考看看下面的文章,
Python 串列切片
Python 檢查 list 列表是否為空
Python 新手入門教學懶人包
Python str 字串用法與範例
Python set 集合用法與範例
Python dict 字典用法與範例
Python tuple 元組用法與範例

Python str 字串用法與範例

本篇 ShengYu 要介紹 Python str 字串用法與範例,str 字串是 python 最基本的功能,以下為 Python str 字串的基本用法與範例。

以下 Python str 內容將分為這幾部份,

  • Python 字串基本用法
  • 字串連接
  • 讀取字串的元素,字串索引
  • 字串索引值為 -1 或 -n
  • for 迴圈遍歷巡訪字串裡的元素
  • 建立空字串
  • 字串切片
  • 字串切割
  • 字串去除空白
  • 用 str() 將其他類型轉為字串

Python 字串基本用法

Python 的字串語法為文字前後加上兩個單引號 '' 或兩個雙引號 "",例如:'hello world' 或者 "hello world",依個人喜好或情境來選擇使用,例如今天如果遇到字串裡有包含 ' 像是 "ShengYu's notebook",就使用兩個雙引號來表示字串。

Python 字串的變數類型為 str,可以使用 type() 來檢查變數類型得知。

Python 字串可以使用 len() 來計算字串的長度。

1
2
3
4
s = 'hello world'
print(s)
print(type(s))
print(len(s))

輸出結果如下,

1
2
3
hello world
<class 'str'>
11

字串連接

在 Python 上使用 + 加號來連接兩個字串,用法範例如下,

1
2
3
4
5
s1 = 'hello'
s2 = ' world'
s3 = s1 + s2
print(s3)
print('hello' + ' ' + 'world')

輸出結果如下,

1
2
hello world
hello world

讀取字串的元素,字串索引

這邊介紹字串索引或讀取字串裡的元素的方法

1
2
3
4
s = 'hello world'
print(s[0])
print(s[1])
print(s[2])

輸出結果如下,

1
2
3
h
e
l

字串索引值為 -1 或 -n

字串索引值是 -1 表示字串的最後一個元素,索引值是 -2 表示字串的倒數第二個元素,索引值是 -n 表示字串的倒數第n個元素,依此類推,

1
2
3
4
s = 'hello world'
print(s[-1])
print(s[-2])
print(s[-3])

輸出結果如下,

1
2
3
d
l
r

for 迴圈遍歷巡訪字串裡的元素

用 for 迴圈來遍歷巡訪字串 str 裡的所有元素並印出來,

1
2
3
s = 'hello world'
for i in s:
print(i)

輸出結果如下,

1
2
3
4
5
6
7
8
9
10
11
h
e
l
l
o

w
o
r
l
d

更多 for 迴圈用法請看這篇

建立空字串

建立空字串方式如下,

1
2
3
4
5
6
7
8
9
s1 = ''
print(s1)
print(type(s1))
print(len(s1))

s2 = str()
print(s2)
print(type(s2))
print(len(s2))

輸出結果如下,

1
2
3
4
5
6

<class 'str'>
0

<class 'str'>
0

字串切片

字串切片就是基於原本字串中取得某區間的元素,例如:前幾個元素或後幾個元素等等,用法範例如下,

1
2
3
4
5
6
s = 'hello world'
print(s[0:3]) # 字串索引值0到索引值3
print(s[2:7]) # 字串索引值2到索引值7
print(s[2:]) # 字串索引值2到最後
print(s[:7]) # 字串前7個元素
print(s[:]) # 字串所有元素

輸出結果如下,

1
2
3
4
5
hel
llo w
llo world
hello w
hello world

詳細的字串切片用法與範例可以看這篇

字串切割

字串切割也是常常用的的功能,簡單的字串切割空白作為分隔符號的用法範例如下,

1
2
s = 'hello world'
print(s.split())

輸出結果如下,

1
['hello', 'world']

詳細的字串切割用法與範例可以看這篇

字串去除空白

偶爾會需要將字串的空白或其他多餘的字元給去除,就要這樣使用,

1
2
s = ' hello world '
print(s.strip())

輸出結果如下,

1
hello world

詳細的字串去除空白或其他多餘的字元用法與範例可以看這篇

用 str() 將其他類型轉為字串

str() 可以將整數或浮點數或其他變數類型的資料轉換為字串,用法範例如下,

1
2
3
4
5
6
7
8
9
n = 123
s1 = str(n)
print(type(n))
print(s1)

f = 123.456
s2 = str(f)
print(type(f))
print(s2)

輸出結果如下,

1
2
3
4
<class 'int'>
123
<class 'float'>
123.456

下一篇將介紹 list 串列的用法

以上就是 Python str 字串用法與範例介紹,
如果你覺得我的文章寫得不錯、對你有幫助的話記得 Facebook 按讚支持一下!

其它相關文章推薦
如果你想學習 Python 相關技術,可以參考看看下面的文章,
Python 字串切片
Python 檢查 str 字串是否為空
3 種 Python 字串搜尋並且忽略大小寫方法
4 種 Python 字串中搜尋關鍵字的方法
Python 新手入門教學懶人包
Python list 串列用法與範例
Python set 集合用法與範例
Python dict 字典用法與範例
Python tuple 元組用法與範例

C/C++ clang-format 對程式碼風格排版格式化

在大型專案裡多人一起開發, 程式碼的 coding sytle 統一是個值得討論的議題,
統一 coding sytle 有下列好處:

  • 提昇 code 閱讀速度
  • 規則明確, 無須多花費時間

以下介紹一個 c/c++ coding sytle 統一的好工具, clang-format

安裝方法

1
sudo apt-get install clang-format

clang-format 對程式碼排版格式化

輸入以下指令就會將 main.cpp 進行風格統一

1
clang-format -i main.cpp

git 指令對程式碼排版格式化

如果我只想針對我的修改部分做clang-format要怎麼做?

首先先將更動的檔案git add加入stage,
再使用git clang-format就會對stage裡檔案的修改部分進行clang-format重新排版。

1
2
3
git add <file>
...
git clang-format

Visual Studio Code 對程式碼排版格式化

VS Code 要先裝clang-format擴充套件才能夠對程式碼clang-format排版格式化,
格式化原始碼的快捷鍵為:
Ctrl + Shift + f (Windows)
Ctrl + Shift + i (Linux)
Option + Shift + f (MacOS)
也可以按右鍵選擇 Format Document。

.clang-format 程式碼風格設定檔

如果目錄下有.clang-format檔會以這個檔裡設定值為主。

其他參考
使用 clang-format 對程式碼進行排版
https://hsins.me/blog/2019/05/27/format-source-code-with-clang-format/

其它相關文章推薦
C/C++ 新手入門教學懶人包
Visual Studio Code 常用快捷鍵

C/C++ 常見的各種程式碼風格

在一般大型專案裡開發, 不管是公司還是開源專案裡, 開發貢獻專案的人一多, 各式各樣的風格就會呈現在程式碼,
這時通常很難沈住氣去讀code, 通常是邊讀邊罵, 這時就需要一杯咖啡靜下心來, 再好好地對付這些程式碼,
在沒有能力去統一這些風格前, 只好訓練自己, 成為看懂各式各樣 code 的人,
要能看懂這些風格的 code 前, 需要先了解有哪些派別, 下列列出常見的 c/c++ code 各種風格:

  1. macro 派 : 各種難以 trace 的神 macro function
  2. 新潮派 : 各種最新最潮的 C++11 寫法
  3. C style 派 : 大量 typedef struct / malloc 和 free
  4. OO 派 : 喜愛 new 和 delete, 各式各樣的 class 與繼承關係
  5. 三位一體 : call by pointer / reference / copy 在同一個 function 參數列共存
  6. template 派: 很愛編譯時期決定的朋友

其它相關文章推薦
C/C++ 新手入門教學懶人包
C/C++ snippets 程式碼片段