- Python 是一種解釋性,面向對象的程序設計語言。
- Python 嚴格區分大小寫
程序設計錯誤可以分爲三種類型:
1.語法錯誤 2.運行時錯誤 3.邏輯錯誤
在終端運行Python
在終端輸入 Python
運行Python 源文件
python filename.py
簡單的打印語句
“`
print(“Python”)
print(5 + 3)
“`
運行Python源文件命令
Python 文件名.py
終端輸出
註釋
行註釋
#This program displays !Welcome to Python
段註釋
三個連續單引號
'''This program displays !Welcome to Python
'''
簡單的算術
print(1 + 2)
print(8 - 3)
print(2 * 3)
print(6 / 2)
圖形化程序設計– Turtle
1.導入Turtle 命令
import turtle # Import turtle module
2.顯示Turtle當前位置和方向
turtle.showturtle()
3.繪製文本
turtle.write(" Welcome to python ")
4.繪製一條直線(移動100像素)
turtle.forward(100)
5.箭頭旋轉角度Turtle顏色改變(自行多嘗試)
turtle.right(90)
turtle.color("red")
6.移動Turtle 圖形窗口的中心位置初始座標爲(0,0)
turtle.goto(0,50)
7.擡起和放下筆(控制是否是一條直線)
turtle.penup() # 擡起畫筆
turtle.goto(-50,50) # 移動位置
turtle.pendown() # 放下畫筆
8.畫個簡單的圓
turtle.color("red")
turtle.circle(50) # radius 50
9.小程序畫奧運五環(自行確定好座標)
turtle.color("blue")
turtle.penup()
turtle.goto(-110,-25)
turtle.pendown()
turtle.circle(45)
turtle.color("black")
turtle.penup()
turtle.goto(0,-25)
turtle.pendown()
turtle.circle(45)
turtle.color("red")
turtle.penup()
turtle.goto(110,-25)
turtle.pendown()
turtle.circle(45)
turtle.color("yellow")
turtle.penup()
turtle.goto(-55,-75)
turtle.pendown()
turtle.circle(45)
turtle.color("blue")
turtle.penup()
turtle.goto(55,-75)
turtle.pendown()
turtle.circle(45)
turtle.done()
效果圖
turtle.done() 作用是導致程序暫停知道用戶關閉Python Turtle 圖形化窗口,它的目的是給用戶時間來查看徒刑,沒有這行,圖形窗口會在程序完成是立即關閉。