Python 3基礎

Python 3基礎

Python是Guido van Rossum在90年代早期開發的,它的最新版本是3.7.1,我們可以簡單地稱之為Python3。Python3.0於2008年發佈。是一種解釋性語言,即它沒有編譯,解釋器將逐行檢查代碼。本文可以用來學習Python編程語言的基本知識。

所以在繼續之前..讓我們來做最流行的“HeloWord”的傳統,並將Python的語法與C、C++、java進行比較。

<code># Python code for "Hello World" 
# nothing else to type...see how simple is the syntax.

print("Hello World") /<code>

請注意,Python的範圍不取決於括號{},而是使用縮進作為範圍。

現在繼續前進,讓我們開始瞭解Python的基礎知識。我將在一些小節中介紹基礎知識。只要仔細閱讀它們,並相信我,您會很容易地學習Python的基礎知識。

簡介和設置

1)如果您使用的是Windows操作系統,請點擊此處下載Python(https://www.python.org/downloads/windows/),然後從安裝程序中安裝,並在開始菜單中輸入IDLE.IDLE,您可以將其視為運行Python腳本的Python IDE。

看起來會是這樣:

Python 3基礎

2)99%的Linux操作系統上預安裝Python。如果您使用的是類似Linux /Unix的操作系統,請打開終端,然後在終端中鍵入“python3”即可開始使用。

它看起來像這樣:

Python 3基礎

“>>>”表示python shell及其準備接受python命令和代碼。

變量和數據結構

在其他編程語言(例如C,C++和Java)中,您需要聲明變量的類型,但在Python中,則不需要這樣做。只需鍵入變量,並在將其賦值時,它將自動知道給定的值是int,float還是char甚至是String。

<code># Python program to declare variables 
myNumber = 3
print(myNumber)

myNumber2 = 4.5
print(myNumber2)

myNumber ="helloworld"
print(myNumber) /<code>

輸出:

<code>3
4.5
helloworld/<code>

看,它有多簡單,只需創建一個變量併為其分配所需的任何值,然後使用打印功能即可將其打印出來。Python有4種內置的數據結構類型,即列表(List),字典(Dictionary),元組(Tuple)和集合(Set)。

List是python中最基本的數據結構。列表是可變的數據結構,即在創建列表之後,可以將項添加到列表中。就好像你要去當地市場購物,列出一些商品的清單,然後你可以在清單上添加越來越多的商品。

append() 函數用於將數據添加到列表中。

<code># Python program to illustrate list  

# creates a empty list
nums = []

# appending data in list
nums.append(21)
nums.append(40.5)
nums.append("String")

print(nums) /<code>

輸出:

<code>[21, 40.5, String]/<code>

註釋:

<code># is used for single line comment in Python
""" this is a comment """ is used for multi line comments/<code>

輸入輸出

在本節中,我們將學習如何從用戶那裡獲取輸入,然後操縱或簡單地顯示它。 input()函數用於接收用戶的輸入。

<code># Python program to illustrate 
# getting input from user
name = input("Enter your name: ")

# user entered the name 'prog61'
print("hello", name) /<code>

輸出:

<code>hello prog61/<code>
<code># Python3 program to get input from user 

# accepting integer from the user
num1 = int(input("Enter num1: "))
num2 = int(input("Enter num2: "))

num3 = num1 * num2
print("Product is: ", num3)
/<code>

輸出:

<code>Enter num1: 8 Enter num2: 6 ('Product is: ', 48)/<code>

選擇(Selection)

在Python中的選擇是使用兩個關鍵字“ if”和“ elif”以及else(elseif)

<code># Python program to illustrate 
# selection statement

num1 = 34
if(num1>12):
print("Num1 is good")
elif(num1>35):
print("Num2 is not gooooo....")
else:
print("Num2 is great") /<code>

輸出:

<code>Num1 is good/<code>

函數

您可以將函數想像成一堆旨在在整個Python腳本中完成特定任務的代碼。Python使用關鍵字“ def”來定義函數。

語法:

<code>def function-name(arguments):
#function body/<code>
<code># Python program to illustrate 
# functions
def hello():

print("hello")
print("hello again")
hello()

# calling function
hello() /<code>

輸出:

<code>hello
hello again
hello
hello again/<code>

現在我們知道任何程序都從“main”函數開始……讓我們像許多其他編程語言一樣創建main函數。

<code># Python program to illustrate  
# function with main
def getInteger():
result = int(input("Enter integer: "))
return result

def Main():
print("Started")
# calling the getInteger function and
# storing its returned value in the output variable
output = getInteger()
print(output)

# now we are required to tell Python
# for 'Main' function existence
if __name__=="__main__":
Main() /<code>

輸出:

<code>Started
Enter integer: 5/<code>

迭代(循環)

顧名思義,它要求一次又一次地重複。我們將在此處使用最流行的“for”循環。

<code># Python program to illustrate 
# a simple for loop

for step in range(5):
print(step) /<code>

輸出:

<code>0
1
2
3
4/<code>

模塊

Python有一個非常豐富的模塊庫,其中包含一些函數來執行許多任務。

關鍵字“import”用於將特定模塊導入到您的python代碼中。例如,考慮以下程序。

<code># Python program to illustrate 
# math module
import math

def Main():
num = float(input("Enter a number: "))
# fabs is used to get the absolute value of a decimal
num = math.fabs(num)
print(num)
if __name__=="__main__":
Main() /<code>

輸出:

<code> Enter a number: 85.0/<code>

這些是Python編程語言的一些最基礎知識,我將在我的後續文章中介紹中級和高級Python主題。


分享到:


相關文章: