Python3 基礎語法

2021-08-21 03:55:40 字數 979 閱讀 4685

注釋方式

# 這是注釋

'''這是注釋

'''"""

這是注釋

"""

字串

str='hello'

print(str) # 輸出字串

print(str[0:-1]) # 輸出第乙個到倒數第二個的所有字元

print(str[0]) # 輸出字串第乙個字元

print(str[2:4]) # 輸出從第三個開始到第四個的字元

print(str[2:]) # 輸出從第三個開始的所有字元

print(str * 2) # 輸出字串兩次

print(str + ' furong') # 連線字串

print('hello\nfurong') # 使用轉義字元

print(r'hello\nfurong') # 在字串前面新增乙個 r,表示原始字串,不會發生轉義

執行結果

hello

hellhll

llohellohello

hello furong

hello

furong

hello\nfurong

print 輸出

print 預設輸出是換行的,如果要實現不換行需要在變數末尾加上 end=」 「

x="a"

y="b"

print( x, end=" " )

print( y, end=" " )

執行結果

a b

python3基礎語法

識別符號 1.第乙個字元必須是字母表中字母或者下劃線 2.識別符號的其他部分由字母 數字和下劃線組成。3.識別符號對大小寫敏感 python保留字 保留字即關鍵字,我們不能把它們用作任何識別符號名稱。python 的標準庫提供了乙個 keyword 模組,可以輸出當前版本的所有關鍵字 import ...

Python3基礎語法

import keyword python中的乙個標準庫 print keyword.kwlist 輸出當前版本所有的關鍵字 echo false none true and as assert async await break class continue def del elif else e...

Python3基礎語法

在python3 中,可以用中文來作為變數名,非ascii識別符號也是允許的 保留字即關鍵字,不能用作於python 中的識別符號。python的標準庫提供了乙個keyword模組,可以輸出所有關鍵字 import keyword keyword.kwlist false none true and...