1. 程式人生 > >python3------基礎語法

python3------基礎語法

str 代碼塊 反斜杠 如果 表達 註意 函數 ase bin

1 註釋

1.1 以#號開頭的註釋

1.2 以""" 註釋內容 """

1.3 以 ''' 註釋內容'''

2 行與縮進

python代碼塊通過縮進對齊表達代碼邏輯而不是使用大括號;

縮進表達一個語句屬於哪個代碼塊。

語句塊縮進(常用4個空格)

3 多行語句

如果書寫的語句很長,可以使用反斜杠(\)來實現多行語句,例如:

>>> print ("hello \

world")

hello world

註意:在列表[],字典{}或者元組()中的多行語句中不需要使用反斜杠

4 數字類型

int(整數)

bool(布爾)

float(浮點)

complex(復數)

5 字符串

5.1 python中單引號和雙引號使用完全相同

5.2 使用轉義字符 \

5.3 使用r可以讓反斜杠不發生轉義

>>> print ("this is a string \n")

this is a string


>>> print (r"this is a srting \n")

this is a srting \n

5.4 python中字符串有兩種索引的方式,從左往右以0開始,從右往左以-1開始

5.5 字符串截取

#!/usr/bin/python
str = 'hello world'
print (str) #輸出字符串 hello world

print (str[0:-1]) #輸出第一個到倒數第二個 hello worl

print (str[0]) #輸出第一個字符 h

print (str[2:5]) #輸出從第三個開始到第五個字符 llo

print (str * 2) #輸出字符串2次 hello worldhello world

print (str + 'add') #連接字符串 hello worldadd


5.6 等待用戶輸入 input

>>> input ("please input:")

please input:hello world

'hello world'

6 import 與 from ... import

import:將整個模塊導入

from ... import :從某個模塊中導入函數

7 命令行參數

# python -h

python3------基礎語法