初學python基礎

2021-10-01 22:51:31 字數 1828 閱讀 9086

基本輸入輸出語句

res=

input

('請隨便輸入:'

)#輸入操作

print

(res)

#輸出操作

print

('周%s放假啦'

%res)

#佔位符格式化

print引數解釋:

『』』value:需要輸出值,可多個

sep:間隔符號,預設空格,可修改

end:輸出語句後附加字元,預設換行』\n』

file:將文字輸入到某檔案或資料流中

flush:值為布林值,表是否立刻將輸出語句中的內容寫入檔案中

『』』

print

(res,

'下雪啦'

,123

,true

,sep=

'\$'

,end=

'****'

)#輸出操作

格式化資料型別:

『』』%s:格式化字串

%d:格式化整數

%t:格式化浮點數

『』』

num=

2score=

89.58

#res='我的學號是:'+str(num)+'我的成績:'+str(score)+'。'

res=

'我的學號是:%03d,我的成績:%.1f。'

%(num,score)

print

(res)

format格式化函式:

0,1表示位置,冒號後表示格式

num=

2score=

89.58

res=

'我的學號是:,我的成績:'

.format

(num,score)

print

(res)

條件結構語句:

#單分支條件結構語句if2

<3:

print

('2不大於3'

)#雙分支條件結構語句

#從控制台輸入兩個整數,判斷大小,輸出較大的整數

int0=

input

('第乙個整數:'

)int1=

input

('第二個整數:'

)if int0 >int1 :

print

(int0 )

else

:print

(int1 )

#多分支條件結構語句

#判斷輸入年齡段,1-6:幼兒,7-12:少兒,13-17:青少年,18-45成年人,46-69,:中年,>69:老年

age=

input

('請輸入年齡:'

)age=

int(age)if0

<=age and age<=6:

print

('幼兒'

)elif

7<=age and age<=12:

print

('少兒'

)elif

13<= age and age <=17:

print

('青少年'

)elif

18<= age and age <=45:

print

('成年人'

)elif

46<= age and age <=69:

print

('中年人'

)else

:print

('老年人'

)

初學Python 基礎篇子Python檔案操作

python file 檔案 方法 open 方法 python open 方法用於開啟乙個檔案,並返回檔案物件,在對檔案進行處理過程都需要使用到這個函式,如果該檔案無法被開啟,會丟擲 oserror。注意 使用 open 方法一定要保證關閉檔案物件,即呼叫 close 方法。open 函式常用形式...

python初學函式 python 初學函式

len s 金老闆小 len s def my len 自定義函式 i 0 for k in s i 1 print i length my len print length 函式 定義了之後,可以在任何需要它的地方呼叫 沒有返回長度,只是單純的列印 返回的重要性 a,b len a 內建函式 le...

Python初學筆記

工作需要,要接觸python的程式,學習一下 使用的3.2版本 話說比2.5變化還真多 print都變了 總體感覺,py比較perl來說,特點還是非常之強烈的 1 py可以自動字串複製 x 3 x 3 333 2 py是強型別變數,和perl不同,型別變數不能混用 3 字串連線 號和join等str...