標籤:woe 字串格式化 oct bsp int for 基本 put bre
print(‘‘‘
hello word
‘‘‘)
hello word
print (" hello \nwoerd")
hello
woed
print("hello \\nword ")
hello \nword
print (r"hello word\n")
hello work\n
a="dogs"
print ("hello %s" %a)
hello dogs
a=123
print("hello %d " %a)
heelo 123
a=123
print ("hello %i" %a)
hello 123
字串格式化
a=123
printf (hello %d " %a)
hello 123
a=123
print("hello %i " %a)
hello 123
a=123
print ("hello %s " %a)
hello 123
運算子
10%20
10
2**8
256
12>>2
3
12>>3
1
進位轉換
二進位轉換
a=123
b=bin(a)
print(b)
ob1111011
a=123
b=oct(a)
print (b)
0o173
a=123
b=hex(a)
print(b)
ox7b
轉換二進位
a=‘0b1111011‘
b=int (a,base=2)
print (b)
123
a=‘ob1111011‘
b=int(a,base=2)
c=oct(b)
print(c)
0o173
八進位轉換
a=‘0o173‘
b=int(a,base=8)
print(b)
123
a=‘0o173‘
b=int(a ,base=8)
c=bin(b)
print(c)
ob1111011
a=‘0o173‘
b=int (a,base=8)
c=hex(b)
print(c)
ox7b
a=123
a=b=c=456
print(a,b,c)
456 456 456
if 3>4
print("牛逼")
else;
print ("不牛逼")
不牛逼
a=input(“請輸入的數字”)
print(“輸入的數字 %s " %a)
請輸入的數字123
輸入的數字123
求100內的奇數之和 偶數之和
odd=0
oven=0
for i in range (1,101):
if 1%2:
odd=odd+i
else:
oven=oven+i
print(odd)
print(oven)
二分法實現
a=input("請輸入一個五位元:")
a=int (a)
if a > 99:
if a > 9999:
print("5位元")
if a > 999
print("4位元")
else:
print("3位元")
else:
print("2位元") if a > 9 else print ("1位元")
list range(1,20,2)
1,3,5,7,9,11,13,15,17,19
### continue,,執行continux之後,立刻執行本次迴圈後面的
for i in range (5)
if i ==3
prinf(i)
0 1 2 4 5
###break,,執行了break之後表示跳出迴圈,迴圈終止
for i in range (10):
if i == 4 :
break
print(i)
0 1 2 3 4
python 基本語句