python 基本語句

2022-03-22 11:39:03 字數 2236 閱讀 9373

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

2**8

12>>2

12>>3

進製轉換

二進位制轉換

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)

a='ob1111011'

b=int(a,base=2)

c=oct(b)

print(c)

0o173

八進位制轉換

a='0o173'

b=int(a,base=8)

print(b)

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最基本的語句 Python基本語句

輸入 python中提供了input 來讓使用者輸入字串並儲存到乙個變數裡 比如 可以看到,input 將使用者輸入的封裝成了乙個字串 input內可以放入字串當作提示內容 如下所示 輸出用print 在括號中加入字串,就可以在螢幕上輸出指定的文字。如下 也可以在裡面放入多個字串,用逗號 隔開 比如...

python基本迴圈語句

if else迴圈語句 if 判斷條件 執行語句 if 判斷條件 執行語句 elif 判斷條件 執行語句 else pass 三目運算 result1 if 判斷條件 else result2 while迴圈語句 while 判斷條件 執行語句 while 判斷條件 執行語句 else 執行語句 w...

Python基本語法 迴圈語句

學習筆記以備日後溫習 python3入門與高階 分支迴圈條件與列舉 包 模組 函式 功能 輸入賬號密碼完成開鎖 account qiyue password 123456 print please input account user account input print please input...