Python 100天-從新手到大師學習筆記Day02:語言元素

變數

進行數學計算

a = 321
b = 123
print(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為整數型態
用占位符號格式化輸出的字串
"""

使用type()

"""
使用type()檢查變數的類型
"""

運算符號

a = 5
b = 10
c = 3
d = 4
e = 5
a += b #a=a+b
a -= c #a=a-c
a *= d #a=a*d
a /= e #a=a/e
print("a = ", a)

華氏溫度轉攝氏溫度

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

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

import math

判斷年份是否為閏年

year = int(input('輸入年份: '))

is_leap = (year % 4 == 0 and year % 100 != 0 or
year % 400 == 0)
print(is_leap)

字串進階

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

Machine Learning / Deep Learning / Python / Flutter cakeresume.com/yanwei-liu

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store