# 變數

`a = 321b = 123print(a + b)print(a - b)print(a * b)print(a / b)print(a // b)print(a % b)print(a ** b)`

# 使用input()和int()

`"""使用input輸入函數使用int()轉換成int  #int為整數型態用占位符號格式化輸出的字串"""a = int(input('a = '))b = int(input('b = '))print('%d + %d = %d' % (a, b, a + b))print('%d - %d = %d' % (a, b, a - b))print('%d * %d = %d' % (a, b, a * b))print('%d / %d = %f' % (a, b, a / b))print('%d // %d = %d' % (a, b, a // b))print('%d %% %d = %d' % (a, b, a % b))print('%d ** %d = %d' % (a, b, a ** b))`

# 使用type()

`"""使用type()檢查變數的類型"""a = 100b = 12.345c = 1 + 5jd = 'hello, world'e = Trueprint(type(a))print(type(b))print(type(c))print(type(d))print(type(e))int()：將數值或字串轉為整數float()：將字串轉成浮點數(即有小數點的數字)str()：轉成字串。(可用於將random模組產生出的數字轉為字串，方便翻頁爬蟲)`

# 運算符號

`a = 5b = 10c = 3d = 4e = 5a += b            #a=a+ba -= c            #a=a-ca *= d            #a=a*da /= e            #a=a/eprint("a = ", a)flag1 = 3 > 2flag2 = 2 < 1flag3 = flag1 and flag2flag4 = flag1 or flag2flag5 = not flag1print("flag1 = ", flag1)print("flag2 = ", flag2)print("flag3 = ", flag3)print("flag4 = ", flag4)print("flag5 = ", flag5)print(flag1 is True)print(flag2 is not False)`

# 華氏溫度轉攝氏溫度

`f = float(input('輸入華氏溫度: '))c = (f - 32) / 1.8print('%.1f華氏度 = %.1f攝氏度' % (f, c))`

# 輸入圓半徑計算周長和面積

`import mathradius = float(input('輸入圓的半徑: '))perimeter = 2 * math.pi * radiusarea = math.pi * radius * radiusprint('周長: %.2f' % perimeter)print('面積: %.2f' % area)`

# 判斷年份是否為閏年

`year = int(input('輸入年份: '))is_leap = (year % 4 == 0 and year % 100 != 0 or           year % 400 == 0)print(is_leap)#印出True或者False`

# 字串進階

`str1 = 'hello, world!'print('字串長度:', len(str1))print('首字母大寫: ', str1.title())print('字串變大寫: ', str1.upper())# str1 = str1.upper()print('字串是不是大寫: ', str1.isupper())print('字串是不是以hello開頭: ', str1.startswith('hello'))print('字串是不是以hello結尾: ', str1.endswith('hello'))print('字串是不是以驚嘆號開頭: ', str1.startswith('!'))print('字串是不是以驚嘆號結尾: ', str1.endswith('!'))str2 = '- \u9a86\u660a'str3 = str1.title() + ' ' + str2.lower()print(str3)`

Written by