Try   HackMD

Python基本認識與R的比較

LHB阿好伯, 2020/02/09

tags: R & python Python R

學習R也有三年多了
在使用的過程中也常接觸到Python
兩者語法與使用上有許多差異
這算是第一篇學習紀錄Python的文章
希望將兩者的差異進行一個比較以利後續再使用上不會有混淆的狀況

註解符號#

與R語言一樣註解可以使用井字號 #
但不同的是多了一個多行註解的方式
在上下三個單引號或是雙引號間的文字可以視為註解
為何說是視為?主要原因為三個單雙引號間文字為多行字串
沒有賦值的話就沒有任何功能所以大家都用來當作註解

""" print("A") """ #print("B") print("C")

C

變數命名

在python中變數的賦值是使用等號 = 而並非R常用的小於減號 <-
在變數的命名上為使用大小寫英文字母、底線 _ 、以及數字所組成
當然也可以用中文字但建議少用
最大的不同地方在於小數點無法作為變數使用

參考R編程風格指南中的範例R的變數命名為下方所列

  • variable.name

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    avg.clicks
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    avg_Clicks ,avgClicks

  • FunctionName

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    CalculateAvgClicks
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    calculate_avg_clicks , calculateAvgClicks

參考:google Python樣式指南

  • variablename

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Avg_Clicks ,AvgClicks
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    avg.clicks

  • FunctionName

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Calculate_Avg_Clicks()
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    calculateAvgClicks()

基本計算

四則運算

跟R平時所用的加(+)、減(-)、乘(*)和除(/)一樣

x = 2 + 4 y = x - 6 z = (y + 4 ) * x z / x

餘數和商數

與R語言不同的在於R計算餘數所使用的符號是 %%, 整除所使用的符號是 %/%

#計算9除以6的餘數 9 %% 6 #計算12除3的商數 12 %/% 3

3
4

而python餘數是使用單一個百分比符號 % 而商數是使用兩個除號 //

#計算9除以6的餘數 9 % 6 #計算12除3的商數 12 // 3

3
4

次方或平方根

R語言的次方的符號是 **^,平方根是使用函數sqrt()也可以0.5次方替代

#計算2的平方 2 ** 2 #計算2的3次方 2 ^ 3

4
8

python則只能使用兩個乘號 **

# 計算2的10次方 2 ** 10 # 計算4的平方根 4 ** 0.5

1024
2.0

或是使用 pow(x,y) 它等同於 x ** y

常用運算函數

abs() 絕對值
round() 四捨五入

指派運算子

Python 具備一種很便利的指派運算子可以讓我們的程式更簡潔
在R中是無法使用的

運算子 範例 說明
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
//= a //= b a = a//b
**= a **= b a = a**b
&= a &= b a = a & b
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
^= a ^= b a = a ^ b
>>= a >>= b a = a >> b
<<= a <<= b a = a << b
a = 10 a = a + 10 a =+ 10

多重賦值

在R與Python中都可以進行多個變數同時賦值
例如R

a <- b <- c <- 10 a b c

10
10
10

Python

a = b = c = 10 a b c

10
10
10

在Python中甚至可以一次給不同變數賦予不同的值

a, b, c = 10, 20, 30 a b c

10
20
30

也可以利用這特性來進行變數的交換

a, b, c = c, b, a a b c

30
20
10

物件變數

若要查詢變數R語言可以使用 ls() 取得
而Python似乎可以使用globals(),locals(),vars()和dir()這類內建的函數取得變量列表但使用過後就發現會出現許多不需要的變數資料
但在Jupyter中可以使用特殊的魔術指令 %who 就可以取得所有變數名稱

在R語言中刪除特定變數使用的是rm("變數名稱")
在python中則是使用 del 變數名稱

物件種類-type()

python利用type()函數可以查看變數資料類型
而R語言中則是使用class()函數

參考資料與書籍

Python入門邁向高手之路王者歸來
洪錦魁 , 2017/12/21

R語言:邁向Big Data之路


洪錦魁

-Python網路免費基礎資料與書單

全文分享至

https://www.facebook.com/LHB0222/

有疑問想討論的都歡迎於下方留言

喜歡的幫我分享給所有的朋友 \o/

有所錯誤歡迎指教