Python × 資料分析

Basic


Kristen Chan

Agenda


  • 變數命名規則 (Variable Name Rules)
  • 變數賦值 (Assignment Statements)
  • 交換變數 (Swap variables)
  • 資料型態 (Data Type)
  • 運算子 (Operator)
  • 資料型態轉換 (Data Type Conversion)

Variable Name Rules

Variable Name Rules


  • First character

    A letter (a - z, A - B) or underscore ( _ )
  • Other characters

    Letters (a - z, A - B) , numbers (0-9) or ( _ )
  • No

    : !, @, #, $, %, - etc.
  • Reserved words 保留字

Variable Name Rules


Reserved words

Falseclassfinallyisreturn
Nonecontinueforlambdatry
Truedeffromnonlocalwhile
anddelglobalnotwith
aselififoryield
assertelseimportpass
breakexceptinraise

Variable Name Rules


Good

In [1]:
_variable = 1 
In [2]:
variable = 1 
In [3]:
first_variable = 1

Bad

In [4]:
$variable = 1
  File "<ipython-input-4-1ea8277c23c2>", line 1
    $variable = 1
    ^
SyntaxError: invalid syntax
In [5]:
1variable = 1
  File "<ipython-input-5-ae24453ae98c>", line 1
    1variable = 1
            ^
SyntaxError: invalid syntax

Assignment Statements

Assignment Statements


  • Single assignment
  • Multiple Assignment

Assignment Statements


Single assignment

In [6]:
myname = 'Kristen'
In [7]:
print('My Name : ',myname)
My Name :  Kristen

Assignment Statements


Multiple Assignment

In [8]:
yourname = hisname = hername = 'Kristen' #Multiple Assignment
In [9]:
print('Your Name : ',yourname)
print('His Name : ',hisname)
print('Her Name : ',hername)
Your Name :  Kristen
His Name :  Kristen
Her Name :  Kristen
In [10]:
my_age, my_gender, my_city = 80, "Female", "Taipei" #Multiple Assignment
In [11]:
print('Age : ',my_age)
print('Gender : ',my_gender)
print('City : ',my_city)
Age :  80
Gender :  Female
City :  Taipei

Swap variables

Swap variables


In [12]:
student1_name = 'Vincent'
In [13]:
student2_name = 'Benson'
In [14]:
print('Student 1 : ',student1_name)
print('Student 2 : ',student2_name)
Student 1 :  Vincent
Student 2 :  Benson

Swap

In [15]:
student1_name , student2_name = student2_name , student1_name
In [16]:
print('Student 1 : ',student1_name)
print('Student 2 : ',student2_name)
Student 1 :  Benson
Student 2 :  Vincent

Data Type

Data Type


  • 數值型態(Numeric type)
  • 字串型態(String type)

Data Type


TpyeExample
int10
float3.14
booleanTrue , False
string'Apple'

Data Type


type( x ) : 使用 type 來得知 x 的型態

  • 數值型態(Numeric type)
In [17]:
type(10)
Out[17]:
int
In [18]:
type(3.14)
Out[18]:
float
In [19]:
type(True)
Out[19]:
bool
In [20]:
type(1>3)
Out[20]:
bool

Data Type


type( x ) : 使用 type 來得知 x 的型態

  • 字串型態(String type)
In [21]:
type('10')
Out[21]:
str

Data Type


!注意

In [22]:
type(True)
Out[22]:
bool
In [23]:
type(true)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-23-83ab3fb73e0b> in <module>()
----> 1 type(true)

NameError: name 'true' is not defined
In [24]:
type(False)
Out[24]:
bool
In [25]:
type(false)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-25-a2d9c628763d> in <module>()
----> 1 type(false)

NameError: name 'false' is not defined

Boolean : True / False 的 T 和 F 要大寫

Operator

Operator


  • 算術運算子 (Arithmetic Operator)
  • 關係運算子 (Comparison Operator)
  • 指派運算子 (Assignment Operator)
  • 邏輯運算子 (Logical Operator)

Operator


Arithmetic Operator

運算子功能
加法運算
減法運算/負號
*乘法運算
**指數運算
/除法運算
//整除
%取餘數

Operator


Arithmetic Operator

In [26]:
1 + 2
Out[26]:
3
In [27]:
5 - 10
Out[27]:
-5
In [28]:
4 * 6
Out[28]:
24
In [29]:
3 / 2
Out[29]:
1.5
In [30]:
3 // 2 
Out[30]:
1
In [31]:
7 % 5
Out[31]:
2

Exercise

Q. 現在台北溫度是 19$℃$,請將$℃$ 轉成 $℉$

公式: $Fahrenheit = Celsius × (9/5) + 32$

Answers

In [32]:
Taipei_Celsius = 19

Taipei_Fahrenheit = Taipei_Celsius * (9/5) + 32

print(Taipei_Fahrenheit)
66.2

Operator


Comparison Operator

運算子說明
$<$小於
$>$大於
$<=$小於等於
$>=$大於等於
$==$等於
$!=$不等於

Operator


Comparison Operator

In [33]:
x = 10
In [34]:
x < 10
Out[34]:
False
In [35]:
x > 10
Out[35]:
False
In [36]:
x <= 10
Out[36]:
True
In [37]:
x >= 10
Out[37]:
True
In [38]:
x == 10
Out[38]:
True
In [39]:
x != 10
Out[39]:
False

Operator


Assignment Operator

運算子說明
$=$將右邊的運算結果,指定給左邊的變數
$+=$將左邊變數值加右邊的值,再指定給左邊的變數
$-=$將左邊變數值減右邊的值,再指定給左邊的變數
$*=$將左邊變數值乘右邊的值,再指定給左邊的變數
$/=$將左邊變數值除右邊的值,再指定給左邊的變數
% $=$將左邊變數值除右邊的值,再指定餘數給左邊的變數

Operator


Assignment Operator

In [40]:
y = 10
y += 3
print(y)
13
In [41]:
y = 10
y -= 3
print(y)
7
In [42]:
y = 10
y *= 3
print(y)
30
In [43]:
y = 10
y /= 3
print(y)
3.3333333333333335
In [44]:
y = 10
y %= 3
print(y)
1

Operator


Logical Operator

運算子功能
&且 ( And )
$|$或 ( Or )

Operator


Logical Operator

In [45]:
(True & True)
Out[45]:
True
In [46]:
(True & False)
Out[46]:
False
In [47]:
(False & True)
Out[47]:
False
In [48]:
(False & False)
Out[48]:
False

Operator


Logical Operator

In [49]:
(True | True)
Out[49]:
True
In [50]:
(True | False)
Out[50]:
True
In [51]:
(False | True)
Out[51]:
True
In [52]:
(False | False)
Out[52]:
False

Data Type II

Data Type II


  • 容器型態(Container type)

Data Type II


TypeExample
list[1,2,'YA']
tuple(1,2,'YA')
dictionary{'Key1' : 'value1', 'Key2' : 'value2'}
set{1,2}

Data Type II


list

  • [ ]一串以 , 分開的值包起來
    e.g. my_list = [ 1 , 2 , ’YA’ ]
  • list 中的元素不必放相同的資料型態 (int, float, boolean, string)
  • Python index 從 0 開始
    e.g. my_list[ 0 ] = 1

Data Type II


list

In [53]:
weather_list = [ 'Taipei' , 18.5 , 'rainy' ]
In [54]:
type(weather_list)
Out[54]:
list
In [55]:
weather_list[0]
Out[55]:
'Taipei'
In [56]:
weather_list[1]
Out[56]:
18.5
In [57]:
weather_list[0:1]
Out[57]:
['Taipei']
In [58]:
weather_list[0:3]
Out[58]:
['Taipei', 18.5, 'rainy']
In [59]:
weather_list[:]
Out[59]:
['Taipei', 18.5, 'rainy']

Data Type II


list

  • list.append( x )
    list 後面加入一個新的元素 x,直接放入該元素

  • list.extend( x )
    list 後面加入一個新的元素 x,先將元素展開再放入

元素 x : int , string , list , tuple

Data Type II


list

list.append( x )

In [60]:
weather_list.append('80%') #濕度
print(weather_list)
['Taipei', 18.5, 'rainy', '80%']
In [61]:
weather_list.append(['Northeast',4]) #風相關[風向,風力級]
print(weather_list)
['Taipei', 18.5, 'rainy', '80%', ['Northeast', 4]]
In [62]:
type(weather_list[4])
Out[62]:
list

Data Type II


list

list.extend( x )

