python基礎語法總結(三)-- 數與字符串

python常用系統函數方法與模塊

python基礎語法總結(一)-- python類型轉換函數+文件讀寫

python基礎語法總結(二)-- 函數function

python基礎語法總結(三)-- 數與字符串

python基礎語法總結(四)-- list列表

python基礎語法總結(五)-- 字典dic + 元組tuple

python基礎語法總結(六)-- python類與OOP面向對象特性

 


一. 數

1. 基本操作

'''數型'''
# 八進制表示
a = 0o123                   # python八進制用前綴 0o表示,與C、java的前綴0稍有不同
print(a)                    # 十進制83

# 複數 complex
m = 9 + 3j                  #複數定義

# 整數相除的精度問題
print(3/2)          #小數1.5 (python3)
print(3//2)         #整數1 (python3)

# n次方
print(3**2)         # 求平方 得到9

#浮點數精度損失
print(2.3 - 1.3)    # float計算時,有可能會出現浮點數精度損失的問題

# python對大整數的支持
a = 99**99          # a特別大,已經超過了int和long的儲存範圍,但python存儲和處理都無壓力

 

2. 數的處理:math模塊

'''math模塊'''
from math import *
x = 0.5 ; y=0.5
sin(x)          # 即math.sin(),求正弦
cos(x)          # 餘弦
asin(x)         # 反正弦
acos(x)         # 反餘弦
tan(x)          # 正切
atan(x)         # 餘切
hypot(x,y)      # 直角三角形的斜邊長度
fmod(x,y)       # 求x/y的餘數
ceil(x)         # 取不小於x的最小整數
floor(x)        # 取不大於x的最大整數
fabs(x)         # 求絕對值
exp(x)          # 求e的x次冪
pow(x,y)        # 求x的y次冪
log10(x)        # 求以10位底的x的對數
sqrt(x)         # 求x的平方根
pi              # π 3.1415926...

 

3. 隨機數 random

### 隨機數
import random
print(random.random())                  # 產生 0 到 1 之間的隨機浮點數
print(random.randint(1, 100))           # 產生 1 到 100 的一個整數型隨機數  
print(random.uniform(0.1, 7.9))         # 產生  0.1 到 7.9 之間的隨機浮點數,區間可以不是整數
print(random.choice('hello world'))     # 從序列中隨機選取一個元素
print(random.randrange(1,100,5) )       # 生成從1到100的間隔爲5的隨機整數

 

二. 字符串

1. 定義

'''
字符串
'''
message = "hello world~"    #變量賦值語句,不需要var、public、int等關鍵字
a1, a2, a3 = '1', '2', '3'      # 多變量賦值
print(message)              #打印語句,本例中,控制檯輸出:hello world~

 

2. 字符串屬性 

### 字符串屬性
type(message)               #得到變量的實際數據類型,本例:str
len(message)                #返回字符串長度,本例:12

message.count('o')          #某個子字符串在其中出現的次數,未出現爲0,本例:2
message.find('w')           #某個子字符串在其中第一次出現的位置,不存在爲-1,本例:6

message.join("123")         #連接字符串,將原字符串插入參數字符串中每兩個字符之間 本例: '1hello world~2hello world~3'
str.join(message,'123')         # 連接字符串,本例: '1hello world~2hello world~3'
message + "123"             # 拼接兩個字符串,本例:'hello world~123'
message.split()             # 分割字符串,默認用空格分割,本例:['hello', 'world~']
message.split('o')              # 分割字符串, 本例:['hell', ' w', 'rld~']
message * 3                 # 複製字符串,再拼接。 本例:'hello world~hello world~hello world~'

 

3. 字符串分片

# 字符串分片(截取)
message[1:3]                # 截取第二、三個字符的新字符串
message[3:-2]               # 截取第四到倒數第三個字符串。本例:'lo worl'

 

4. 字符串判斷

### 判斷
'86GEsdEW'.isalnum()        # 檢測字符串是否只包含 0-9A-Za-z 本例:True
'86GEsd&EW'.isalnum()           # 因有'&' 本例:False
'B23dds'.isalpha()          # 檢測字符串是否僅包含 A-Za-z(純字母),本例:False
"Hello".isalpha()               # 本例True
'B23dds'.isdigit()          # 檢測字符串是否僅包含數字
'123'.isdigit()                 # 本例False
message.isspace()           # 檢測所有字符是否均爲空白,本例:False
'  '.isspace()                  # 本例True
message.istitle()           # 檢測字符串中單詞是否爲首字母大寫
message.isupper()           # 檢測字符串是否都爲大寫

'or' in message             # 判斷message中是否存在:'or'子字符串,本例:True

 

5. 大小寫轉換

### 大小寫轉換,原字符串不會變,只是返回新值
message.upper()             #字符串中字母全部轉爲大寫返回,本例:HELLO WORLD~
message.lower()             #字符串中字母全部轉爲小寫返回,本例:hello world~
message.title()             #字符串每個單詞首字母大寫,本例:Hello World~
message.capitalize()        #句子中首個單詞大寫,本例:Hello world~
message.center(30)          #將字符串居中,並使用空格填充至長度 width 的新字符串,本例:'         hello world~         '
message.swapcase()          #字符串大小寫全部翻轉,本例:hELLO wORLD~

 

6. 去除空格

### 去除空格,字符串本身沒變
str_ = ' example word    '
str_.strip()                #兩端去空格,本例:'example word'
str_.rstrip()               #右側(right)去空格,本例:' example word'
str_.lstrip()               #左側(left)去空格,本例:'example word    '

 

7. 格式化字符串

### 格式化字符串
# 在python中,可以在字符串中使用以 % 開頭的字符,使得腳本中改變字符串中的內容 #
"""
%c : 單個字符
%d : 十進制數字
%o : 八進制數字
%s : 字符串
%x : 十六進制數字,其中字母小寫
%X : 十六進制數字,其中字母大寫
"""
template = "Your name is %s"
print(template % '小明')          # 打印:Your name is 小明,
'%s is %d years old' % ('小紅', 20)       # 多個參數替換   結果:'小紅 is 20 years old'

# 原始字符串,不再轉義
s = r"c:\user\123.txt"              #字符串前加r,則字符串中轉義不再生效

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章