Python語法
1. 結構
1. 沒有花括號{}
2. 表示式以冒號:開頭
3. 換行符分割語句
4. 以程式碼塊的對齊方式來區分程式碼塊
2. 變數
沒有型別關鍵字,沒有宣告關鍵字,直接變數名=
year = 2017
month = 9
day = 1
date = "2017年9月1日"`
4. 定義類
class開頭
class 類名:
def 方法名(self):
print("Hello wrold!")
5. 繼承
# 繼承
class Father:
name = ""
def __init__(self, name):
self.name = name
def sayName(self):
print(self.name)
class Son(Father):
def __init__(self, name):
super().__init__(name)
self.name = name + "的父親"
son = Son("張三")
son.sayName()
結果
張三的父親
6.訪問控制
__foo__:
定義的是特列方法,類似 __init__() 之類的。
_foo:
以單下劃線開頭的表示的是 protected 型別的變數,即保護型別只能允許其本身與子類進行訪問,
不能用於 from module import *
__foo:
雙下劃線的表示的是私有型別(private)的變數, 只能是允許這個類本身進行訪問了。
7.List
集合使用中括號定義,訪問通過下標進行訪問,和其他語言都是一樣的
name=["王大","趙二","張三","李四"]
#刪除下標為2的元素:張三
del name[2]
8. 字典(Dictionary)
相當於java中的map,是有key-value組成的
dict = {`Alice`: `2341`, `Beth`: `9102`, `Cecil`: `3258`}
9. 例項化物件
沒有new關鍵字,直接以類名()建立
10.流程控制
基本上與其他語言一樣,只不過沒有()和{},詳情看1
if case :
print("111")
else :
print("222")
資料型別轉換
int(x [,base])
將x轉換為一個整數
long(x [,base] )
將x轉換為一個長整數
float(x)
將x轉換到一個浮點數
complex(real [,imag])
建立一個複數
str(x)
將物件 x 轉換為字串
repr(x)
將物件 x 轉換為表示式字串
eval(str)
用來計算在字串中的有效Python表示式,並返回一個物件
tuple(s)
將序列 s 轉換為一個元組
list(s)
將序列 s 轉換為一個列表
set(s)
轉換為可變集合
dict(d)
建立一個字典。d 必須是一個序列 (key,value)元組。
frozenset(s)
轉換為不可變集合
chr(x)
將一個整數轉換為一個字元
unichr(x)
將一個整數轉換為Unicode字元
ord(x)
將一個字元轉換為它的整數值
hex(x)
將一個整數轉換為一個十六進位制字串
oct(x)
將一個整數轉換為一個八進位制字串
相關文章
- python 語法總結:Python語法快速入門Python
- Python語法—物件Python物件
- Python 基本語法Python
- python基礎語法—語句Python
- Python語法—讀寫Python
- python 常用的語法Python
- 修改python語法注意Python
- Python 基礎語法Python
- Python的基本語法Python
- Python基礎語法Python
- Python的基礎語法Python
- Python基礎(06):if語法Python
- Python初級面試語法Python面試
- Python基礎語法(二)Python
- Python基礎語法(一)Python
- python 基礎語法(三)Python
- 如何自定義python語法.Python
- Python相關語法2Python
- Python with...as... 語法深入解析Python
- 小白自學Python(三)Python基本語法Python
- Python 語法之裝飾器Python
- Python爬蟲之XPath語法Python爬蟲
- Python語法的轉義字元Python字元
- Python基礎語法資料Python
- Python3 基礎語法Python
- Python 函式 類 語法糖Python函式
- python語法-測試程式碼Python
- 【PYTHON】語法基礎 | 開始使用PythonPython
- assert語句,python, 笨辦法學pythonPython
- Python學習筆記(語法篇)Python筆記
- Python十大裝B語法Python
- 快速掌握Python基礎語法(下)Python
- Python語法—迭代器、生成器Python
- Python 與 JavaScript 語法差異點PythonJavaScript
- python高階語法:繼承性Python繼承
- python 基礎語法 - 函式(一)Python函式
- Python基礎:語法基礎(3)Python
- 鮮為人知的 Python 語法Python