设为首页 加入收藏

TOP

Python-语法模板大全(常用)(一)
2019-01-11 18:08:22 】 浏览:86
Tags:Python- 语法 模板 大全 常用

目录

1.怎么存数据

  • 变量: age =10
  • 字符串: name = "python"
  • 列表: [1,2,3,"python"]
  • 元组: (1,2,3)(不可以更改)
  • 字典: {"a":100, "b":"666"}

2.怎么用数据

  • 数字操作符: +、-、*、/、%、//、**
  • 判断循环:
    • if判断:
    if a>10:
    b = a + 20
    if b>20:
      pass
    elif: a>8:
    pass
    else:
    pass
    • while循环
while i<5:
  # do something
  pass
  i = i + 1

while true:
  pass

3.函数

# 位置参数  
def person(name, age):
  print(name,age)

# 默认参数  

def person(name,age=20):
  print(name, age)

# 关键字参数
def person(name, age, **kw):
    print('name:', name, 'age:', age, 'other:', kw)  

person('hao', 20) # name: Michael age: 30 other: {}
person('hao', 20, gener = 'M', job = 'Engineer') # name: Adam age: 45 other: {'gender': 'M', 'job': 'Engineer'}  
extra = {'city': 'Beijing', 'job': 'Engineer'}
person('Jack', 24, **extra)  

# 命名关键字参数
def person(name, age, *, city='Beijing', job):
    print(name, age, city, job)

person('Jack', 24, job = '123')
person('Jack', 24, city = 'Beijing', job = 'Engineer')

# Combination
# 可变 + 关键字参数
def f1(a, b, c=0, *args, **kw):
    print('a =', a, 'b =', b, 'c =', c, 'args =', args, 'kw =', kw)

f1(1, 2, 3, 'a', 'b')   # a = 1 b = 2 c = 3 args = ('a', 'b') kw = {'x': 99}
f1(1, 2, 3, 'a', 'b', x=99) # a = 1 b = 2 c = 0 d = 99 kw = {'ext': None}

# 默认参数 + 命名关键字参数 + 关键字参数
def f2(a, b, c=0, *, d, **kw):
    print('a =', a, 'b =', b, 'c =', c, 'd =', d, 'kw =', kw)

f2(1, 2, d=99, ext=None) # a = 1 b = 2 c = 0 d = 99 kw = {'ext': None}

4. 类和对象

4.1. 定义类的模板

class Student(object):
    def __init__(self, name, score):
        self.__name = name
        self.__score = score

    # print(mike)
    def __str__(self):
        msg = "name: " + self.__name + "score: " + str(self.__score)
        return msg

    # mike
    __repr__ = __str__
    # mike()
    __call__ = __str__

    @property
    def name(self):
        return self.__name

    @name.setter
    def name(self, value):
        if type(value) == str:
            self.__name = value
        else:
            raise ValueError('Bad name')

    @property
    def score(self):
        return self.__score

    @score.setter
    def score(self, value):
        if 0 <= value <= 100:
            self.__score = value
        else:
            raise ValueError('Bad score')

    def final_report(self):
        if self.__score >= 90:
            level = 'A'
        elif self.__score >= 70:
            level = 'B'
        elif self.__score >= 60:
            level = 'C'
        else:
            level = 'D'
        msg = "Your final value is: " + level
        return msg

# 调用

mike = Student('mike', 85)
print("-" * 20 + "Print property" + "-" * 20)
print(mike)
print("name: %s" % (mike.name))
print("-" * 30 + "Print methods" + "-" * 20)
print(mike.final_report())
print("-" * 30 + "Print modified infor" + "-" * 20)
mike.name = "Obama"
mike.score = 50
print("-" * 30)
print("modified name: %s" % (mike.name))
--------------------Print property--------------------
name: mikescore: 85
name: mike
------------------------------Print methods--------------------
Your final value is: B
------------------------------Print modified infor--------------------
------------------------------
modified name: Obama

4.2.继承

class SixGrade(Student):
    def __init__(self, name, score, grade):
        super().__init__(name, score)
        self.__grade = grade

    # grade是一个只读属性
    @property
    def grade(self):
        return self.__grade

    def final_report(self,
编程开发网
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇OpenCV中图像的读取,显示与保存 下一篇Python使用LDAP做用户认证

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

array(4) { ["type"]=> int(8) ["message"]=> string(24) "Undefined variable: jobs" ["file"]=> string(32) "/mnt/wp/cppentry/do/bencandy.php" ["line"]=> int(214) }