print("Hello")
Hello
print(物件) 是將物件印出到螢幕上的內建「函式」
"Hello" 是一個物件,型別是「字串(str)」
函式稍後會再細講,我們先來看看物件
print("Hello")
print('Hello')
print("Hello" + " " + "World")
Hello Hello Hello World
print("111"+"222")
print(111+222)
111222 333
print("3.14"+"1.59")
print(3.14+1.59)
3.141.59 4.73
print("Hello")
print(Hello) # 一個名稱叫做Hello的物件(但因為從來沒有出現過,程式不知道是什麼)
Hello
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-5-f853250e53bd> in <module>() 1 print("Hello") ----> 2 print(Hello) # 一個名稱叫做Hello的物件(但因為從來沒有出現過,程式不知道是什麼) NameError: name 'Hello' is not defined
print (10*3) # 乘法
print (10/3) # 除法
print (10%3) # 取餘數
print (10**3) # 次方
30 3.3333333333333335 1 1000
print ...)xyz = "http://blog.marsw.tw" vs.url = "http://blog.marsw.tw"Hello = "Hi"
print(Hello)
Hi
名稱 = 資料內容¶上面的語法,在Python中代表的意義是,
我們在右邊的「資料內容」存放位置貼上左邊「名稱」的標籤。
更改資料內容,就會是把這張標籤撕下來,黏到另外一個位置去。
# 用id(物件)可看到這個物件的存放位置
a=3
print(id(a))
a=4
print(id(a))
10105888 10105920

print("Hello")
print("Hello World")
print("Hello PyLadies")
Hello Hello World Hello PyLadies
greeting_word = "Hello"
print(greeting_word)
print(greeting_word+" World")
print(greeting_word+" PyLadies")
Hello Hello World Hello PyLadies
first_word=input("first_word=")
second_word=input("second_word=")
print(first_word+second_word)
first_word=Hello second_word=PyLadies HelloPyLadies
input(提示字)是輸入函式,
從程式執行畫面由鍵盤中輸入,輸入的資料會被儲存成字串型態。
而在 Line Bot 中,因為會與 Line 背後的系統串接,會是用另外的輸入方式。
print(11>8)
print(11%2==0)
True False
a = 11
b = 8
# 第1組判斷式
if (a>b) and (a%2==0):
print ("a>b , a is even")
elif (a>b):
print ("a>b , a is odd")
else:
print ("a<=b")
print ("這是判斷式外的區塊")
a>b , a is odd 這是判斷式外的區塊
=,代表的是將某個資料內容(右)貼上標籤(左),
a=3代表的是將儲存3的地方貼上標籤a,即標籤a的資料內容是3。
==,則是代表判斷左右兩邊的資料內容是否相等,
a==3代表的是標籤a的內容是否與3相等。
「縮排」(四個空白)是Python非常重要的一個觀念,
程式是用縮排,來斷定程式碼屬於那一個區塊,同時也兼顧了易讀性。
cost = 1000
gift = ""
# 第1組判斷式
if cost>=500: # 滿500,送購物袋
gift = gift+"購物袋"
# 第2組判斷式
if cost>=1000: # 滿1000折100
cost = cost-100
print(cost)
print(gift)
900 購物袋
想想看聊天機器人、Siri...,
這些是要用幾組判斷式呢?
大概有抓到機器人回話的方式了吧~
我們還剩產生回覆的程式碼。
另外,會不會覺得
「我想問」、「我有問題」、「求救」用or串接,程式碼很長呢?
my_list = ["Womany",2016,"PyLadies",2017]
print("The 2nd element is",my_list[1])
my_list.append(2016.0) # my_list = ["Womany",2016,"PyLadies",2017,2016.0]
print("The 2nd-last element is",my_list[-2])
The 2nd element is 2016 The 2nd-last element is 2017
my_string = "PyLadies Taiwan"
print("The 1st element of my_string = ",my_string[0])
print("The 8th element of my_string = ",my_string[7])
print("The last element of my_string = ",my_string[-1])
The 1st element of my_string = P The 8th element of my_string = s The last element of my_string = n
my_string = "PyLadies Taiwan"
print("Length of my_string = ",len(my_string))
print("The time 'a' appears in my_string = ",my_string.count('a'))
print("Py" in my_string)
Length of my_string = 15 The time 'a' appears in my_string = 3 True
my_list = ["Womany",2016,"PyLadies",2017,2016.0]
print("Length of my_list = ",len(my_list))
print("The time '2016' appears in my_string = ",my_list.count(2016))
print(2018 in my_list)
Length of my_list = 5 The time '2016' appears in my_string = 2 False
random工具箱randint工具:產生一隨機數字(起始值<=隨機數<=結束值)import random
my_list = ["Womany",2016,"PyLadies",2017,2016.0]
index = random.randint(0,len(my_list)-1)
print(my_list[index])
PyLadies
串列的長度是len(串列),上面的程式碼中串列長度為4,
我們要隨機產生串列中的可能,而索引是從0開始數,最後一個物件的索引是3,
所以結束值就是「串列長度-1」