Python程式語法元素分析 1

2021-10-09 21:15:07 字數 1959 閱讀 8445

【eg.1】根據半徑計算圓的面積

#檔案式

r=25

area=

3.14

*r*r

print

(area)

print(""

.format

(area)

)#保留兩位小數

【eg.2】繪製多個同切圓

#互動式

import turtle

turtle.pensize(2)

turtle.circle(10)

turtle.circle(40)

turtle.circle(80)

turtle.circle(

160)

【eg.3】五角星繪製

#互動式

from turtle import

*color(

'red'

,'red'

)begin_fill(

)for i in

range(5

):fd(

200)

rt(144)

end_fill(

)#檔案式

from turtle import

*color(

'red'

,'red'

)begin_fill(

)for i in

range(5

):fd(

200)

rt(144)

end_fill(

)done(

)

【eg.4】溫度轉化例項

#tempconvert.py

tempstr =

input

("請輸入帶有符號的溫度值:"

)if tempstr[-1

]in['f'

,'f']:

c=(eval

(tempstr[0:

-1])

-32)/

1.8print

("轉換後的溫度是c"

.format

(c))

elif tempstr[-1

]in['c'

,'c']:

f=1.8*

eval

(tempstr[0:

-1])

+32print

("轉化後的溫度是f"

.format

(f))

else

:print

("輸入格式錯誤"

)

【eg.4】溫度轉化例項

#tempconvert.py

tempstr =

input

("請輸入帶有符號的溫度值:"

)if tempstr[-1

]in['f'

,'f']:

c=(eval

(tempstr[0:

-1])

-32)/

1.8print

("轉換後的溫度是c"

.format

(c))

elif tempstr[-1

]in['c'

,'c']:

f=1.8*

eval

(tempstr[0:

-1])

+32print

("轉化後的溫度是f"

.format

(f))

else

:print

("輸入格式錯誤"

)

注釋

命名:關聯識別符號的過程

保留字:被程式語言內部定義並保留使用的識別符號

正向遞增序號和反向遞減序號

使用獲取字串中的乙個或多個字元

Python程式語法元素分析

程式的格式框架 注釋命名與保留字 字串賦值語句 input 函式 分支語句 eval 函式 print 函式 迴圈語句 函式1.程式的格式框架 python語言採用嚴格的縮進來表明程式的格式框架。縮排表達所屬關係。注 不是所有的 都可以通過縮排包含其他的 如print 這樣簡單語句不表達包含關係,不...

Python程式語法元素分析

目錄命名與保留字 語句與函式 python程式的輸入輸出 下面是一段溫度轉換的 示例 tempconvert.py tempstr input 請輸入帶有符號的溫度值 if tempstr 1 in f f c eval tempstr 0 1 32 1.8 print 轉換後的溫度是f forma...

Python語法元素分析

1 1個縮排 4個空格。2 用以在python中標明 的層次關係。3 縮排式python語言中表明程式框架的唯一手段。1 注釋 程式設計師在 中加入的說明資訊,不被計算機執行。2 注釋的兩種方法 1 單行注釋以 開頭 here are the comments 2 多行注釋以 開頭和結尾。this ...