基础语法
类型转换
a = 3.3
int ( a)
b = 123
str ( b)
round ( 15.4 )
字符串部分操作
str1= '1,2,4'
str1. split( ',' )
str2= ' '
str2. join( str1)
str1= str1. replace( '4' , '3' )
str . upper( )
str . lower( )
str . isalpha( )
str . isdigit( )
s2. find( s1)
str . lstrip( )
list列表
a = [ 1 , '2' , 3.0 , 4 ]
a[ 0 ]
a[ 1 : ]
a[ - 1 ]
a= [ 1 , 2 , 3 , 4 , 5 ]
a[ 2 : 4 ]
del a[ 0 ]
3 in a
b= [ 1 , 2 , [ 3 , 4 ] ]
b[ 2 ] [ 1 ]
b. count( 1 )
a. index( 1 )
a. append( 'hello' )
a. insert( 2 , 'hello' )
a. remove( 1 )
a. pop( 1 )
a. sort( )
c= sorted ( a)
a. reverse( )
字典 没有先后顺序
a= { }
a= dict ( { 1 : 2 , 2 : 3 } )
b= [ 1 ,2 ]
c= [ 3 ,4 ]
d= dict ( [ b, c] )
a[ 'hello' ] = 'world'
a[ 'hello' ] = '1'
a[ 'hello' ]
a[ 1 ] += 1
a. pop( 1 )
b= { 3 : 4 }
a. update( b)
1 in a
a. keys( )
a. values( )
a. items( )
集合
t = [ 1 , 1 , 3 , 4 ]
t= set ( t)
t= set ( [ 1 , 2 , 3 , 4 , 4 ] )
a= { 1 , 2 }
b= { 2 , 3 }
a. union( b) a| b
a. intersection( b) a& b
a. difference( b) a- b
b. issubset( a)
a. add( 4 )
a. update( [ 4 , 5 , 6 ] )
a. remobe( 2 )
a. pop( )
赋值机制
a= 1
b= 1
a= 1000
b= 1000
判断
a= 1000
if a > 50 :
print ( 'A' )
elif 50 > a> 20 :
print ( 'B' )
else :
print ( 'S' )
循环
a= 0
while a < 10 :
print ( a)
a += 1
a= set ( [ 1 , 2 , 3 ] )
for i in a:
print ( i)
a= [ 1 , 2 , 3 , 4 , 5 , 6 ]
for i in range ( 6 )
print ( a[ i] )
函数
def add ( a, b) :
print ( a+ b)
return ( a+ b)
a= 4
b= 6
c= add( a, b)
def add1 ( a, * args) :
def add2 ( a, ** kwarge) :
return a, i
模块
% % writefile 1. py
a= 3
print ( a)
% run 1. py
import 1
print ( a)
import 1 as 2
from 1 import a
异常
import math
for i in range ( 10 ) :
try :
a= input ( 'number' )
if a == '1' :
break
result= math. log( float ( a) )
print ( result)
except ValueError:
print ( 'ValueError' )
except exception:
print ( 'error' )
try :
print ( 'hello' )
finally :
print ( 'finally' )
except exception:
print ( 'error' )
文件操作
txt = open ( './123.txt' )
txt_read= txt. read( )
print ( read)
line= txt. readlines( )
print ( line)
txt. close( )
txt = open ( './123.txt' , 'w' )
txt. write( '123\n' )
txt. close( )
with open ( './123.txt' ) as f:
f. read( )
类
class people :
def __init__ ( self, name, age) :
self. name= name
self. age= age
def display ( self) :
print ( self. name)
p1 = people( 'a' , 20 )
p2 = people( 'b' , 30 )
p1. display( )
p1. name = 'c'
hasattr ( p1, 'name' )
getattr ( p1, 'name' )
delattr ( p1, 'name' )
继承
class parent :
def __init__ ( self) :
print ( '调用父类构造' )
def parentM ( self) :
print ( '调用父类方法' )
def setAttr ( self, attr) :
parent. parentAttr = attr
def getAttr ( self) :
print ( '属性:' ,parent. parentAttr)
def newM ( self) :
print ( '重写夫' )
class child ( parent) :
def __init__ ( self) :
print ( '调用子构造' )
def childM ( self) :
print ( '调用子方法' )
def newM ( self) :
print ( '子重写' )
c = child( )
c. childM( )
c. parentM( )
c. setAttr( 100 )
c. getAttr( )
c. newM( )
时间模块
import time
print ( time. time( ) )
time. localtime( time. time( ) )
time. asctime( time. localtime( time. time( ) ) )
time. strftime( '%Y-%m-%d %H:%M:%S' , time. localtime( time. time( ) ) )
import calendar
calendar. month( 2021 , 11 )