备赛笔记:python基础笔记

1 基础数据类型
整数:对于16进制整数使用0x前缀,如0xff00

浮点数:可以使用科学计数法,如1.23e9即为1.23 * 10^9

字符串:使用 \ 代表转义字符,如 \t 为制表符 \ 为 \ \n 为换行
字符串索引从左往右从0开始,也可以从右往左-1开始
字符串可以使用单括号也可以使用双括号
在这里插入图片描述
截取字符串:[头:尾]
截取时只能从左往右,不能从右往左。就算要使用负数索引也要为[-5:-2]而非[-2:-5]

布尔值:大写True False
逻辑运算符 and or not

空格:用None表示

变量:python中变量为动态数据类型

列表:使用[ ]标示,也可以用截取字符串的方法截取列表
+ 连接列表
* 列表重复操作

list1 = [1, 2, 3]
list1[0]		# 1
list1[0:1]	# 1, 2
list2 = [4, 5, 6]
list1 + list2 = [1, 2, 3, 4, 5, 6]
list1 * 2 = [1, 2, 3, 1, 2, 3]

列表可以同时储存不同数据类型

list2 = [0, 1, 0.4, True]
list2[2]	# 0.4

元祖:使用(), 元祖不能二次赋值

tuple = (1, 2, 3)
tuple[2]	# 3
tuple[1] = 5	# error

字典:散列表数据结构。由键和值组成,使用{}

hashTable = {"a":True, "b":False, "c":0.1, "d":4}
hashTable["a"] # True
hashTable.keys() # hashTable_keys(["a", "b", "c", "d"])
hashTable.values() # hashTable_values([True, False, 0.1, 4]) 

数据类型转换:

int(x, [,base])	# x -> int
float(x)	# x -> float
str(x)		# object -> string
tuple(x)	# x -> tuple
list(x)		# x -> list
chr(x)		# int -> char
ord(x)		# char -> int

控制语句
if语句
Python用缩进区分语句范围

if condition:
	# 1
else:
	# 2
if condition1:
	# 1
elif condition2:
	# 2
elif condition3:
	# 3
else:
	# 4

while循环

while condition:
	statement

for循环

for i in range (end):	# 0 -> end - 1
for i in range (start, end): # start -> end - 1
for i in range (start, end, step): 	# start -> end -1  start += step

foreach循环变量序列(列表,字符串等)

for iterating_var in sequence:
	statement

和java的foreach一样,python的foreach只能查找,不能修改
foreach可以遍历字符串,每一个循环为字符串从左往右一个字母

跳转语句

break
continue
pass	# 空语句,用于保证程序结构完整

函数

def 函数名称 (参数):

参数要防止圆括号中间
函数第一行可以使用文档字符串存储函数说明
函数内容以冒号开始,以缩进划分
return返回值,函数头无需声明返回值

如果要传入多个参数,在参数前加*,这样会接受多个参数并存储在一个元祖里

def function(*para):
    for item in para:
        print(item)
function("a","b","c")
plist = [1,2,3,4,5,6,7,8,9,0]
function(*plist)

如要传入字典在参数前加**

def function(**para):
    for key, value  in para.items():
        print(key,value)
pdict = {"1":"a","2":"b","3":"c","4":"d"}
printcoff(**pdict)

面向对象编程

创建类:

class 类名:

可以在第一行加入文档字符串

class Employee:
        "a class of employee"
        empCount = 0
        
        def __init__(self, name, salary):
            self.name = name
            self.salary = salary
            Employee.empCount += 1
            
        def displayCount(self):
            print("Total Employee %d" % Employee.empCount)
            
        def displayEmployee(self):
            print("Name : ", self.name, ",Salary:", self.salary)

init()为构造函数
self代表类的实例,类似于this

python创建对象没有new关键字,使用函数调用方法

emp1 = Employee("Jane", 5000)
emp2 = Employee("Jack", 3000)

用点号访问对象

empl.displayEmployee() # Name :  Jane ,Salary: 5000

python使用自动垃圾回收,不需要手动释放对象

继承

声明继承关系:

class subClass (superClass)

子类对象可以直接调用超类方法,当子类对象调用方法时会先搜索子类再搜索超类。因此可以利用子类同名方法进行重写。

示例:

class Parent:
    parentAttr = 100
    def __init__(self):
        print("suprclass constructor")
        
    def parentMethod(self):
        print("superclass method")
        
    def setAttr(self, attr):
        Parent.parentAttr = attr
        
    def getAttr(self):
        print(Parent.parentAttr)

class Child(Parent):
    def __init__(self):
        print("child constructor")
        
    def childMethod(self):
        print("child method")
c = Child()	# child constructor
c.childMethod() # child method
c.parentMethod() # superclass method
c.getAttr() # 100
c.setAttr(500)
c.getAttr() # 500

使用 __ (双下划线)定义私有属性

__private

注:python的私有属性其实是伪私有属性,本质上是把__private变为了_(class name)__private

多继承

1 菱形结构

class A():
    def out_text(self):
        print('from A')

class B(A):
    def out_text(self):
        print('from B')

class C(A):
    def out_text(self):
        print('from C')

class D(B,C):
    pass

obj = D()

在这里D继承B和C,B和C都继承A,继承关系形成一个菱形

obj.out_text()  # from B
print(D.mro())  #[<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>]

这里查找超类方法的顺序按照mro,在这里可以看到先找B后找C,所以最后结果为from B

对于非菱形结构,经典类(没有继承object)按深度优先查找,新式类(继承object)按广度优先查找。
对于python3里面所有类默认为新式类

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值