Python Overview

Python Overview

数据类型

  • Nummbers

    1
    a = 1
  • Strings

    1
    s = 'python'
  • Lists

    1
    squeres = [1, 4, 9, 25]

控制流

  • if 语句

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    x = int(input("请输入一个数字:"))
    if x < 0:
    x = 0
    print('Negative changed to zero')
    elif x == 0:
    print('Zero')
    elif x == 1:
    print('Single')
    else
    print('More')
  • for 语句

    1
    2
    3
    words = ['cat', 'windows', 'defenestrate']
    for w in words:
    print(w, len(w))
  • while 语句

    1
    2
    3
    4
    a, b = 0, 1
    while a < 10:
    print(a)
    a, b = b, a + b
  • range() 函数

    1
    2
    for i in range(5):
    print(i)
  • break 和 continue 语句,循环中的 else 从句

    1
    2
    3
    4
    5
    6
    7
    for n in range(2, 10):
    for x in range(2, n):
    if n % x == 0:
    print(n, 'equals', x, '*' n//x)
    break
    else:
    print(n, 'is a prime number')
  • pass 语句

    1
    2
    while True:
    pass
  • 函数定义

    1
    2
    3
    4
    5
    6
    7
    8
    def fib(n):
    a, b = 0, 1
    while a < n:
    print(a, end=' ')
    a, b = b, a + b
    print()

    fib(2000)

Python 代码风格

  • 使用4个空格缩进,不使用Tab缩进
  • 每行代码不超过79个字符
  • 函数和类之间用空行隔开,大块代码段也用空行隔开
  • 注释单独占一行
  • 使用文档字符串
  • 操作符两侧和逗号后面留空格,但括号里的操作符两侧不加空格
  • 统一类名和函数名,推荐类名用驼峰命名:SampleClass,函数名用小写和下划线:sample_function(),尽量用 self 作为方法的第一个参数
  • 使用 UTF-8 和 ASCII 编码
  • 不要使用非 ASCII 编码

数据结构

  • Lists
    1. list.append(x)
    2. list.extend(iterable)
    3. list.insert(i, x)
    4. list.remove(x)
    5. list.pop([i])
    6. list.clear()
    7. list.index(x[, start[, end]])
    8. list.count(x)
    9. list.sort(key=None, reverse=False)
    10. list.reverse()
    11. list.copy()
    12. List 用作栈
    13. List 用作队列
    14. List 嵌套
  • del 语句
  • 元组 Tuples 和有序

    1
    2
    3
    4
    5
    6
    7
    8
    9
    a = 1, 2, 3
    print(a[0])
    # 1
    print(a)
    # (1, 2, 3)
    t = (a, 4, 5)
    print(t)
    # ((1, 2, 3), 4, 5)
    x, y, z = t
  • 集合 Sets

    1
    2
    3
    4
    5
    6
    7
    8
    basket = {'apple', 'orange', 'apple', 'lemon'}
    print(basket)
    # {'orange, 'apple', 'lemon'}
    'orange' in basket
    # True
    a = set('abcacd')
    print(a)
    # {'a', 'b', 'c', 'd'}
  • 字典 Dictionaries

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    tel = {'jack': 4098, 'sape': 4139}
    tel['guido'] = 4127
    print(tel)
    # {'jack': 4098, 'sape': 4139, 'guido': 4127}
    list[tel]
    # ['jack', 'sape', 'guido']
    dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
    # {'sape': 4139, 'guido': 4127, 'jack': 4098}
    dict(sape=4139, guido=4127, jack=4098)
    # {'sape': 4139, 'guido': 4127, 'jack': 4098}

遍历

  • 使用 items() 方法

    1
    2
    3
    4
    5
    person = {'name': 'itroger', 'age': '25'}
    for k, v in person.items():
    print(k, v)
    # name itroger
    # age 25
  • 使用 enumerate() 函数

    1
    2
    3
    4
    5
    for i, v in enumerate(['a', 'b', 'c'])
    print(i ,v)
    # 0 a
    # 1 b
    # 2 c
  • 同时遍历多个序列使用 zip() 函数

    1
    2
    3
    4
    5
    6
    7
    questions = ['name', 'age', 'city']
    answers = ['itroger', 25, 'Guangzhou']
    for q, a zip(questions, answers)
    print(q, a)
    # name itroger
    # age 25
    # city Guangzhou

模块 Modules

  • 引用模块

    1
    2
    3
    4
    5
    import numpy
    from matplotlib import pyploy
    from numpy import *
    import numpy as np
    from matplotlib.pyplot as plt
  • 执行模块

    1
    2
    3
    4
    python numpy.py

    if __name__ == "__main__":
    import sys

包 Packages

  • 包含 __init__.py 文件用于初始化包

输入输出

  • 输出格式
    1
    2
    3
    print('name: {}, city: {}'.format('itroger', 'Guangzhou'))
    print('name: {1}, city: {0}'.format('Guangzhou', 'itroger'))
    print('name: {name}, city: {city}'.format(name='itroger', city='Guangzhou'))

  • 类定义

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    class Complex:

    age = 25

    def __init__(self, name, city):
    self.name = name
    self.city = city

    def say_name(self):
    print(self.name)

    person = Complex('itroger', 'city')
  • 继承

    1
    2
    class DerivedClassName(modname.BaseClassName):
    class DerivedClassName(Base1, Base2, Base3)
  • 私有变量

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    class Mapping:
    def __init__(self, iterable):
    self.items_list = []
    self.__update(iterable)

    def update(self, iterable):
    for item in iterable:
    self.items_list.append(item)

    __update = update

    class MappingSubclass(Mapping):
    def update(self, keys, values):
    for item in zip(keys, values):
    self.items_list.append(item)

迭代器

  • for 语句

    1
    2
    for element in [1, 2, 3]:
    print(element)
  • iter() 和 next()

    1
    2
    3
    s = 'abc'
    it = iter(s)
    print(next(it))
-------------本文结束 感谢您的阅读-------------
您的支持将鼓励我继续创作!