入门Python3基础教程-知识点摘要

语法规则

  • 变量语法
      • 所有标识符可以包括英文、数字以及下划线(_),但不能以数字开头,不能用Python3的关键字,如if while 等
      • 大小写敏感
        • a = 1 A = 2 是两个不同的变量
  • 注释语法
      • # 啦啦啦啦

数据类型

  • 数值
      • 整数
        • a = 1
        • b = 0x10
      • 浮点数
        • c = 1.2
        • d = .5
        • e = .314e1
      • 操作符运算
        • print(1 + 1) # 2
        • print(2 – 1) # 1
        • print(3 * 2) # 6
        • print(5 / 4) # 1.25
        • print(2 ** 10) # 1024
        • print(5 // 4) # 1
        • print(5 % 4) # 1
      • 类型转换
        • float(3) # 3.0
        • float(‘3.14’) # 3.14
        • int(3.14) # 3
  • 字符串
      • s = ‘Dog’ # 单引号表示字符串
      • s1 = “Dogge’s Home” # 双引号表示字符串
      • s2 = “”” fdfsdfsdsfs “”” # 允许写多行
      • 长度
        • len(s1)
      • 切片
        • s[0:2], s[1:-5] # -5表示从后往前数第五个字符
      • 连接两个字符串
        • ‘abc’ + ‘efg’
      • 类型转换
        • str(3.14) # ‘3.14’
        • str(6) # ‘6’
        • str([1,2,3])
        • str({‘k1’: ‘v1’, ‘k2’: ‘v2’}
  • Byte字节
      • byte_data = b’abc’
      • print(byte_data[0] == 97) # True
      • len(byte_data)
  • None
      • 空类型,表示空字符串,空列表等等

数据结构

  • List 列表
    • lis = [‘python’, 3, ‘is’, ‘cool’]
    • len(lis) # 列表长度
    • 切片
      • lis[0]
      • lis[-1]
      • lis[1:-1] # -1表示最后一个元素
    • 操作
      • ls[0] # 访问下标0, 输出python
      • lis.append(3.14) # 插入3.14在最后 ‘python’, 3, ‘is’, ‘cool’, 3.14
      • lis.insert(2, ‘pic’) # 在下标2的位置插入’pic‘ ‘python’, 3, ‘pic’ ‘is’, ‘cool’, 3.14
      • lis.extend([‘hello’, ‘world’]) # 扩展一个list ‘python’, 3, ‘pic’ ‘is’, ‘cool’, 3.14, ‘hello’, ‘world’
      • lis.pop() # 弹出’world’, lis 变成 ‘python’, 3, ‘pic’ ‘is’, ‘cool’, 3.14, ‘hello’
      • lis.pop(2) # 弹出 ‘pic’ lis 变成’python’, 3, ‘is’, ‘cool’, 3.14, ‘hello’
      • lis.index(‘is’) # 查找 ‘pic’ 返回 2
      • 遍历
        • for elem in lis:
  • Tuple 元组, 不可变列表
    • tp = (1, 2, 3, [4, 5])
    • print(len(tp)) # 输出4
    • print(tp[2]) # 3
    • 单个元素 tp = (1,)
  • Set集合
    • st = set([‘s’, ‘e’, ‘T’])
    • st.add(‘t’) # 添加元素t, st = {‘s’, ‘e’, ‘t’, ‘T’}
    • st.add(‘t’) # 添加元素t, st不变,因为集合元素不可重复
    • s.remove(‘T’) # 删除元素 st = {‘s’, ‘e’, ‘t’}
  • Dict字典
    • dic = {‘k1’: ‘v1’, ‘k2’: ‘v2’}
    • print(len(dic)) # 2
    • print(dic[‘k2’]) # ‘v2’,访问k2为key的值
    • dic[‘k2’] = ‘v3’ # 新增,更新 k2 的值
    • print(dic[‘k2’]) # ‘v3’, 访问k2的值
    • 遍历
      • for key in dic:
    • print(‘k2’ in dic) # dic是否包含k2 , True

控制流

  • 选择控制流
if sys.version_info.major < 3:
    print('version 2.x')
elif sys.version_info.major > 3:
    print('future')
else:
    print('version 3.x')
  • for 实现循环控制流
for i in 'hello':
    print(i)
  • while 实现循环控制流
prod = 1
i = 1
while i < 10:
    prod = prod * i
    i += 1
print(prod)
  • break, continue
for i in range(2, 10):
    if n % 2 == 0:
        print('found an even number ', n)
        continue # 结束本次循环
    if n > 5:
        print('greater than 5')
        break # 跳出循环

生成器

def reverse(data):
    for index in range(len(data)-1, -1, -1):
        yield data[index]
nohtyp = reverse('Python')
print(nohtyp)
for i in nohtyp:
    print(i)

面向过程–函数

def foo(val, val2): # 函数定义
    return val * 10 + val2
a = foo(2, 1)  # 函数调用
print(a)

面向对象–类和对象

class Animal: # 定义一个Animal类
    def __init__(self, can_fly = False):
        self.can_fly = can_fly
    def fly(self):
        if self.can_fly:
            print('I can fly!')
        else:
            print('I can not fly')

a = Animal()  # a是属于Animal类的对象
a.fly() 

b = Animal(True)  # b是属于Animal类的对象
b.fly()

代码管理

  • module
    • Python 模块(Module),是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和Python语句
  • package
    • 包是一个分层次的文件目录结构,它定义了一个由模块及子包,和子包下的子包等组成的 Python 的应用环境
    原文作者:凯威讲堂
    原文地址: https://zhuanlan.zhihu.com/p/38743486
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