Python基础语法要点

貢獻者:自然即存在 類別:代码 時間:2024-03-20 13:17:11 收藏數:97 評分:2
返回上页 舉報此文章
请选择举报理由:




收藏到我的文章 改錯字
Python language reserved words:
False elif lambda None else nonlocal True except not and finally or
as for pass assert from raise break global return class if try
continue import while def in with del is yield
The branching structure of a program:
Single branch structure:
if <condition>:<statement block>
Two branch structure:
if <condition>:<statement block 1>
else:<statement block 2>
Multi branch structure:
if <condition 1>:<statement block 1>
elif <condition 2>:<statement block 2>
...
else:<statement N>
The two branch structure has a more concise expression:
<expression 1> if <condition> else <expression 2>
The loop structure of the program:
Traverse loop:
for <loop variable> in <traverse structure>:<statement block>
Infinite loop:
while <condition>:<statement block>
String processing function:
len(x) str(x) chr(x) ord(x) hex(x) oct(x)
Common operators and functions for sequence types:
x in s, x not in s, s + t, s * n, s[i], s[i:j], s[i:j:k]
min(s), max(s), s.index(x[,i[,j]]), s.count(x), len(s)
Operator for collection type:
S - T, S -= T, S & T, S &= T, S ^ T, S ^= T, S | T, S |= T, S <= T, S >= T
The method or operation of a dictionary:
<d>.keys() <d>.values() <d>.items() <d>.get(<key>,<default>) <d>.popitem()
<d>.pop(<key>,<default>) <d>.clear() del <d>[<key>], <key> in <dict>
List type specific functions or methods:
ls[i] = x, ls[i:j] = lt, ls[i:j:k] = lt, del ls[i:j], del ls[i:j:k]
s += t or ls.extend(lt), ls *= n, ls.append(x) ls.clear() ls.copy()
ls.remove(x) ls.insert(i,x) ls.pop(i) ls.reverse(x)
Operation function or method of collection type:
S.add(x) S.clear() S.copy() S.pop() S.discard(x) S.remove(x)
len(S) S.isdisjoint(T), x in S, x not in S
Library programming:
import A
A.b()
from A import b,c
b() + c()
from A import *
b() + c() + d() + ...
<a>.<b>()
File operation function:
<variable name> = open(<file name>,<open mode>)
<variable name>.close()
<file>.readall()
<file>.read(size=-1)
<file>.readline(size=-1)
<file>.readlines(hint=-1)
<file>.write(s)
<file>.writelines(lines)
<file>.seek(offset)
声明:以上文章均为用户自行添加,仅供打字交流使用,不代表本站观点,本站不承担任何法律责任,特此声明!如果有侵犯到您的权利,请及时联系我们删除。
文章熱度:
文章難度:
文章質量:
說明:系統根據文章的熱度、難度、質量自動認證,已認證的文章將參與打字排名!

本文打字排名TOP20

登录后可见