In [63]:
weather_list.extend(['Kaohsiung',25.9,'sunny','69%'])
print(weather_list)
['Taipei', 18.5, 'rainy', '80%', ['Northeast', 4], 'Kaohsiung', 25.9, 'sunny', '69%']
In [64]:
weather_list.append(('Northwest',3)) #風相關[風向,風力級]
print(weather_list)
['Taipei', 18.5, 'rainy', '80%', ['Northeast', 4], 'Kaohsiung', 25.9, 'sunny', '69%', ('Northwest', 3)]

Data Type II


list

  • list.insert( 索引位置 , x )
    新增元素 x ,並放在指定的索引位置之前

元素 x : int , string , list , tuple

Data Type II


list

list.insert

In [65]:
print(weather_list)
['Taipei', 18.5, 'rainy', '80%', ['Northeast', 4], 'Kaohsiung', 25.9, 'sunny', '69%', ('Northwest', 3)]
In [66]:
weather_list.insert(5,'Taichung') 
weather_list.insert(6,21.5) 
weather_list.insert(7,'cloudy')
weather_list.insert(8,'73%')
weather_list.insert(9,['North'])
print(weather_list)
['Taipei', 18.5, 'rainy', '80%', ['Northeast', 4], 'Taichung', 21.5, 'cloudy', '73%', ['North'], 'Kaohsiung', 25.9, 'sunny', '69%', ('Northwest', 3)]

Exercise

Q. 請將 weather_list[9] 中的 list [‘North’] 後面加入一個風力 2 級 的資訊

[ 'North' , 2 ]

Answers

In [67]:
weather_list[9].insert(len(weather_list[9]),2)
print(weather_list)
['Taipei', 18.5, 'rainy', '80%', ['Northeast', 4], 'Taichung', 21.5, 'cloudy', '73%', ['North', 2], 'Kaohsiung', 25.9, 'sunny', '69%', ('Northwest', 3)]

Data Type II


tuple

  • ( )一串以 , 分開的值包起來
    e.g. my_tuple = ( 1 , 2 , ’YA’ )
  • tuple 中的元素不必放相同的資料型態 (int, float, boolean, string)
  • tuplelist 很像
  • tuple 不能新增、刪除、修改

Data Type II


tuple

In [68]:
weather_tuple = ( 'Taipei' , 18.5 , 'rainy' )
In [69]:
type(weather_tuple)
Out[69]:
tuple
In [70]:
weather_tuple.append('80%') #濕度
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-70-935cda1f4aac> in <module>()
----> 1 weather_tuple.append('80%') #濕度

AttributeError: 'tuple' object has no attribute 'append'

Data Type II


tuple

  • tuple.count( x )
    元素 x 在 tuple 中出現幾次

  • tuple.index( x )
    元素 x 在 tuple 的索引位置是哪裡
In [71]:
city = ('Taipei','Taichung','Kaohsiung','Taipei')
In [72]:
city.count('Taipei')
Out[72]:
2
In [73]:
city.index('Taichung')
Out[73]:
1

Data Type II


set

  • { }一串以 , 分開的值包起來
    e.g. my_set = { 1 , 2 , ’YA’ }
  • set 中的元素不必放相同的資料型態 (int, float, boolean, string)
  • 沒有順序性
  • set 中的元素是不重複

Data Type II


set

In [74]:
weather_set = { 'Taipei' , 18.5 , 'rainy', 18.5 }
In [75]:
type(weather_set)
Out[75]:
set
In [76]:
weather_set
Out[76]:
{'rainy', 'Taipei', 18.5}
  1. 沒有按照原本輸入的順序擺放
  2. 相同的元素會被剔除

Data Type II


set

  • set1.difference( set2 )
    找出 set1 中不存在 set2 的元素

  • set1.symmetric_difference( set2 )
    找出兩個 set 中不同的元素

  • set1.intersection( set2 )
    找出兩個 set 中相同的元素

Data Type II


set

set1.difference( set2 )

In [77]:
set1 = {1,2,3,4,6}
set2 = {2,3,4,5}
In [78]:
set1.difference( set2 )
Out[78]:
{1, 6}

Data Type II


set

set1.symmetric_difference( set2 )

set1 = {1,2,3,4,6}
set2 = {2,3,4,5}
In [79]:
set1.symmetric_difference( set2 )
Out[79]:
{1, 5, 6}

Data Type II


set

set1.intersection( set2 )

