_variable = 1
variable = 1
first_variable = 1
$variable = 1
File "<ipython-input-4-1ea8277c23c2>", line 1 $variable = 1 ^ SyntaxError: invalid syntax
1variable = 1
File "<ipython-input-5-ae24453ae98c>", line 1 1variable = 1 ^ SyntaxError: invalid syntax
myname = 'Kristen'
print('My Name : ',myname)
My Name : Kristen
yourname = hisname = hername = 'Kristen' #Multiple Assignment
print('Your Name : ',yourname)
print('His Name : ',hisname)
print('Her Name : ',hername)
Your Name : Kristen His Name : Kristen Her Name : Kristen
my_age, my_gender, my_city = 80, "Female", "Taipei" #Multiple Assignment
print('Age : ',my_age)
print('Gender : ',my_gender)
print('City : ',my_city)
Age : 80 Gender : Female City : Taipei
student1_name = 'Vincent'
student2_name = 'Benson'
print('Student 1 : ',student1_name)
print('Student 2 : ',student2_name)
Student 1 : Vincent Student 2 : Benson
student1_name , student2_name = student2_name , student1_name
print('Student 1 : ',student1_name)
print('Student 2 : ',student2_name)
Student 1 : Benson Student 2 : Vincent
type(10)
int
type(3.14)
float
type(True)
bool
type(1>3)
bool
type('10')
str
type(True)
bool
type(true)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-23-83ab3fb73e0b> in <module>() ----> 1 type(true) NameError: name 'true' is not defined
type(False)
bool
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 要大寫
1 + 2
3
5 - 10
-5
4 * 6
24
3 / 2
1.5
3 // 2
1
7 % 5
2
公式: $Fahrenheit = Celsius × (9/5) + 32$
Taipei_Celsius = 19
Taipei_Fahrenheit = Taipei_Celsius * (9/5) + 32
print(Taipei_Fahrenheit)
66.2
x = 10
x < 10
False
x > 10
False
x <= 10
True
x >= 10
True
x == 10
True
x != 10
False
y = 10
y += 3
print(y)
13
y = 10
y -= 3
print(y)
7
y = 10
y *= 3
print(y)
30
y = 10
y /= 3
print(y)
3.3333333333333335
y = 10
y %= 3
print(y)
1
(True & True)
True
(True & False)
False
(False & True)
False
(False & False)
False
(True | True)
True
(True | False)
True
(False | True)
True
(False | False)
False
Type | Example |
---|---|
list | [1,2,'YA'] |
tuple | (1,2,'YA') |
dictionary | {'Key1' : 'value1', 'Key2' : 'value2'} |
set | {1,2} |
weather_list = [ 'Taipei' , 18.5 , 'rainy' ]
type(weather_list)
list
weather_list[0]
'Taipei'
weather_list[1]
18.5
weather_list[0:1]
['Taipei']
weather_list[0:3]
['Taipei', 18.5, 'rainy']
weather_list[:]
['Taipei', 18.5, 'rainy']
weather_list.append('80%') #濕度
print(weather_list)
['Taipei', 18.5, 'rainy', '80%']
weather_list.append(['Northeast',4]) #風相關[風向,風力級]
print(weather_list)
['Taipei', 18.5, 'rainy', '80%', ['Northeast', 4]]
type(weather_list[4])
list
weather_list.extend(['Kaohsiung',25.9,'sunny','69%'])
print(weather_list)
['Taipei', 18.5, 'rainy', '80%', ['Northeast', 4], 'Kaohsiung', 25.9, 'sunny', '69%']
weather_list.append(('Northwest',3)) #風相關[風向,風力級]
print(weather_list)
['Taipei', 18.5, 'rainy', '80%', ['Northeast', 4], 'Kaohsiung', 25.9, 'sunny', '69%', ('Northwest', 3)]
print(weather_list)
['Taipei', 18.5, 'rainy', '80%', ['Northeast', 4], 'Kaohsiung', 25.9, 'sunny', '69%', ('Northwest', 3)]
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)]
list
[‘North’] 後面加入一個風力 2 級 的資訊¶[ 'North' , 2 ]
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)]
weather_tuple = ( 'Taipei' , 18.5 , 'rainy' )
type(weather_tuple)
tuple
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'
city = ('Taipei','Taichung','Kaohsiung','Taipei')
city.count('Taipei')
2
city.index('Taichung')
1
weather_set = { 'Taipei' , 18.5 , 'rainy', 18.5 }
type(weather_set)
set
weather_set
{'rainy', 'Taipei', 18.5}
- 沒有按照原本輸入的順序擺放
- 相同的元素會被剔除
set1 = {1,2,3,4,6}
set2 = {2,3,4,5}
set1.difference( set2 )
{1, 6}
set1.symmetric_difference( set2 )
{1, 5, 6}
set1.intersection( set2 )
{2, 3, 4}
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 |
weather_dict = { 'Taipei':'rainy', 'Taichung':'cloudy' , 'Tainan':'sunny' , 'Kaohsiung':'sunny' }
type(weather_dict)
dict
weather_dict = { 'Taipei':'rainy', 'Taichung':'cloudy' , 'Tainan':'sunny' , 'Kaohsiung':'sunny' , 'Kaohsiung':'rainy'}
總共有兩個 'Kaohsiung' 的 Key ,因為 Key 不能重複,所以會留下後面的
print(weather_dict)
{'Taipei': 'rainy', 'Taichung': 'cloudy', 'Tainan': 'sunny', 'Kaohsiung': 'rainy'}
weather_dict = { 'Taipei':[18.5,'rainy','80%', 'Northeast', 4], 'Taichung': [21.5, 'cloudy', '73%', 'North', 2] }
print(weather_dict)
{'Taipei': [18.5, 'rainy', '80%', 'Northeast', 4], 'Taichung': [21.5, 'cloudy', '73%', 'North', 2]}
value 也可以是一個
list
weather_dict['Taipei']
[18.5, 'rainy', '80%', 'Northeast', 4]
取出 weather_dict 中台北的資訊
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 |
int('10')
10
int(3.14)
3
float('10')
10.0
float(10)
10.0
str(10)
'10'
str(3.14)
'3.14'
str(weather_list)
"['Taipei', 18.5, 'rainy', '80%', ['Northeast', 4], 'Taichung', 21.5, 'cloudy', '73%', ['North', 2], 'Kaohsiung', 25.9, 'sunny', '69%', ('Northwest', 3)]"
str(weather_tuple)
"('Taipei', 18.5, 'rainy')"
str(weather_dict)
"{'Taipei': [18.5, 'rainy', '80%', 'Northeast', 4], 'Taichung': [21.5, 'cloudy', '73%', 'North', 2]}"
list('10')
['1', '0']
list(weather_tuple)
['Taipei', 18.5, 'rainy']
list(weather_dict)
['Taipei', 'Taichung']
tuple('10')
('1', '0')
tuple(weather_list)
('Taipei', 18.5, 'rainy', '80%', ['Northeast', 4], 'Taichung', 21.5, 'cloudy', '73%', ['North', 2], 'Kaohsiung', 25.9, 'sunny', '69%', ('Northwest', 3))
string
¶'Hello' + ' ' + 'Python!'
'Hello Python!'
string
¶如果沒有用到引號,Python可能會認為是:
- 數值型的物件
- 已經被命名的物件
#數值型的物件
print("1"+"2")
print(1+2)
12 3
string
¶如果沒有用到引號,Python可能會認為是 :
- 數值型的物件
- 已經被命名的物件
#已經被命名的物件
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
string
¶{ }
與 format
,可以彈性填入字、詞my_string = "Hello {} !!".format("Hugh")
print(my_string)
Hello Hugh !!
city = 'Taipei'
print(city[0])
print(city[1])
print(city[2])
T a i
{ }
與 format
的方法,print
出 Hello 你的名字 !!¶my_string = "Hello {} !!".format(my_name)
print(my_string)
Hello Kristen !!
my_name = "Kristen"
my_string = "Hello {} !!".format(my_name)
print(my_string)
Hello Kristen !!
string
¶split
(切割)my_string = "Hello Kristen"
print (my_string.split(" "))
['Hello', 'Kristen']
print (my_string.split(" ")[0])
print (my_string.split(" ")[1])
Hello Kristen
string
¶replace
(取代)my_string = "Hello Kristen !!"
my_string = my_string.replace("Kristen","Hugh")
print(my_string)
Hello Hugh !!
split
應用¶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
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.']
print ("文章中第二個字 : ",article.split(" ")[1])
文章中第二個字 : Korea
replace
應用¶article = "美國總統川普昨天警告北韓別再挑釁,否則將面臨「戰火與怒火」回擊。對此,白宮發言人桑德斯今天聲明,川普談話的語氣與力道,白宮國安會團隊及幕僚長凱利事先都知情。"
print (article)
美國總統川普昨天警告北韓別再挑釁,否則將面臨「戰火與怒火」回擊。對此,白宮發言人桑德斯今天聲明,川普談話的語氣與力道,白宮國安會團隊及幕僚長凱利事先都知情。
article=article.replace("北韓","朝鮮民主主義人民共和國")
print (article)
美國總統川普昨天警告朝鮮民主主義人民共和國別再挑釁,否則將面臨「戰火與怒火」回擊。對此,白宮發言人桑德斯今天聲明,川普談話的語氣與力道,白宮國安會團隊及幕僚長凱利事先都知情。