# Exercise
123
123
type(123)
int
"Hello World!"
'Hello World!'
type("Hello World!")
str
a = 1
print(a)
print(type(a))
1 <class 'int'>
b = "Hello"
print(b)
print(type(b))
Hello <class 'str'>
a = 1
a = 2
#1 = 2
a = 8
print(a)
b = a
print(b)
a = 7
print(a)
8 8 7
name0 = "Mary"
_nAmE_2 = "Joe"
Name0 = "John" # case sensitivity
print(name0 , _nAmE_2 , Name0)
Mary Joe John
#1name = "Mike"
#+name = "James" # + 為運算子
#name.1 = "Kate"
#for = "Cindy" # for 為保留字
a1 = 5
a2 = 10
a3 = a1 * a2
print(a3)
50
bike_time = 5
bike_speed = 10
bike_distance = bike_time * bike_speed
print(bike_distance)
car_time = 3
car_speed = 30
car_distance = car_time * car_speed
print(car_distance)
50 90
print(10 + (-5))
print(1 + 2 + 3 + 4 - 4 - 3 - 2 - 1)
print(10 * (-1))
print(10 / 3) # 會變成浮點數
print(10 % 3)
print(10 // 3)
print(10 ** (-2))
5 0 -10 3.3333333333333335 1 3 0.01
1 / 2
0.5
10 / 3
3.3333333333333335
round(10/3)
3
round(10 / 3, 2)
3.33
round(0.5)
0
round(1.5)
2
print(1.5 + 2)
print(type(1.5 + 2))
print(1.5 // 0.5) # 浮點數運算完 還是浮點數
print(type(1.5 // 0.2))
3.5 <class 'float'> 3.0 <class 'float'>
a = 10
b = -1.5
print(a > b)
print(a < b)
print(a == b)
print(a >= b)
print(a <= b)
print(a != b)
True False False True False True
(1+1 > 2) or (2 > 0)
True
not(False) and (3 > 2)
True
1 << 3
8
a = 2
a **= 2
print(a)
4
a = 3 + 10/5*2*(1+2)
a
15.0
# Exercise
2 > 3
False
not True
False
chinese = 98
english = 65
math = 87
history = 77
physics = 50
chemistry = 88
score = [98, 65, 87, 77, 50, 88]
a = []
numbers = [100, 200, 300, 400, 500]
fruit = ['apple', 'banana']
mixed = [1, 0.9, 'cat', [1, 2], fruit]
print(a,numbers, fruit, mixed)
[] [100, 200, 300, 400, 500] ['apple', 'banana'] [1, 0.9, 'cat', [1, 2], ['apple', 'banana']]
numbers = [100, 200, 300, 400, 500]
print(numbers[0])
print(numbers[1])
print(numbers[-1])
100 200 500
numbers = [100, 200, 300, 400, 500]
print(numbers)
print(numbers[1:3])
[100, 200, 300, 400, 500] [200, 300]
numbers = [100, 200, 300, 400, 500, 600, 700, 800]
print(numbers[1:8:2])
[200, 400, 600, 800]
numbers = [100, 200, 300, 400, 500, 600, 700, 800]
del[numbers[1:10:2]]
print(numbers)
print(300 in numbers)
print(400 not in numbers)
[100, 300, 500, 700] True True
a = [2, 4, 6]
b = [1, 3, 5, 7, 9]
print(a + b)
c = [1]*10
print(c)
print(len(c))
print(b)
# print(max(b))
# print(min(b))
[2, 4, 6, 1, 3, 5, 7, 9] [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] 10 [1, 3, 5, 7, 9]
a = (1, 2, -1.2, 'hi')
print(a)
print(type(a))
(1, 2, -1.2, 'hi') <class 'tuple'>
fruit = ('apple', 'banana', 'grape', 'watermelon')
fruit = fruit + ('guava', '123')
print(fruit)
('apple', 'banana', 'grape', 'watermelon', 'guava', '123')
a = (1)
print(type(a))
b = (1,)
print(type(b))
c = (1, 2)
print(type(c))
<class 'int'> <class 'tuple'> <class 'tuple'>
number = (1, 2, 3)
# number[0] = 5
number = (1, 2, 3)
number = (4, 5, 6)
print(number)
(4, 5, 6)
number = [1, 2, 3]
number[0] = 4
number[1] = 5
number[2] = 6
subject = ['Chinese', 'English', 'Math', 'History', 'Physics', 'Chemistry']
score = [98, 65, 87, 77, 50, 88]
ind = subject.index('Physics')
print("Physics score = ", score[ind])
Physics score = 50
grade = {
'Chinese' : 98,
'English' : 65,
'Math' : 87,
'History' : 77,
'Physics' : 50,
'Chemistry' : 88
}
print("Physics score = ", grade['Physics'])
Physics score = 50
grade = {
'Chinese' : 98,
'English' : 65,
}
print(grade)
grade['Math'] = 100
print(grade)
{'Chinese': 98, 'English': 65} {'Chinese': 98, 'English': 65, 'Math': 100}
print(grade)
grade['English'] = 95
print(grade)
{'Chinese': 98, 'English': 65, 'Math': 100} {'Chinese': 98, 'English': 95, 'Math': 100}
a = {
0 : 'Hi!',
'Apple' : 12.3,
}
print(a)
print(a['Apple'])
{0: 'Hi!', 'Apple': 12.3} 12.3
a = "Apple"
print(a[1])
print(a[-1])
b = 'Banana'
c = a + " " + b
print(c)
p e Apple Banana
a = input("Enter a sentence: ")
print(a)
Enter a sentence: Hi! Hi!
a = input("Enter a number: ")
print("The number you type is :", a)
print(type(a))
Enter a number: 123 The number you type is : 123 <class 'str'>
a = input("Enter a number: ")
#print(2 + a)
Enter a number: 0
a = "123"
print(type(a))
b = int(a)
print(type(b))
c = float(b)
print(type(c))
print(c)
d = str(c)
print(type(d))
print(d)
print(d[3])
<class 'str'> <class 'int'> <class 'float'> 123.0 <class 'str'> 123.0 .
print(1, 2.0, '3', 4, end='')
print(1)
1 2.0 3 41
print(1, 2.0, '3', 4, sep='' )
12.034
print(1, 2.0, "3", 4, end='!!!!!!!' )
1 2.0 3 4!!!!!!!
weather = input("Enter the weather: ")
if weather == 'rainy':
print("Bring umbrella")
print("Go out.")
Enter the weather: rainy Bring umbrella Go out.
if False:
print("In the if." )
print("Out of if.")
Out of if.
if 2 > 0:
print("True")
# print("True")
True
score = 57
if score >= 60:
print("Yeah! I pass.")
else:
print("Oh no! I fail.")
Oh no! I fail.
score = 60
if score >= 90:
print('A')
elif score >= 80:
print('B')
elif score >= 70:
print('C')
else:
print('D')
print('End')
D End
score = 70
if score >= 80:
if score >= 90:
print('A')
else:
print('B')
else:
if score >= 70:
print('C')
else:
print('D')
print('End')
C End
if 0.1*3 == 0.3:
print('True')
print(0.1*3 == 0.3)
False
if 0.1*3 - 0.3 < 10**(-6):
print('True')
True
score = 70
if score < 70:
print('D')
elif score >= 70:
print('C')
elif score >= 80:
print('B')
elif score >= 90:
print('A')
C
# Exercise
A = [-1, -10]
B = [5, -5]
C = [1, 1]
D = [-2, 3]
score = int(input("Enter your grade: "))
if score < 70:
print('D')
elif score >= 70:
print('C')
elif score >= 80:
print('B')
elif score >= 90:
print('A')
Enter your grade: 90 C
vim 就像是word、記事本一樣是一種文字編輯器 安裝插件,就可以寫非常多種的程式語言 直接在終端機下指令,編譯、執行程式或直譯
reference 為你自己學 Git - 終端機及常用指令介紹 --- 高見龍
jupyter notebook 為求方便看見運算結果可支援這麼做,但是在 .py檔,不能這麼做。
假設我有100個cell,我在第一個 cell 打 a = 1,我寫到第100個cell時,要用到a,可是我忘記a的值,我就打a,就可以輸出a的值,而不是用print()。
print(1)
print(3)
print(5)
print(7)
print(9)
1 3 5 7 9
print(1, 3, 5, 7, 9, 11)
1 3 5 7 9 11
a = 1
if a < 10:
print(a)
a += 2
print("end")
1 end
a = 1
while a < 100:
print(a, end = ' ')
a += 2
print("end")
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 end
total = 1 + 2 + 3 + 4 + 5 + 6
n = 100
iterate = 0
while iterate != n:
iterate += 1
print(iterate, end=' ')
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
n = 100
iterate = 0
total = 0
while iterate != n:
iterate += 1
total += iterate
print(total)
5050
101 % 2
1
a = 5
while a < 7:
if (a % 2 == 0):
print(a, "is even")
else:
print(a, "is odd")
a += 1
5 is odd 6 is even
n = 0
ans = 0
while n <= 1000:
if (n % 11 == 0):
ans = n
n += 1
print(ans)
990
n = 1000
flag = True
while n > 0:
if (n % 11 == 0) and flag:
print("correct", n)
flag = False
break
print(n)
n -= 1
1000 999 998 997 996 995 994 993 992 991 correct 990
n = 1000
while n > 0:
if (n % 11 == 0):
print("correct", n)
break
n -= 1
print(n)
999 998 997 996 995 994 993 992 991 990 correct 990
n = 10
while n > 0:
if n % 2 == 0:
print(n, "is even.")
n -= 1
continue
print(n, "is odd.")
n -= 1
print("End")
10 is even. 9 is odd. 8 is even. 7 is odd. 6 is even. 5 is odd. 4 is even. 3 is odd. 2 is even. 1 is odd. End
# Exercise
import random
answer = random.randint(1,100)
i = 1
while i < 10:
print(i, '*', 1, '=', i * 1, sep='', end='\t')
print(i, '*', 2, '=', i * 2, sep='', end='\t')
print(i, '*', 3, '=', i * 3, sep='', end='\t')
print(i, '*', 4, '=', i * 4, sep='', end='\t')
print(i, '*', 5, '=', i * 5, sep='', end='\t')
print(i, '*', 6, '=', i * 6, sep='', end='\t')
print(i, '*', 7, '=', i * 7, sep='', end='\t')
print(i, '*', 8, '=', i * 8, sep='', end='\t')
print(i, '*', 9, '=', i * 9, sep='', end='\n')
i += 1
1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9 2*1=2 2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 3*1=3 3*2=6 3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 4*1=4 4*2=8 4*3=12 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 6*7=42 6*8=48 6*9=54 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 7*8=56 7*9=63 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 8*9=72 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
i = 1 # 被乘數
j = 1 # 乘數
while i <= 9:
while j <= 9:
print(i, '*', j, '=', i* j, sep='', end='\t')
j += 1
print("")
j = 1
i += 1
1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9 2*1=2 2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 3*1=3 3*2=6 3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 4*1=4 4*2=8 4*3=12 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 6*7=42 6*8=48 6*9=54 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 7*8=56 7*9=63 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 8*9=72 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
n = 9
i = 1
j = 1
while i <= n:
while j <= 5:
print(i, '*', j, '=', i* j, sep='', end='\t')
j += 1
print("")
j = 1
i += 1
1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 2*1=2 2*2=4 2*3=6 2*4=8 2*5=10 3*1=3 3*2=6 3*3=9 3*4=12 3*5=15 4*1=4 4*2=8 4*3=12 4*4=16 4*5=20 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45
n = 9
i = 1
j = 1
while i <= n:
while j <= n:
print(i, '*', j, '=', i* j, sep='', end='\t')
j += 1
print("")
j = 1
i += 1
1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9 2*1=2 2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 3*1=3 3*2=6 3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 4*1=4 4*2=8 4*3=12 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 6*7=42 6*8=48 6*9=54 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 7*8=56 7*9=63 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 8*9=72 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
i = 1
j = 1
while i <= 9:
while j <= 9:
if i*j < 50:
print(i, '*', j, '=', i* j, sep='', end='\t')
j += 1
print("")
j = 1
i += 1
1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9 2*1=2 2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 3*1=3 3*2=6 3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 4*1=4 4*2=8 4*3=12 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 6*7=42 6*8=48 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45
i = 1
j = 1
while i <= 9:
while j <= 9:
if i*j > 50:
break
print(i, '*', j, '=', i* j, sep='', end='\t')
j += 1
print('')
j = 1
i += 1
1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9 2*1=2 2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 3*1=3 3*2=6 3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 4*1=4 4*2=8 4*3=12 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 6*7=42 6*8=48 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45
# Try it
range(1, 10)
range(1, 10)
type(range(1, 10))
range
for i in range(10):
print(i, end=' ')
0 1 2 3 4 5 6 7 8 9
for i in range(2, 10, 2):
print(i, end=' ')
2 4 6 8
total = 0
for i in range(101):
total += i
print(total)
5050
n = 1
total = 0
while n <= 100:
total += n
n += 1
print(total)
5050
fruit = ['apple', 'banana', 'grape', 'watermelon']
for i in fruit:
print(i, end=' ')
apple banana grape watermelon
prime = [2, 3, 5, 7, 11]
for i in prime:
print(i, end=' ')
2 3 5 7 11
fruit = 'Apple'
for i in fruit:
print(i)
A p p l e
for i in range(1000,0,-1):
if (i % 11 == 0):
print(i)
break
990
for i in range(0,11):
if(i % 2 == 0):
print(i, "is even.")
continue
print(i, "is odd.")
0 is even. 1 is odd. 2 is even. 3 is odd. 4 is even. 5 is odd. 6 is even. 7 is odd. 8 is even. 9 is odd. 10 is even.
a = [10, 2, 8, 15, 21, 1]
maximum = 0
for i in a:
if maximum < i:
maximum = i
print(maximum)
21
A = [-1, -10]
B = [5, -5]
C = [1, 1]
D = [-2, 3]
dots = [
['A', A],
['B', B],
['C', C],
['D', D]
]
for i in dots:
print(i[0], "is in", end=' ')
if i[1][0] > 0 and i[1][1] > 0:
print("I")
elif i[1][0] < 0 and i[1][1] > 0:
print("II")
elif i[1][0] < 0 and i[1][1] < 0:
print("III")
elif i[1][0] > 0 and i[1][1] < 0:
print("IV")
A is in III B is in IV C is in I D is in II
n = 10
for i in range(1, n):
for j in range(1, n):
print(i, '*', j, '=', i* j, sep='', end='\t')
print("")
1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9 2*1=2 2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 3*1=3 3*2=6 3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 4*1=4 4*2=8 4*3=12 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 6*7=42 6*8=48 6*9=54 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 7*8=56 7*9=63 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 8*9=72 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
n = 3
m = 3
table = []
for i in range(n):
table.append([0] * 3)
print(table)
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
empty = []
empty.append(1)
print(empty)
empty.append([0] * 3)
print(empty)
[1] [1, [0, 0, 0]]
n = 3
m = 3
table = []
for i in range(n):
table.append([0] * 3)
print("Initialize : ",table)
for i in range(n):
table[0][i] = i+1
table[1][i] = (i+1)**2
table[2][i] = (i+1)**3
print(table)
print(table)
Initialize : [[0, 0, 0], [0, 0, 0], [0, 0, 0]] [[1, 0, 0], [1, 0, 0], [1, 0, 0]] [[1, 2, 0], [1, 4, 0], [1, 8, 0]] [[1, 2, 3], [1, 4, 9], [1, 8, 27]] [[1, 2, 3], [1, 4, 9], [1, 8, 27]]
n = 9
m = 9
mul_table = []
for i in range(n):
mul_table.append([0] * m)
print(mul_table)
for i in range(1, 10):
for j in range(1, 10):
mul_table[i-1][j-1] = i*j
print(mul_table[i-1])
[[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]] [1, 2, 3, 4, 5, 6, 7, 8, 9] [2, 4, 6, 8, 10, 12, 14, 16, 18] [3, 6, 9, 12, 15, 18, 21, 24, 27] [4, 8, 12, 16, 20, 24, 28, 32, 36] [5, 10, 15, 20, 25, 30, 35, 40, 45] [6, 12, 18, 24, 30, 36, 42, 48, 54] [7, 14, 21, 28, 35, 42, 49, 56, 63] [8, 16, 24, 32, 40, 48, 56, 64, 72] [9, 18, 27, 36, 45, 54, 63, 72, 81]
# Exercise
# print("In single line comment")
print("Out of comment")
Out of comment
'''
print("In multiple lines comment")
print("In multiple lines comment")
'''
print("Out of comment")
Out of comment
x = 1
x = x + 1 # Increment x
bike_time = 5 # hr
bike_speed = 10 # km/hr
bike_distance = bike_time * bike_speed
print(bike_distance)
50