首頁  >  文章  >  後端開發  >  Python 教學 - 簡介

Python 教學 - 簡介

王林
王林原創
2024-08-25 06:01:50290瀏覽

Python 是目前最受歡迎的程式語言之一,尤其是隨著人工智慧技術的興起。 Python 是一種多用途程式語言,用於開發 Web 應用程式、後端服務、資料科學和機器學習等許多東西。

設定

以下是使用 Python 編碼的準備:

  1. 下載 Python 然後安裝它。
  2. 您可以使用任何文字編輯器來編寫 Python 程式碼(例如 Visual Studio Code)或專用 IDE(例如 PyCharm)。

寫第一個程式碼

建立一個名為 main.py、副檔名為 .py 的新檔案。然後編寫這段程式碼。

print("Hello World!")

執行此指令來執行Python程式碼。

python main.py

這是輸出。

Hello World!

基於上面的程式碼,print()函數顯示Hello World!文字。

變數和資料類型

變數是儲存整數、浮點數和字串(一堆字母數字字元)等值的地方。這是 Python 中變數使用的範例。

number = 42
username = "John Doe"
price = 2.95

要顯示變數的值,請使用 print() 函數。

number = 42
username = "John Doe"
price = 2.95

# display a value from variable
print("this is a number", number)
print("price: ", price)
# using formatting
print(f"hello, my username is {username}")

這是輸出。

this is a number 42
price:  2.95
hello, my username is John Doe

這是Python中常用資料類型的清單。

Data Type Value
Integer non-decimal number
Float decimal number
String Alphanumeric characters
Boolean True or False

操作員

Python中有許多基本的算術運算子。這些運算符可用於執行整數和浮點數等數字資料類型的計算。

Operator Description
+ add operation
- substract operation
* multiply operation
/ division operation
// floor division operation
% modulo operation (get the remainder from division operation)
** Perform the operation of raising a number to the power of a number

這是 Python 中運算子使用的範例。

first = 4
second = 2

addition = first + second
subtraction = first - second
multiplication = first * second
division = first / second
mod = first % second
square_of_first = first ** 2

print(f'{first} + {second} = {addition}')
print(f'{first} - {second} = {subtraction}')
print(f'{first} * {second} = {multiplication}')
print(f'{first} / {second} = {division}')
print(f'{first} % {second} = {mod}')
print(f'{first} ** {2} = {square_of_first}')

輸出

4 + 2 = 6
4 - 2 = 2
4 * 2 = 8
4 / 2 = 2.0
4 % 2 = 0
4 ** 2 = 16

// 運算子執行除法,然後傳回除法結果的下限。

result = 29 // 5 # returns 5 (actual value before floor operation: 5.8)

新增用戶輸入

input() 函數讀取使用者的輸入。此函數對於在 Python 中建立互動式程式非常有用。預設情況下,input() 傳回 String 資料類型。

這是使用 input() 函數的基本範例。

# get username from input
username = input("enter username: ")
# get age from input
# the int() function converts string into integer data type
age = int(input("enter age: "))

print(f"username: {username}")
print(f"age: {age}")

輸出

Python Tutorial - ntroduction

範例 1 - 矩形面積計​​算

讓我們用 Python 建立一個矩形面積計​​算程式。該程式允許使用者輸入矩形的長度和寬度。然後,程式計算矩形的面積,然後將其顯示給使用者。

# get length from user input
length = int(input("enter length: "))

# get width from user input
width = int(input("enter width: "))

# calculate the area of rectangle
area = length * width

# display the result
print(f"area of rectangle: {area}")

輸出

Python Tutorial - ntroduction

範例 2 - 取得折扣價

讓我們建立一個程式來計算應用折扣後商品的價格。該程式允許用戶輸入實際價格和折扣。然後,程式返回折扣價。

# get price from user input
price = int(input("enter price: "))

# get discount from user input
discount = int(input("enter discount: "))

# calculate the discounted price
discounted_price = price - (price * (discount / 100))

# display the result
print(f"original price: {price}")
print(f"discounted price: {discounted_price}")

輸出

Python Tutorial - ntroduction

來源

  • Python 官方頁面。
  • Python 教程。

希望這篇文章對你學習Python有幫助。如果您有任何意見,請在評論區告訴我。

以上是Python 教學 - 簡介的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn