ชีตสรุป Python
ข้อมูลอ้างอิง Python 3 ที่ค้นหาและพิมพ์ได้ — ไวยากรณ์ โครงสร้างข้อมูล comprehensions ฟังก์ชัน คลาส ไฟล์ และไลบรารีมาตรฐาน ฟรี
Variables & types
10x = 1
x: int = 1
a, b = 1, 2
type(x)
int('42')
str(42)
isinstance(x, int)
None
x = y = 0
PI: Final = 3.14
Strings
11f'Hello {name}'
len(s)
s.upper()
s.strip()
s.split(',')
','.join(items)
s.replace('a', 'b')
'a' in s
s[1:4]
s[::-1]
s.startswith('a')
Lists & tuples
10a = [1, 2, 3]
a.append(4)
a.insert(0, x)
a.pop()
a[1:3]
sorted(a, reverse=True)
a.sort(key=len)
len(a)
t = (1, 2)
first, *rest = a
Dicts & sets
10d = {'k': 'v'}
d['k']
d.get('k', default)
d.keys() / d.values()
d.items()
{**a, **b}
a | b
del d['k']
s = {1, 2, 3}
set(a) & set(b)
Comprehensions
8[x * 2 for x in a]
[x for x in a if x > 0]
{x: x**2 for x in a}
{x for x in a}
(x for x in a)
[y for row in m for y in row]
[a if c else b for x in items]
sum(x for x in a)
Control flow
9if x > 0:\n ...
elif / else:
a if cond else b
for x in range(10):
for i, x in enumerate(a):
for a, b in zip(x, y):
while cond:
break / continue
match x:\n case 1: ...
Functions
8def f(a, b=1): return a + b
def f(*args, **kwargs):
f(name='Sam')
lambda x: x + 1
def f(a: int) -> int:
@decorator
yield value
global x / nonlocal x
Classes
9class A(Base):
def __init__(self, x):
self.x = x
def __str__(self):
@property
@staticmethod / @classmethod
super().__init__()
@dataclass
isinstance(obj, A)
Files, errors & modules
9with open('f.txt') as fh:
fh.read() / fh.readlines()
open('f.txt', 'w').write(s)
try:\n ...\nexcept ValueError as e:
raise ValueError('bad')
finally:
import os
from math import sqrt
import numpy as np
ไม่มีรายการที่ตรงกับ “:q”
เกี่ยวกับ ชีตสรุป Python
เอกสารสรุป Python นี้บีบอัด Python 3 ไว้ในหน้าเดียวที่ค้นหาได้: ตัวแปรและชนิดข้อมูล string ลิสต์และทูเพิล dict และ set comprehension การควบคุมโฟลว์ ฟังก์ชัน คลาส และไฟล์ ข้อผิดพลาดและโมดูล แต่ละแถวแสดงโค้ดที่ใช้งานได้จริงพร้อมคำอธิบายสั้น ๆ
มันอิงกับ Python ที่เป็นสำนวนเฉพาะตัว — f-string, การแบ่งส่วนข้อมูล (slicing), list และ dict comprehension, การแกะกล่อง (unpacking), argument ค่าเริ่มต้นและคีย์เวิร์ด, context manager และการจัดการข้อยกเว้น — ทำให้มันเป็นตัวทบทวนที่รวดเร็วไม่ว่า Python จะเป็นภาษาประจำวันของคุณหรือเป็นภาษาที่คุณกลับมาใช้เป็นครั้งคราว
เอกสารนี้ฟรีและทำงานฝั่ง client ทั้งหมด กรองแบบสดจากกล่องค้นหา นำทางด้วยเมนูส่วนที่ติดอยู่บนหน้า คัดลอกสนิปเป็ตใด ๆ ด้วยคลิกเดียว และพิมพ์เอกสารอ้างอิงทั้งหมดเมื่อคุณต้องการให้อยู่ข้างคีย์บอร์ด
วิธีใช้ ชีตสรุป Python
- ไล่ดูส่วนต่าง ๆ — จาก Variables & types ผ่าน Comprehensions ไปจนถึง Files, errors & modules
- พิมพ์ลงในกล่องค้นหาเพื่อกรองทุกสนิปเป็ตแบบสดทั่วทั้งเอกสาร
- กระโดดไปยังส่วนอย่าง Dicts & sets โดยใช้สารบัญที่ติดอยู่บนหน้า
- คลิกสนิปเป็ตใด ๆ หรือไอคอนคัดลอกของมันเพื่อคัดลอกโค้ด Python
- พิมพ์หน้าเพื่อเก็บไว้เป็นข้อมูลอ้างอิง Python 3 แบบออฟไลน์