在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Python/ Python3基礎(chǔ)語法
Python3文件操作
Python3日期和時(shí)間
Python3基礎(chǔ)語法
Python3字典
Python3元組
Python3文件方法
Python3字符串
Python3引入什么新的東西?
Python3異常處理
Python3模塊
Python3數(shù)字
Python3變量類型
Python3函數(shù)
Python3循環(huán)
Python3 os文件目錄的方法
Python3 while循環(huán)語句
Python3斷言
Python3基本運(yùn)算符
Python3環(huán)境安裝設(shè)置
Python3標(biāo)準(zhǔn)異常
Python3嵌套循環(huán)
Python3教程
Python3決策
Python3 for循環(huán)語句
Python3列表

Python3基礎(chǔ)語法

Python語言有許多與Perl,C和Java的相似之處。不過,語言之間也有一些明顯的差異。

第一個(gè)Python程序

讓我們在不同的編程模式下執(zhí)行程序。

交互模式編程

調(diào)用解釋器,不用通過一個(gè)腳本文件作為參數(shù),如下提示 -
$ python
Python 3.3.2 (default, Dec 10 2013, 11:35:01)
[GCC 4.6.3] on Linux
Type "help", "copyright", "credits", or "license" for more information.
>>>
在 Windows 系統(tǒng)上:
C:\Users\Administrator>python
Python 3.5.1 (v3.5.1:37a07cee5969, Dec  6 2015, 01:54:25) [MSC v.1900 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
在 Python 提示符下編寫下面的文本代碼,然后按Enter:
>>> print ("Hello, Python!") 

如果你使用的是 Python(Python2.X)的舊版本,在print 函數(shù)使用括號(hào)是一個(gè)可選項(xiàng)。這將產(chǎn)生以下的結(jié)果:

Hello, Python!

腳本編程模式

解釋器調(diào)用一個(gè)腳本參數(shù)開始執(zhí)行腳本,并一直持續(xù)到腳本完成。當(dāng)腳本完成,解釋器活動(dòng)失效。

讓我們在腳本中寫一個(gè)簡單的 Python 程序。Python文件使用 .py 擴(kuò)展名。 創(chuàng)建一個(gè)文件 test.py 并寫入以下代碼:

print ("Hello, Python!") 

假設(shè)你已經(jīng)在 PATH 變量中設(shè)置了 Python 解釋器路徑。 現(xiàn)在,試著如下運(yùn)行這個(gè)程序 -

在Linux上執(zhí)行
$ python test.py 
這將產(chǎn)生以下的結(jié)果:
Hello, Python!
在 Windows 上執(zhí)行
C:\Python3>Python test.py
這將產(chǎn)生以下的結(jié)果:
Hello, Python!
讓我們嘗試使用另一種方式在Linux中執(zhí)行 Python 腳本。下面是修改 test.py 文件 -
#!/usr/bin/python3

print ("Hello, Python!") 

假設(shè)你已經(jīng)在 /usr/bin目錄安裝好了 Python 解釋器。現(xiàn)在,試著如下運(yùn)行這個(gè)程序 -

$ chmod +x test.py     # This is to make file executable
$./test.py
這將產(chǎn)生以下結(jié)果 -
Hello, Python!

Python標(biāo)識(shí)符

Python標(biāo)識(shí)符是用來標(biāo)識(shí)變量,函數(shù),類,模塊或其他對象的名稱。標(biāo)識(shí)符是以字母A到Z開始或a?z或后跟零個(gè)或多個(gè)字母下劃線(_),下劃線和數(shù)字(0?9)。

Python標(biāo)識(shí)符范圍內(nèi)的不容許有如:@, $ 和 % 符號(hào)。Python是一種區(qū)分大小寫的編程語言。因此,Manpower 和 manpower 在Python中是兩種不同的標(biāo)識(shí)符。

下面是 Python 標(biāo)識(shí)符命名的約定 -
  • 類名稱使用大寫字母。所有其它標(biāo)識(shí)符開始使用小寫字母。
  • 開頭使用一個(gè)下劃線的標(biāo)識(shí)符表示該標(biāo)識(shí)符是私有的。
  • 開始以兩個(gè)前導(dǎo)下劃線的標(biāo)識(shí)符表示強(qiáng)烈私有的標(biāo)識(shí)符。
  • 如果標(biāo)識(shí)符使用兩個(gè)下劃線作為結(jié)束時(shí),所述標(biāo)識(shí)符是語言定義的特殊的名字。

保留字

Python 的關(guān)鍵字如下列出。這些是保留字,不能把它們作為常量或變量或任何其他標(biāo)識(shí)符名稱。 所有Python的關(guān)鍵字僅包含小寫字母。

and exec Not
as finally or
assert for pass
break from print
class global raise
continue if return
def import try
del in while
elif is with
else lambda yield
except


行和縮進(jìn)

Python不使用大括號(hào)({})來表示的代碼塊類和函數(shù)定義或流程控制。代碼塊由行縮進(jìn),這是嚴(yán)格執(zhí)行表示。

在縮進(jìn)位的數(shù)目是可變的,但該塊內(nèi)的所有語句的縮進(jìn)量必須相同。 例如 -

if True:
    print ("True")
else:
  print ("False")
但是,以下塊產(chǎn)生一個(gè)錯(cuò)誤 -
if True:
    print ("Answer")
    print ("True")
else:
    print "(Answer")
  print ("False") 

