Python基礎學習筆記記錄

2021-10-10 22:46:51 字數 2870 閱讀 7388

條件判斷語句

4.迴圈語句

單行注釋:

# 這是單行注釋

print(

"這是單行注釋"

)

多行注釋:

''

'print("這是多行注釋")

print("這是多行注釋")

print("這是多行注釋")

'''

中文注釋:

若在程式中用到了中文,直接執行輸出,程式會出錯。需在程式的開頭寫入如下**:

# -*- coding=utf-8 -*-
import keyword

print

(keyword.kwlist)

結果:

[『false』, 『none』, 『true』, 『and』, 『as』, 『assert』, 『break』, 『class』, 『continue』, 『def』, 『del』, 『elif』, 『else』, 『except』, 『finally』, 『for』, 『from』, 『global』, 『if』, 『import』, 『in』, 『is』, 『lambda』, 『nonlocal』, 『not』, 『or』, 『pass』, 『raise』, 『return』, 『try』, 『while』, 『with』, 『yield』]

# 這是普通輸出

print

("普通輸出"

)

# 格式化輸出

year =

2020

month =

11day =

28print

("今年是%d年"

% year)

print

("今天是%d年%d月%d日"

%(year, month, day)

)

# 換行輸出

print

("今年是%d年\n今天是%d年%d月%d日"

%(year, year, month, day)

)

if

true

:print

("true"

)else

:print

("false"

)

import random

while

true

: player_in =

int(

input

("--石頭剪刀布小遊戲--\n請出拳 [剪刀(0)石頭(1)布(2)]\n"))

while player_in not

inrange(0

,3):

print

('輸入錯誤,請重新輸入!'

) player_in =

int(

input()

) computer_in = random.randint(0,

2)if computer_in ==0:

computer =

'剪刀'

elif computer_in ==1:

computer =

'石頭'

else

: computer =

'布'print

('計算機出的為 %s'

%computer)

if player_in == computer_in:

print

('tie break'

)elif

(player_in ==

0and computer_in ==2)

or(player_in ==

1and computer_in ==0)

or(player_in ==

2and computer_in ==1)

:print

('you won!'

)else

:print

('you lost!'

)

for i in

range(0

,10):

print

(i)

count =

10while count<5:

print

(count,

'小於5'

) count+=

1else

:print

(count,

'大於或等於5'

)

- break continue pass

break與continue與其它語言相同,break跳出for和while迴圈體,continue跳過當前迴圈,直接進行下一輪迴圈

pass語句是空語句,用來佔位,不做任何事情

九九乘法表的實現:

for i in

range(1

,10):

for j in

range(1

, i +1)

:print

("%d*%d=%d"

%(i, j, i * j)

, end=

'\t'

) j +=

1print

('\n'

)

未完。。

python學習記錄 基礎

模組匯入與使用 編寫規範 其他浮點數 複數 a 3 4j b 5 6j c a b c 8 10j c.real 8.0 c.imag 10.0 a.conjugate 3 4j a b 9 38j a b 0.6393442622950819 0.03278688524590165j python...

python基礎學習筆記 知識碎片記錄(1)

1 最早只有127個字元被編碼到計算機裡,也就是大小寫英文本母 數字和一些符號,這個編碼表被稱為ascii編碼,ascii碼佔乙個位元組 byte 8位。但是要處理中文顯然乙個位元組是不夠的,至少需要兩個位元組,還不能和ascii編碼衝突,所以,中國制定了gb2312編碼,用來把中文編進去,各國都有...

python 學習筆記記錄(二)

號表示之後的字元為python的注釋 換行 n 是標準的行分隔符 通常乙個語句一行 反斜槓 繼續上一行 分號 將兩個語句連線在一行中 冒號 將 塊的頭和體分開 語句 塊 用縮排塊的方式體現 不同的縮排的深度分隔不同的 塊 python檔案以模組的形式組織 在python 語言中,物件是通過引用傳遞的...