|
Class class A
Object a =
Instance = A()
|
|
class (static) variable
Instance varialbe in the __init__(self)
__private
_protected public static variable |
| def __init__(self): instance variable |
|
import module
from module import function
|
| comments |
| To help you and your team to make your code more readable and maintainable.
#Single line comment Common Tags: |
| = (assignment) |
|
if __name__ == “__main__”:
….
|
| Run it only when it’s the main |
|
import openpyxl
wb = openpyxl.Workbook()
ws = wb.active
ws.title = “Start Coding”
wb.save(‘autoexcel.xlsx’)
|
| global a |
|
a = 1
def test_function():
global a
a = a +1
test_function()
print(a) # 2
|
|
def function_name( self ):
var = ‘ ‘
return var
|
|
Swap
a = 3
b = 5
a,b = b, a
|
| (*args) |
| function( a, b, c, …. ) |
| (**kwargs) |
| ( name=””, value=””, …… ) |
| ( …., last = True ) |
|
def function(a, b):
return a+b, a*b
|
| (x, y) |
| return |
| add = lambda a, b: a+b |
| result = add(3,4) |
| a = [llambda a, b: a++b, lambda a, b: a*b] |
| print(a[1](3,4) ) #12 |
|
def function(a, b):
return a+b, a*b
|
| (x, y) |
| return |
| f = open(“file1.txt”, ‘w’, encoding=”UTF-8″) |
|
for i in range(1, 11):
f.write(i)
|
|
f.close()
|
|
f = open(“file1.txt”, ‘r’)
|
|
while True:
line = f.readline()
if not line: break
print( line )
|
|
lines = f.readlines()
for line in lines:
print( line )
|
|
data = f.read( )
print( data )
|
|
for line in f:
print( line )
|
| f.close() |
|
with open(“file1.txt”, “w”) as f:
|
| f.write(“”) |
|
import sys
args = sys.argv[1:]
for i in args:
print( int(i), end=’ ‘ )
|
| PS F:\cs\program\back_end\python\app\csapp> python .\index.py 1 2 3 1 2 3 |