首頁 > 後端開發 > Python教學 > Python的基本概念是什麼?

Python的基本概念是什麼?

王林
發布: 2023-08-31 22:09:10
轉載
1435 人瀏覽過

Python的基本概念是什麼?

Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language.

Python的特點

以下是Python的主要特點:

  • Python支援函數式和結構化程式設計方法以及物件導向程式設計。

  • It can be used as a scripting language or can be compiled to byte-code for building large applications.

  • #It provides very high-level dynamic data types and supports dynamic type checking.

  • It supports automatic garbage collection.

Variables in Python

Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. Let’s create a variable.

a = 10
登入後複製

上面,a是一個變量,賦值為整數10。

Numeric Datatype in Python

Number data types store numeric values. They are immutable data types, means that changing the value of a number data type results in a newly allocated object.

Python supports four different numerical types.

  • int (signed integers) − They are often called just integers or ints, are positive or negative whole numbers with no decimal point.

  • long (long integers ) − Also called longs, they are integers of unlimited size, written like integers and followed by an uppercase or lowercase L.

  • float (floating point real values) − Also called floats, they represent real numbers and are written with a decimal point dividing the integer and fractional parts. Floats may also be in scient. with E 或 e indicating the power of 10 (2.5e2 = 2.5 x 102 = 250).

  • complex (complex numbers) − are of the form a bJ, where a and b are floats and J (or j) represents the square root of -1 (which is an imaginary number ). The real part of the number is a, and the imaginary part is b. Complex numbers are not used much in Python programming.

Strings in Python

字串是Python中最受歡迎的類型之一。我們可以透過在引號中包含字元來簡單地創建它們。 Python將單引號和雙引號視為相同。建立字串就像將一個值賦給一個變數一樣簡單。

Let’s see how to easily create a String in Python.

myStr = Thisisit!'
登入後複製

Lists in Python

清單是Python中最多功能的資料類型,可以用方括號將逗號分隔的值(項目)寫成清單。讓我們看看如何建立不同類型的清單。

myList1 = ['abc', 'pq'];
myList2 = [5, 10, 15, 20];
登入後複製

Tuples in Python

Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square# brack.

#Creating a tuple is as simple as putting different comma-separated values. Optionally you can put these comma-separated values between parentheses also. Let’s see how to create a Tuple.#
myTuple1 = ('abc', 'pq)];
myTuple2 = (5, 10, 15, 20);
登入後複製

Dictionary in Python

字典是Python中的一種序列。在字典中,每個鍵都與其值以冒號(:)分隔,項目之間用逗號分隔,整個字典被括在花括號中。字典中的鍵是唯一的,而值可能不是唯一的。字典的值可以是任何類型,但鍵必須是不可變的資料類型,例如字串、數字或元組。

Let’s see how to create a Dictionary −

# Creating two Dictionaries
dict1 = {'Player':['Jacob','Steve','David','John','Kane'], 'Age':[29, 25, 31, 26, 27]}
dict2 = {'Rank':[1,2,3,4,5], 'Points':[100,87, 80,70, 50]}
登入後複製

Classes & Objects in Python

類別是一個使用者定義的物件原型,它定義了一組屬性,這些屬性描述了該類別的任何物件。這些屬性是資料成員和方法,透過點符號存取。

An object is a unique instance of a data structure that's defined by its class. An object comprises both data members (class variables and instance variables) and methods.

Functions in Python

function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing.

################################################################# ###函數區塊以關鍵字 def 開始,後面跟著函數名稱和括號 ( ( ) )。讓我們建立一個函數。 ###
def demo(s):
   print (s)
   return
# Function call
demo("Function Called")
登入後複製
###輸出###
Function Called
登入後複製
###

以上是Python的基本概念是什麼?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板