【Python】Python3 基础

Python数据类型

  • Python3提供六种标准数据类型
    • 数字:Number
    • 字符串:String
    • 列表:List
    • 元祖:Tuple
    • 集合:Set
    • 字典:Dictionary
  • 常用函数
    • len(obj):返回长度,元素个数,或键值对个数
    • type(a):返回变量的具体类型,包括数字类型的int, float等,且不会认为子类型是父类型
    • isinstance(a,int):会认为子类型是父类型

1. Number

  • int
  • bool :True | False
  • float
  • complex
    • Python支持复数,复数由实数部分和虚数部分构成,可以用a + bj,或者complex(a,b)表示, 复数的实部a和虚部b都是浮点型
  • 特性:不可变
  • 常用函数
    • del var1[, var2]:删除一个或多个变量对象引用
  • 注意事项
    • 数值的除法包含两个运算符:/ 返回一个浮点数,// 返回一个整数。
    • 在混合计算时,Python会把整型转换成为浮点数。

2. String

  • 引号

    单引号和双引号意义相同,三引号可制定多行字符串

  • 转义

    \ 表示转义,在字符串前加上r使字符串中的转义字符失效

  • 字符串拼接

    + 可用于拼接字符串,* 可用于重复输出字符串
    变量对象拼接字符串的三种方式

    oldList = [1, 2, 3, 4, 5]  
    newList = oldList[0:3]  
    

    1.用%s%d等像C语言的预定义输出格式,再用%指定变量对象,多个参数就用括号%s%s%s %(a,b,c)

    print("newList is %s ." % newList)  
    str1 = "this" + " is list : %s" % newList  
    print(str1)
    

    2.使用str()函数,就可以和字符串一样用+号连接

    print("this " + str(newList) + " is list")
    

    3.使用英文半角字符的逗号连接

    print("this ", newList, " is new list")
    
# 变量对象拼接字符串的三种方式
oldList = [1, 2, 3, 4, 5]
newList = oldList[0:3]

# 1.用%s%d等类C语言的预定义输出格式,再用%指定变量对象,多个参数就用括号%s%s%s %(a,b,c)
print("newList is %s ." % newList)
str1 = "this" + " is list : %s" % newList
print(str1)

# 2.使用str()函数,就可以和字符串一样用+号连接
print("this " + str(newList) + " is list")

# 3.使用英文半角字符的逗号连接
print("this ", newList, " is new list")          
  • 字符串截取

    str [ startIndex : endIndex+1 [ : stepLength ] ]
    startIndex : 需要截取字符串的首位索引
    endIndex : 需要截取字符串的末位索引
    stepLength : 从上述已经截取的字符串结果中再次截取索引为0, stepLength, stepLength * 2, stepLength * 3....的字符组成新的字符串

  • 索引

    正向索引首位为0,反向首位为-1

3. List

  • 特性:用 [] 声明为list
  • 常用函数
    • list.append(new_thing),添加元素到列表的最后
    • del list[index],删除指定索引的元素
    • pop(), 弹出
    • remove(ele), 移除第一个ele的匹配项
    • insert(index,ele), 插入指定的索引位置
    • count(ele), 返回ele在list中出现的次数
  • 列表与运算符
    • ‘+’,两个列表可以用加号拼接成一个列表
    • ‘*’,连续拼接自身列表n次
    • [n:m], 截取,左开右闭,类似于[n_index,m_index)取整
    • list[i:],截取索引为1及其之后的所有元素
    • list[i:j],截取索引为i到索引为j-1的所有元素

4. Tuple

  • 格式:(1,2,3,4,5)
  • 和Number和String一样不可变

5. Set

  • 特性:用 {} 声明为set,如果声明成空集合应该用set(),{}代表空字典
  • 常用函数
    • set.add(ele)
    • set.update(ele) : 也可用于增添元素
    • set.remove(ele)
    • set.discard(ele) : 也可用于移除元素
    • set.pop()
      • set 集合的 pop 方法会对集合进行无序的排列,然后将这个无序排列集合的左面第一个元素进行删除。随机弹出一个元素,且函数返回这个元素
    • len(set)
    • set.clear()
    • set.intersection(set1) 交集
    • set.union(set1) 并集
    • set.difference(set) 差集
  • 集合与运算符
    • & : set1 & set2 返回两个集合的交集
    • | : set1 | set2 返回两个集合的并集
    • - : set1 - set2 返回两个集合的差集

6. Dictionary

  • 特性:dict =
  • 常用函数 :
    - dict.keys()
    - dict.values()
    - dict.items()
  • 常用操作 :
