Python的筆記1-基本數字、字串、變數

1.簡單的四則運算

運算子

說明

例子

輸出結果

加法

1+6

7

-

減法

30-20

10

*

乘法

20*3.1

62.0

/

除法

8.7/3

2.9

//

除法後的商數

8.7//3

2

%

除法後的餘數

8.5%3

2.5

**

次方

2**10

1024

2.變數的命令

令a=5
a = a-3
則a=2
程式是這樣子運行:指派5這個整數到a這個變數中,接著指派a-3這個運算式給變數a,則現在a=2。

接著看下面的輸出結果也會跟上述結果一樣:
a=5
a-=3
a=2
其中a-=3可以想像成:a減了某個東西,這東西在等號右方(減掉了3)。
上述的表示方法也同樣可以做+= , *= , /= , //=, %=等運算

3.進制轉換

一般我們預設的數字都是十進制的,如果要做二進制、八進制、十六進制需在數字前面加兩個字:

輸入值

說明

例子

結果

0b(+數字)

二進制

0b10

2

0o(+數字)

八進制

0o10

8

0x(+數字)

十六進制

0x10

16

4.類型轉換

有時輸出結果的變數明明是數字,但是卻無法作數值計算,很大的可能是該變數不是int,這時候type指令就很重要了。
>>>a = ‘1’
>>>type(a)
str
>>>a + 1
TypeError: can only concatenate str (not “int”) to str

如何做轉換呢?
str(a) 將a變數轉換成字串
int(a) 將a變數轉換成整數
float(a) 將a變數轉換成有小數的數字

5.字串-用/來轉義(正則表達式)

>>>a= “I have a book.\nAnd a dog.\nBut I don’t like them.\nHaha”
>>>a
Out[1]: “I have a book.\nAnd a dog.\nBut I don’t like them.\nHaha”
>>>print(a)
I have a book.
And a dog.
But I don’t like them.
Haha

注意, print(a)是印出給人類看的文字、句子

表達式

意思

\n

換行

\b

退格(BackSpace)

\’

打出單引號

\”

打出雙引號

\\

打出一個斜槓

6.字串-位置

>>>a=‘abcdefghijkl’
>>>a[0]
‘a’

>>>a[-1]
‘l’

>>>a[1:]
‘bcdefghijkl’
註:a[x:] ,x表示從哪個位置開始(含該位置)

>>>a[:6]
註:a[:y] ,y表示到哪個位置結束(不含該位置)

>>>a[1:2]
‘b’

>>a[1:6:2]
‘bdf’
註:a[x:y:z],z表示每隔z個紀錄一次

>>>a[-1]
‘l’
>>>a[-2]
‘k’
註:負號表示從後面開始算

>>>a[::-1]
‘lkjihgfedcba’

7.Split()分割

說明:使字串變成串列
>>>a=‘I have a book,a cat,a dog,an egg,a pen,and a watch.’
>>>a.split(’,’)
[‘I have a book’, ‘a cat’, ‘a dog’, ‘an egg’, ‘a pen’, ‘and a watch.’]
註:a.split(‘x’) ,以x的字串作分割。

8.join()結合

說明:將串列結合成字串
>>>a=‘I have a book,a cat,a dog,an egg,a pen,and a watch.’
>>>b=a.split(’,’)
>>>’,‘.join(b)
‘I have a book,a cat,a dog,an egg,a pen,and a watch.‘
註:‘x’.join(b),以x的字串當作各個字串間結合的橋梁

9.len()查詢字串個數

>>>a=‘abcdefg’
>>>len(a)
7
同樣地也可以查詢串列個數
>>>a=[‘I have a book’, ‘a cat’, ‘a dog’, ‘an egg’, ‘a pen’, ‘and a watch.’]
>>>len(a)
6
>>>len(a[0])
13
註:亦可查詢某個串列裡面的字數

10.replace()替換

>>>a=‘a dog and a cat’
>>>a.replace(‘dog’,‘pen’)
‘a pen and a cat’
註:a.replace(‘x’,‘y’) 將任何有出現x的字串的內容變成y的內容。
>>a.replace(‘a’,‘the’)
‘the dog thend the cthet’
註:因and 和 cat 裡也有a,所以也變成the了。

11.其他指令

startwith():
>>>a=‘abcdefg’
>>>a.startswith(‘b’)
False
>>>a.startswith(‘a’)
True
註:此為判斷字串首位字為’a’嗎?的意思

endwith():
>>>a=‘abcdefg’
>>>a.endwith(‘b’)
False

find():
>>>a=‘abcdefg’
>>>a.find(‘d’)
3
註:回傳為該字串”第一個”出現的位置
rfind():
>>>a.rfind(‘d’)
3
註:回傳為該字串”最後一個”出現的位置

count():
>>>a=‘abcdefg’
>>>a.count(‘a’)
1

strip():
>>>a=‘abcdefg’
>>>a.strip(‘a’)
‘bcdefg’
註:a.strip(‘x’),刪掉有任何x字串的內容

capitalize():
>>>a=‘a dog and a cat’
>>>a.capitalize()
‘A dog and a cat’
註:第一個字大寫

title():
>>>a=‘a dog and a cat’
>>>a.title()
‘A Dog And A Cat’
註:每一個單字的第一個字大寫

a.upper():
>>>a=‘a dog and a cat’
>>>a.upper():
‘A DOG AND A CAT’
註:全部字母大寫

lower():
>>>a=‘A Dog And A Cat’
>>>a.lower()
‘a dog and a cat’
註:全部字母小寫

swapcase():
>>>a=‘A Dog And A Cat’
>>>a.swapcase()
‘a dOG aND a cAT’
註:所有字母大寫變小寫、小寫變大寫

center():
>>>a=‘A Dog And A Cat’
>>>a.center(30)
’ A Dog And A Cat ‘
註:a.center(x)在x個空格中置中字串

ljust():
>>>a=‘A Dog And A Cat’
>>>a.ljust(30)
’ A Dog And A Cat ‘
註:a.ljust(x)靠左對齊(共x個空格)

rjust():
>>>a=‘A Dog And A Cat’
>>>a.rjust(30)
’ A Dog And A Cat ‘
註:a.rjust(x)靠右對齊(共x個空格)

適合誰

  • 正在探索 AI 工具實際應用的人
  • 想參考他人使用經驗、少踩雷的讀者