單行註解請使用#
多行註解請使用' ' '(三個單引號)
#註解
'''
2_1.輸出語句
'''
程式
temp = 1
print( temp )
輸出
1
程式
temp = 1
temp = 2
print( temp )
輸出
2
宣告 num1 為 10 整數
宣告num2 為 10.01 浮點數
宣告num3 為 true 布林值
宣告text 為 'DICE' 字串
num1 = 10
num2 = 10.01
num3 = True
text = 'DICE'
print(type(num1))
print(type(num2))
print(type(num3))
print(type(text))
<class 'int'>
<class 'float'>
<class 'bool'>
<class 'str'>
int表示整數
float表示淨點數
bool表示布林值,真假值
str表示字串
宣告a 為 使用者輸入的數字
新的a = 右邊先算完蓋掉原本的a
所以a會得到+10之後的值
a = int(input("請輸入一個數字:"))
a = a + 10
print("a變為{0}".format(a))
請輸入一個數字: 3
a變為13
字串內指定第n個 =>str[ n-1]
str = "Hello, Word!"
print( str[0] ) #印出H
print( str[1] ) #印出e
2. 字串內指定第n個 至第m個 => str[n:m]
text = "I love you"
textall = text.split(" ") #分割子字串
text0 = text.split(" ")[0] #印出索引0的子字串
text1 = text.split(" ")[1] #印出索引1的子字串
text2 = text.split(" ")[2] #印出索引2的子字串
text = "I love you"
['I', 'love', 'you']
I
love
you
4. 字串變數為 Hello, World,再將其倒印 =>print(str[::-1])
a1=input("請輸入一個整數:")
print(type(a1))
a1=int(a1) #轉整數
print(type(a1))
a2=float(a2) #轉浮點數
print(type(a2))
請輸入一個整數:5
<class 'str'>
<class 'int'>
<class 'float'>
print("Height: 158CM",end=" ")
print("Weight: 55.66KG")
Height: 158CM Weight: 55.66KG
A = "資一甲"
B = "陳玉曄"
print("班級:{0},老師: {1}".format(A, B)) #{0}的位置會換成A
a= input("輸入文字") #輸入時預設就是文字
b= int(input("輸入數字B")) #若要改成數字做運算,就要用int( )函數做轉換
c = a*b #aXb
c= a/b #a除b
c= a//b #a/b的商
c = a%b #a/b的餘數
c= a**b #a的b次方 , a**2表示a的平方
c= a**0.5 #a開根號
print(3<5) #小於
print(3>5) #小於
print(3!=5) #不等於
print(3==3) #是不是等於
score = int(input())
if score >= 60 : #>=是條件
print("{0}分及格".format( score ))
if 條件式:
print("{0}分及格".format(score))
else:
print("{0}分不及格".format(score))