set1 = {1,2,3,4,6}
set2 = {2,3,4,5}
In [80]:
set1.intersection( set2 )
Out[80]:
{2, 3, 4}

Note

比較兩個 set

Method說 明
`set1.intersectionset2)`set1 & set2
`set1.union(set2)`set1 $|$ set2
`set1.symmetric_difference(set2)`set1 ^ set2
`set1.difference(set2)`set1 $-$ set2
`set1.issubset(set2)`set1 $<=$ set2
`set1.issuperset(set2)`set1 $>=$ set2

Data Type II


dictionary

  • { }一群由 Key,Value 配對組合而成的東西包起來
    e.g. my_dict = { 'Key1':'value1','Key2':'value2' }
  • Key 有大小寫之分
  • 不必放相同的資料型態
  • Key 不能重複,否則會被後面取代
  • 沒有順序之分

Data Type II


dictionary

In [81]:
weather_dict = { 'Taipei':'rainy', 'Taichung':'cloudy' , 'Tainan':'sunny' , 'Kaohsiung':'sunny' }
In [82]:
type(weather_dict)
Out[82]:
dict
In [83]:
weather_dict = { 'Taipei':'rainy', 'Taichung':'cloudy' , 'Tainan':'sunny' , 'Kaohsiung':'sunny' , 'Kaohsiung':'rainy'}

總共有兩個 'Kaohsiung' 的 Key ,因為 Key 不能重複,所以會留下後面的

In [84]:
print(weather_dict)
{'Taipei': 'rainy', 'Taichung': 'cloudy', 'Tainan': 'sunny', 'Kaohsiung': 'rainy'}

Data Type II


dictionary

In [85]:
weather_dict = { 'Taipei':[18.5,'rainy','80%', 'Northeast', 4], 'Taichung': [21.5, 'cloudy', '73%', 'North', 2] }
In [86]:
print(weather_dict)
{'Taipei': [18.5, 'rainy', '80%', 'Northeast', 4], 'Taichung': [21.5, 'cloudy', '73%', 'North', 2]}

value 也可以是一個 list

In [87]:
weather_dict['Taipei']
Out[87]:
[18.5, 'rainy', '80%', 'Northeast', 4]

取出 weather_dict 中台北的資訊

Data Type Conversion

Data Type Conversion


Type說明
int(x) string , float ➜ int
float(x) string , int ➜ float
str(x) int , float , list , tuple , dict ➜ string
list(x) string , tuple, dict ➜ list
tuple(x) string , list ➜ tuple

Data Type Conversion


int(x) : 將 x 轉成 int

In [88]:
int('10')
Out[88]:
10
In [89]:
int(3.14)
Out[89]:
3

Data Type Conversion


float(x) : 將 x 轉成 float

In [90]:
float('10')
Out[90]:
10.0
In [91]:
float(10)
Out[91]:
10.0

Data Type Conversion


str(x) : 將 x 轉成 str

In [92]:
str(10)
Out[92]:
'10'
In [93]:
str(3.14)
Out[93]:
'3.14'
In [94]:
str(weather_list)
Out[94]:
"['Taipei', 18.5, 'rainy', '80%', ['Northeast', 4], 'Taichung', 21.5, 'cloudy', '73%', ['North', 2], 'Kaohsiung', 25.9, 'sunny', '69%', ('Northwest', 3)]"
In [95]:
str(weather_tuple)
Out[95]:
"('Taipei', 18.5, 'rainy')"
In [96]:
str(weather_dict)
Out[96]:
"{'Taipei': [18.5, 'rainy', '80%', 'Northeast', 4], 'Taichung': [21.5, 'cloudy', '73%', 'North', 2]}"

Data Type Conversion


list(x) : 將 x 轉成 list

In [97]:
list('10')
Out[97]:
['1', '0']
In [98]:
list(weather_tuple)
Out[98]:
['Taipei', 18.5, 'rainy']
In [99]:
list(weather_dict)
Out[99]:
['Taipei', 'Taichung']

Data Type Conversion


tuple(x) : 將 x 轉成 tuple

In [100]:
tuple('10')
Out[100]:
('1', '0')
In [101]:
tuple(weather_list)
Out[101]:
('Taipei',
 18.5,
 'rainy',
 '80%',
 ['Northeast', 4],
 'Taichung',
 21.5,
 'cloudy',
 '73%',
 ['North', 2],
 'Kaohsiung',
 25.9,
 'sunny',
 '69%',
 ('Northwest', 3))

