变量

  1. 标识符的定义大体与java相同
    使用help(“keywords”)查看被python占用的关键字,不能用做标识符

    help(“keywords”)

    Here is a list of the Python keywords. Enter any keyword to get more help.

    False def if raise
    None del import return
    True elif in try
    and else is while
    as except lambda with
    assert finally nonlocal yield
    break for not
    class from or
    continue global pass

  2. 列表操作
    列表长度可变,向列表中添加元素后,列表地址不变
    c = [1,2,3]
    id©
    2349932378312
    c.append(4)
    id©
    2349932378312

  3. 访问元组内的列表
    b=(1,2,3,[5,6,8])
    b[3][1]
    6
    元组中的列表是可变的
    b[3][1] = 9
    b[3][1]
    9

  4. true和false
    int和float 0被认为是false,非0表是true
    str中,空字符串表是false,否则就为true

  5. ==和is
    ==判断值是否相等,is比较的是两个变量身份(内存地址)是否相等

  6. 判断变量类型的函数isinstance()
    a[1,2,3]
    isinstance(a,list)
    True
    判断a是不是后面元组中类型的任意一种
    isinstance(a,(int,str,float))

流程控制语句

  1. 条件
    if else elif

  2. 循环
    for in range while

  3. 分支

python中__init__.py文件的作用

参考博客

函数

  1. python自带的函数
    print(),输出
    round(),保留小数点后几位,进行四舍五入
  2. 自定义函数
    关键字 def
    格式:def name(参数列表):

面向对象——类的定义

格式:class 类名:
类中方法的定义
class Person:
def print_person(self):
print('name: ’ + self.name)
print('sex: ’ + self.sex)
print(dir(print_person))
输出结果:[‘annotations’, ‘call’, ‘class’, ‘closure’, ‘code’, ‘defaults’, ‘delattr’, ‘dict’, ‘dir’, ‘doc’, ‘eq’, ‘format’, ‘ge’, ‘get’, ‘getattribute’, ‘globals’, ‘gt’, ‘hash’, ‘init’, ‘init_subclass’, ‘kwdefaults’, ‘le’, ‘lt’, ‘module’, ‘name’, ‘ne’, ‘new’, ‘qualname’, ‘reduce’, ‘reduce_ex’, ‘repr’, ‘setattr’, ‘sizeof’, ‘str’, ‘subclasshook’]

内置的dir()方法查询对象的方法列表

类是现实世界或思维世界中的实体在计算机中的反应
它将数据以及这些数据上的操作封装在一起

实例方法与类方法

实例方法定义

def normalMethod(self, 参数列表)

哪个对象调用方法,self就是哪个对象的引用
类方法定义

@classmethod
def classmethod(cls)

cls代表的是类

  • 静态方法
@staticmethod
def staticMethod(data)

公有私有

私有:用双下划线__
私有后,默认将名字改变,所以不能再访问私有变量,系统改成_classname__name

继承

class Student(Person):
Person是父类
python中支持多继承