# format    {obj : obj}
num = 10
set0 = {1, 2, 3, 4, 4, 3}
arr = [5, 6, 7, 8]
s = "key4"
# list can't be a key,'list' is a type unhashable
# dictionary = {'key1': num, 'key2': lists0, 'key3': 'str1', arr: False}
dictionary = {'key1': num, 'key2': set0, 'key3': 'str1', s: False, num: 100}
print("key1's value is %d " % dictionary['key1'])
print("s's value is %s " % dictionary[s])
print("key4's value is %s " % dictionary['key4'])
print("num's value is %s " % dictionary[num])
print("10's value is %s " % dictionary[10])
# add a K-V
dictionary["key5"] = "value5"
print(dictionary)
# del a K-V
del dictionary[s]
print(dictionary)
# update a K-V
dictionary['key3'] = 'i\'m 3'
print(dictionary)
  • 练习:
# practice
# 公司小组调休,要求从11月3号到11号(3号星期2,11号星期三,中间周末两天正常休)之间选两天,这两天需要落在所在的这两周里,也就是一周休一天且不能同时选择星期一和星期五,12个人怎么排
# 12:A,B,C,D,E,F,G,H,I,J,K,L     其中C和G需要在星期一或者星期五其中一天休,E需要在4号休,J和H不能同时休     一共有多少中符合的情况,放在二维列表中打印
# tips:每次执行都是符合条件的随机结果,如果需要加条件就在最后一个函数里加if-else语句,并且修改flag条件个数,持续重构ing
four = 4
five = 5
six = 6
nine = 9
ten = 10
eleven = 11
week_print = {four: " 11月4号,星期三 : ",
              five: " 11月5号,星期四 : ",
              six: " 11月6号,星期五 : ",
              nine: " 11月9号,星期一 : ",
              ten: " 11月10号,星期二 : ",
              eleven: " 11月11号,星期三 : "}
week_dictionary = {0: "Wednesday",
                   1: "Thursday",
                   2: "Friday",
                   3: "Monday",
                   4: "Tuesday",
                   5: "Wednesday"}
week_order = {four: 0,
              five: 1,
              six: 2,
              nine: 3,
              ten: 4,
              eleven: 5}
employ = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L']


def get_random_no_repeat_list(length):
    random_list = []
    while len(random_list) < length:
        temp_random = random.randint(0, 11)
        if temp_random not in random_list:
            random_list.append(temp_random)
    print("random_set is", random_list)
    return random_list


def get_temp_table():
    temp_table = [[], [], [], [], [], []]
    list0 = get_random_no_repeat_list(12) + get_random_no_repeat_list(12)
    index = 0
    for i in range(6):
        for j in range(4):
            temp_table[i].append(employ[list0[index]])
            index += 1
    return temp_table


print(get_temp_table())


def get_result_table():
    table = []
    # condition num == 5
    flag = 0
    while flag < 5:
        table = get_temp_table()
        friday_index = list(week_dictionary.keys())[list(week_dictionary.values()).index('Friday')]
        monday_index = list(week_dictionary.keys())[list(week_dictionary.values()).index('Monday')]
        # find Friday Compensatory people list
        friday_list = table[friday_index]
        monday_list = table[monday_index]
        if set(friday_list) & set(monday_list) == set():
            flag += 1
        else:
            flag = 0
            continue
        if 'C' not in friday_list and 'C' not in monday_list:
            flag = 0
            continue
        else:
            flag += 1
        if 'G' not in friday_list and 'G' not in monday_list:
            flag = 0
            continue
        else:
            flag += 1
        four_list = table[week_order[four]]
        if 'E' not in four_list:
            flag = 0
            continue
        else:
            flag += 1
        for day_list in table:
            if 'J' in day_list and 'H' in day_list:
                flag = 0
                continue

        flag += 1
    return table


compensatory_table = get_result_table()
for i in range(6):
    print(week_print[list(week_order.keys())[list(week_order.values()).index(i)]], end='')
    for j in range(4):
        print(compensatory_table[i][j], end='')
    print("")

【待完善】
数据类型:
特性,常用函数,注意事项
循环语句:
while,for
迭代器和生成器:
迭代器,生成器
函数:
规则,重载,命名
数据结构(容器接口API):
字符串,列表,字典,元组。。。
模块:
输入输出:
输入,输出
文件处理:
输入流,输出流,字节流,字符流,写入文件,读出文件
操作系统:
错误和异常:
错误,异常
面向对象:
封装,继承,多态,类,局部变量(单下划线:受保护的 ,双下划线:私有的),重写,单继承,多继承,构造器
命名空间作用域:
命名空间,作用域
常用标准库:
实例测验:
========上面记录进入Python基础,下面的每一点都可单独一个文章笔记
Python正则表达式:
数据解析:
XML解析,JSON解析
web开发:
数据库:
网络编程:
协议,TCP,UDP,SMTP,爬虫
多线程:
数据分析:

posted @ 2021-07-27 22:27  拉布  阅读(45)  评论(0编辑  收藏  举报