Note

string

  • 字串可以相加( + ),不能相減( - )
In [102]:
'Hello' + ' ' + 'Python!'
Out[102]:
'Hello Python!'

Note

string

  • 字串可以相加( + ),不能相減( - )

如果沒有用到引號,Python可能會認為是:

  1. 數值型的物件
  2. 已經被命名的物件
In [103]:
#數值型的物件
print("1"+"2")
print(1+2)
12
3

Note

string

  • 字串可以相加( + ),不能相減( - )

如果沒有用到引號,Python可能會認為是 :

  1. 數值型的物件
  2. 已經被命名的物件
In [104]:
#已經被命名的物件
print("Hi")
print(Hi)
Hi
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-104-4d55786baeaf> in <module>()
      1 #已經被命名的物件
      2 print("Hi")
----> 3 print(Hi)

NameError: name 'Hi' is not defined

Note

string

  • 利用 { }format ,可以彈性填入字、詞
In [105]:
my_string = "Hello {} !!".format("Hugh")
print(my_string)
Hello Hugh !!
  • 字串是由一個個字母組成的 list
In [106]:
city = 'Taipei'
print(city[0])
print(city[1])
print(city[2])
T
a
i

Exercise

Q. 創建一個 my_name 變數,並存入自己的名字,接著利用 { }format 的方法,printHello 你的名字 !!

my_string = "Hello {} !!".format(my_name)
print(my_string)
Hello Kristen !!

Answers

In [107]:
my_name = "Kristen"
my_string = "Hello {} !!".format(my_name)
print(my_string)
Hello Kristen !!

Note

string

  • split (切割)
In [108]:
my_string = "Hello Kristen"
print (my_string.split(" "))
['Hello', 'Kristen']
In [109]:
print (my_string.split(" ")[0])
print (my_string.split(" ")[1])
Hello
Kristen

Note

string

  • replace (取代)
In [110]:
my_string = "Hello Kristen !!"
my_string = my_string.replace("Kristen","Hugh")
print(my_string)
Hello Hugh !!

Note

split 應用

In [111]:
article = "North Korea is seriously examining a plan to launch a missile strike targeting an area near the US territory of Guam in response to US President Donald Trump's warning to Pyongyang that any additional threats will be met with fire and fury, according to a new statement from Gen. Kim Rak Gyom published by state-run media KCNA Thursday."

取出一篇文章中的單字

http://edition.cnn.com/2017/08/09/politics/north-korea-considering-near-guam-strike/index.html
In [112]:
print (article.split(" "))
['North', 'Korea', 'is', 'seriously', 'examining', 'a', 'plan', 'to', 'launch', 'a', 'missile', 'strike', 'targeting', 'an', 'area', 'near', 'the', 'US', 'territory', 'of', 'Guam', 'in', 'response', 'to', 'US', 'President', 'Donald', "Trump's", 'warning', 'to', 'Pyongyang', 'that', 'any', 'additional', 'threats', 'will', 'be', 'met', 'with', 'fire', 'and', 'fury,', 'according', 'to', 'a', 'new', 'statement', 'from', 'Gen.', 'Kim', 'Rak', 'Gyom', 'published', 'by', 'state-run', 'media', 'KCNA', 'Thursday.']
In [113]:
print ("文章中第二個字 : ",article.split(" ")[1])
文章中第二個字 :  Korea

Note

replace 應用

In [114]:
article = "美國總統川普昨天警告北韓別再挑釁,否則將面臨「戰火與怒火」回擊。對此,白宮發言人桑德斯今天聲明,川普談話的語氣與力道,白宮國安會團隊及幕僚長凱利事先都知情。"
In [115]:
print (article)
美國總統川普昨天警告北韓別再挑釁,否則將面臨「戰火與怒火」回擊。對此,白宮發言人桑德斯今天聲明,川普談話的語氣與力道,白宮國安會團隊及幕僚長凱利事先都知情。
In [116]:
article=article.replace("北韓","朝鮮民主主義人民共和國")
print (article)
美國總統川普昨天警告朝鮮民主主義人民共和國別再挑釁,否則將面臨「戰火與怒火」回擊。對此,白宮發言人桑德斯今天聲明,川普談話的語氣與力道,白宮國安會團隊及幕僚長凱利事先都知情。