因此,Python中所有連續(xù)不換行,同樣數(shù)量的空格縮進(jìn)將形成一個(gè)塊。下面的例子有各種各樣的語句塊 -

注意:不要試圖理解其中的邏輯在這個(gè)時(shí)候。只要你明白,即使他們不使用括號(hào)在各個(gè)模塊。

#!/usr/bin/python3

import sys

try:
  # open file stream
  file = open(file_name, "w")
except IOError:
  print ("There was an error writing to", file_name)
  sys.exit()
print ("Enter '", file_finish,)
print "' When finished"
while file_text != file_finish:
  file_text = raw_input("Enter text: ")
  if file_text == file_finish:
    # close the file
    file.close
    break
  file.write(file_text)
  file.write("\n")
file.close()
file_name = input("Enter filename: ")
if len(file_name) == 0:
  print ("Next time please enter something")
  sys.exit()
try:
  file = open(file_name, "r")
except IOError:
  print ("There was an error reading file")
  sys.exit()
file_text = file.read()
file.close()
print (file_text)

多行語句

在Python語句通常使用一個(gè)新行作為結(jié)束。但是,Python 允許使用續(xù)行字符(\)表示讓行可以連續(xù)下去。例如-

total = item_one + \
        item_two + \
        item_three 

語句中包含 [], {}, 或() 括號(hào)內(nèi)不需要使用續(xù)行字符。 例如?

days = ['Monday', 'Tuesday', 'Wednesday',
        'Thursday', 'Friday']

在Python的引號(hào)

Python接受單引號(hào)('),雙引號(hào)(“)和三('''或”“”)引用來表示字符串,只要是同一類型的引號(hào)開始和結(jié)束。
三重引號(hào)可用于跨越多個(gè)行字符串。例如,下面所有的都是合法的 -
word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is
made up of multiple lines and sentences."""

Python中的注釋

哈希符號(hào)(#)這是一個(gè)字符作為注釋的開頭。在#之后到行末的所有字符都是注釋的一部分,Python解釋器會(huì)忽略它們。

#!/usr/bin/python3

# First comment
print ("Hello, Python!") # second comment
這將產(chǎn)生以下結(jié)果 -
Hello, Python!
你可以聲明或表達(dá)式之后的同一行寫上注釋 -
name = "Madisetti" # This is again comment
Python沒有多行注釋功能。所以你應(yīng)該單行注釋每一行如下-
# This is a comment.
# This is a comment, too.
# This is a comment, too.
# I said that already.

使用空行

僅包含空格,可能帶有注釋行,如果是空行Python會(huì)完全忽略它。
在交互式解釋器會(huì)話,則必須輸入一個(gè)空的物理行來終止多行語句。

等待用戶

程序的下面一行顯示提示聲明說:“按回車鍵退出”,并等待用戶操作 -
#!/usr/bin/python3

input("\n\nPress the enter key to exit.") 

在這里,“\n\n”是用來顯示實(shí)際行之前創(chuàng)建的兩條新行。一旦用戶按下鍵時(shí),程序就結(jié)束。 這是一個(gè)很好的技巧,以保持控制臺(tái)窗口打開,直到用戶來指定終止應(yīng)用程序運(yùn)行。

在一行多條語句

分號(hào)(;)允許給在單行有多條語句,而不管語句開始一個(gè)新的代碼塊。下面是使用分號(hào)的示例-
import sys; x = 'foo'; sys.stdout.write(x + '\n')

多重聲明組為套件

一組單獨(dú)的語句,它們使單一代碼塊化在Python稱為套件。復(fù)雜的語句,如 if, while, def, 和 class 需要一個(gè)標(biāo)頭行和一個(gè)套件。

頭部行開始的語句(以關(guān)鍵字),并用冒號(hào)終止(:),后面跟一行或多行組成套件。例如 -
if expression : 
   suite
elif expression : 
   suite 
else : 
   suite

命令行參數(shù)

很多程序都可以運(yùn)行,為您提供關(guān)于應(yīng)當(dāng)如何運(yùn)行的一些基本信息。 Python中可以使用 -h 來做到這一點(diǎn) -
$ python -h
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments (and corresponding environment variables):
-c cmd : program passed in as string (terminates option list)
-d     : debug output from parser (also PYTHONDEBUG=x)
-E     : ignore environment variables (such as PYTHONPATH)
-h     : print this help message and exit

[ etc. ] 

您也可以在這樣的,它應(yīng)該接受各種選擇的方式編寫腳本。命令行參數(shù)是一個(gè)高級(jí)的主題,當(dāng)你已經(jīng)學(xué)習(xí)了Python概念后,其余部分的內(nèi)容我們將在接下來的章節(jié)中學(xué)習(xí)。