龙空技术网

python学习笔记(基本语法+脚本)

Anthony-Dong 1029

前言:

如今小伙伴们对“python笔记考点”大致比较讲究,姐妹们都想要知道一些“python笔记考点”的相关知识。那么小编在网摘上汇集了一些有关“python笔记考点””的相关知识,希望小伙伴们能喜欢,姐妹们一起来了解一下吧!

基本语法1. 简单控制语句

字符串推荐用'' 单引号引用

list: List[int] = [1, 2, 3]for elem in list:    if elem > 1:        print(f'data {elem} > 1')  # 这里是format语句,属于语法糖    else:        print(f'data {elem} < 1')'''data 1 < 1data 2 > 1data 3 > 1'''
2. 异常
x = -1try:    if x < 0:        raise Exception("Sorry, no numbers below zero")except Exception as err:    print("find err: %s" % err)'''find err: Sorry, no numbers below zero''' 
3. 推导式(比较晦涩难懂)参考:

推导式好处: 效率更高,底层是c执行

1. 列表推导式

一共两种形式:(参考: ) , 它主要是输出是列表(list)

[xforxindataifcondition] 这里的含义是data只有满足if条件中的情况才保留 (if)[exp1ifconditionelseexp2forxindata] , 这里的含义是data满足if条件时执行exp1 否则 exp2 (if - else)

import re"""获取所有的数字"""list = ["1", "2", "3", "4", "5", "a", "b", "c"]print([elem for elem in list if re.match("\\d", elem)])'''['1', '2', '3', '4', '5']'''"""获取所有的字母"""print([elem for elem in list if re.match("[a-z]", elem)])'''['a', 'b', 'c']'''"""如果元素是数字则存储,否则则upper"""print([elem if re.match("\\d", elem) else elem.upper() for elem in list])'''['1', '2', '3', '4', '5', 'A', 'B', 'C']'''

最佳实践: 参考()

def decode_raw_args(        args: List[Union[str, bytes]],        stdin_encoding: str) -> List[str]:    """    Convert all bytes args to str    by decoding them using stdin encoding.    """    return [        arg.decode(stdin_encoding)        if type(arg) is bytes else arg        for arg in args    ]def decode_raw_args_parse(        args: List[Union[str, bytes]],        stdin_encoding: str) -> List[str]:    """    Convert all bytes args to str    by decoding them using stdin encoding.    不使用推导式    """    result: List[str] = []    for arg in args:        if type(arg) is bytes:            result.append(arg.decode(stdin_encoding))        else:            result.append(arg)    return result# arg.decode(stdin_encoding) if type(arg) is bytes else arg for arg in argsprint(decode_raw_args(args=[b'111', b'222'], stdin_encoding="utf-8"))print(decode_raw_args(args=["111", "222"], stdin_encoding=""))'''['111', '222']['111', '222']'''print(decode_raw_args_parse(args=[b'111', b'222'], stdin_encoding="utf-8"))print(decode_raw_args_parse(args=["111", "222"], stdin_encoding=""))'''['111', '222']['111', '222']'''
2. 字典推导式

{key_expr:value_exprforvalueincollectionifcondition} ,输出是dict

"""{ key_expr: value_expr for value in collection if condition }反转key value,且获取 value 为在set {'a', 'b', 'c'}中的元素"""dict_old = {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}print({dict_old[value]: value for value in dict_old if value in {'a', 'b', 'c'}})'''{'A': 'a', 'B': 'b', 'C': 'c'}'''print({key: value for value, key in dict_old.items() if value in {'a', 'b', 'c'}})'''{'A': 'a', 'B': 'b', 'C': 'c'}'''
3. 集合推导式

表达式:

{exprforvalueincollectionifcondition}{exp1ifconditionelseexp2forxindata}输出是set

其实就是上面列表推导式[] 换成{} ,输出由list 变成了set

4. for 循环 迭代器

import osfrom collections.abc import Iterablewith open("text.log", "wt") as file:    file.truncate()    file.writelines("line 1" + os.linesep)    file.writelines("line 2" + os.linesep)    file.writelines("line 3" + os.linesep)    passwith open("text.log", "rt") as file:    for line in file:        print("type: {type}, isinstance: {isinstance}, line: {line}".format(type=type(file),                                                                            isinstance=isinstance(file, Iterable),                                                                            line=line))    pass'''type: <class '_io.TextIOWrapper'>, isinstance: True, line: line 1type: <class '_io.TextIOWrapper'>, isinstance: True, line: line 2type: <class '_io.TextIOWrapper'>, isinstance: True, line: line 3'''

这里面_io.TextIOWrapper 实现了 __next__() 方法

比如我们自己实现一个可迭代的对象

下面可以看到我使用了类型申明List[str] 其实这个python运行时并不会检测,需要工具进行检测!

变量默认都是Any 类型 ,具体可以看

from typing import Listclass Items(object):    def __init__(self, list: List[str]):        self.list = list        self.index = 0    def __next__(self, *args, **kwargs):        """        next,没有抛出StopIteration        """        if self.index >= len(self.list):            raise StopIteration        result = self.list[self.index]        self.index = self.index + 1        return result    def __iter__(self, *args, **kwargs):        """        返回一个迭代器        """        return selfdata = Items(["1", "2", "3"])for x in data:    print(x)'''123'''
5. 包管理
from ..a import foo  # 上级目录from .a import foo_a  # 当前目录import sys  # 引用源码或者libfrom copy import deepcopy  # 引用源码或者libfrom pygments.formatters.terminal import TerminalFormatter  # 引用 lib.lib.fileimport demo.utils.adef c_foo():    demo.utils.a.foo_a()    TerminalFormatter()    deepcopy()    print(sys.api_version)def b_foo():    foo()
基本数据类型1. 定义方式mylist:list[str]=["apple","banana","cherry"]mylist=["apple","banana","cherry"]

Text Type:

str

Numeric Types:

int,float,complex

Sequence Types:

list,tuple,range

Mapping Type:

dict

Set Types:

set,frozenset

Boolean Type:

bool

Binary Types:

bytes,bytearray,memoryview

2. 数字基本类型

x = 1  # inty = 1.1  # floatz = 1j  # 复数(complex)a = complex(1, 2)  # 复数(complex)print(type(x))print(type(y))print(type(z))print(z.imag, z.real)print(type(a))print(a.imag, a.real)'''<class 'int'><class 'float'><class 'complex'>1.0 0.0<class 'complex'>2.0 1.0'''
3. 字符串
str = "hello"print(str)print(str[0:])print(str[:5])print(str[:-1])print(str[0:5])print(str[0:5:1])print(str[0:5:2])'''hellohellohellohellhellohellohlo'''# formatprint("My name is {} and age is {}".format("tom", 18))'''My name is tom and age is 18'''quantity = 3itemno = 567price = 49.95myorder = "I want to pay {2} dollars for {0} pieces of item {1}."print(myorder.format(quantity, itemno, price))'''I want to pay 49.95 dollars for 3 pieces of item 567.'''# funcstr = "hello world! "print(str.upper())print(str.lower())print(str.strip())print(str + " ...")'''HELLO WORLD! hello world! hello world!hello world!  ...'''# formatmyorder = "I have a {carname}, it is a {model}."print(myorder.format(carname="Ford", model="Mustang"))'''I have a Ford, it is a Mustang.'''
4. lambda

其实就是一个func

def add(num):    return lambda x: x + numprint(add(10)(10))'''20'''

lanbda 例子2

import jsonclass Obj:    def __init__(self):        self.name = "tom"        self.age = 1print(json.dumps(Obj(), default=lambda obj: obj.__dict__))'''{"name": "tom", "age": 1}'''
集合

list,tuple,range,dict,set,frozenset

list , 例如:mylist=["apple","banana","cherry"]tuple 是特殊的数组,就是不能改变, 例如mytuple=("apple","banana","cherry")range 可以理解是个迭代器, 例如:dict 就是个map, 例如:thisdict={"brand":"Ford","model":"Mustang","year":1964}set 就是个去重复的list , 例如:myset={"apple","banana","cherry"}1. list

mylist = ["apple", "banana", "cherry"]# 切片print(mylist[0])print(mylist[2])print(mylist[-1])print(mylist[0:3:2])'''applecherrycherry['apple', 'cherry']'''# 基本操作mylist.append("orange")print(mylist)'''['apple', 'banana', 'cherry', 'orange']'''mylist.insert(0, "mango")print(mylist)'''['mango', 'apple', 'banana', 'cherry', 'orange']'''# 循环for x in mylist:    print(x)'''applebananacherryorange'''for index in range(len(mylist)):    print("index: %d" % index)'''index: 0index: 1index: 2index: 3index: 4'''if "apple" in mylist:    print("success!")'''success!'''# [执行表达式(也就是for循环中的,如果有if则是if中执行的), for item in list 条件表达式]new_list = [elem.upper() for elem in mylist if "a" in elem]  # contains 'a' char elem strprint(new_list)'''['MANGO', 'APPLE', 'BANANA', 'ORANGE']'''newList = []for elem in mylist:    if 'a' in elem:        newList.append(elem.upper())print(newList)'''['MANGO', 'APPLE', 'BANANA', 'ORANGE']'''
2. map
thisdict = {"brand": "Ford", "model": "Mustang", "year": 1964}for key, value in thisdict.items():    print("key: {}, value: {}".format(key, value))'''key: brand, value: Fordkey: model, value: Mustangkey: year, value: 1964'''for key in thisdict:    print("key: {}, value: {}".format(key, thisdict[key]))'''key: brand, value: Fordkey: model, value: Mustangkey: year, value: 1964'''
3. range
# range 会生成一个迭代器,(start,end,sep) , 左闭右开for x in range(6):  # [0,1,2,3,4,5]    print("x is %d" % x)'''x is 0x is 1x is 2x is 3x is 4x is 5'''for x in range(2, 6):    print("x is %d" % x)'''x is 2x is 3x is 4x is 5'''for x in range(1, 6, 2):    print("x is %d" % x)'''x is 1x is 3x is 5'''
方法1. 定义一个空方法
def func_1():    pass  # 空方法必须申明passfunc_1()
2. 参数
# name 为必须添的参数,不然为空会报错# age 为默认参数# agrs 为可变参数# kwargs 为 k v 参数def func_1(name, age=1, *args, **kwargs):    print("name: %s" % name)    print("age: %d" % age)    print("len(args): {}, type: {}".format(len(args), type(args)))    for value in args:        print("args value: {}".format(value))    print("len(kwargs): {}, type: {}".format(len(kwargs), type(kwargs)))    for key, value in kwargs.items():        print("kwargs key: {}, value: {}".format(key, value))func_1(name="tom", age=10, args="1", kwargs="2")'''name: tomage: 10len(args): 0len(kwargs): 0, type: <class 'tuple'>len(kwargs): 2, type: <class 'dict'>kwargs key: args, value: 1kwargs key: kwargs, value: 2'''# 这里注意由于dict所以不能申明kvfunc_1("tom", 10, "1", "2", args="1", kwargs="2")'''name: tomage: 10len(args): 2, type: <class 'tuple'>args value: 1args value: 2len(kwargs): 2, type: <class 'dict'>kwargs key: args, value: 1kwargs key: kwargs, value: 2'''
3. 类型

申明输入输出类型

from typing import List, Uniondef decode_raw_args(    args: List[Union[str, bytes]],    stdin_encoding: str) -> List[str]:    """    Convert all bytes args to str    by decoding them using stdin encoding.    """    return [        arg.decode(stdin_encoding)        if type(arg) is bytes else arg        for arg in args    ]
类1. 定义类和方法
# 如果没有父类继承,这里选择 object,比较规范class Person(object):    # gender none, male or female    gender = "none"    # 构造器    def __init__(self, name, age):        self.name = name        self.age = age    def my_name(self):        return self.namep = Person(name="tome", age=1)print(p.my_name())
2. 类型的继承
import jsonclass Person(object):    # gender none, male or female    gender = "none"    # 构造器    def __init__(self, name, age):        self.name = name        self.age = age    def my_name(self):        return self.namep = Person(name="tome", age=1)print(p.my_name())class Mail(Person):    def __init__(self, name, age):        super(Mail, self).__init__(name, age)        self.gender = "mail"    def my_name(self):        return self.name + "_mail"p = Mail(name="tome", age=1)print(json.dumps(p, default=lambda obj: obj.__dict__))print(p.my_name())
3. 类__new__函数

主要是__init__执行前会调用

#!/usr/bin/pythonimport jsonclass Person(object):    def __new__(cls, *args, **kwargs):        instance = object.__new__(cls)        instance.job = "it"        return instance    # construct    def __init__(self, name, age):        self.name = name        self.age = age    def to_json(self):        return json.dumps(self, default=lambda obj: obj.__dict__)p = Person(name="tome", age=1)print(p.to_json()) # {"age": 1, "job": "it", "name": "tome"}
其他用法技巧1. 断言
if type(1) is int:    print("args is int")    ...  # 等效 pass'''args is int'''
2. 测试<<<

可以参考文件: , 属于doctest

def humanize_bytes(n, precision=2):    # Author: Doug Latornell    # Licence: MIT    # URL:     """Return a humanized string representation of a number of bytes.    >>> humanize_bytes(1)    '1 B'    >>> humanize_bytes(1024, precision=1)    '1.0 kB'    >>> humanize_bytes(1024 * 123, precision=1)    '123.0 kB'    >>> humanize_bytes(1024 * 12342, precision=1)    '12.1 MB'    >>> humanize_bytes(1024 * 12342, precision=2)    '12.05 MB'    >>> humanize_bytes(1024 * 1234, precision=2)    '1.21 MB'    >>> humanize_bytes(1024 * 1234 * 1111, precision=2)    '1.31 GB'    >>> humanize_bytes(1024 * 1234 * 1111, precision=1)    '1.3 GB'    """    abbrevs = [        (1 << 50, 'PB'),        (1 << 40, 'TB'),        (1 << 30, 'GB'),        (1 << 20, 'MB'),        (1 << 10, 'kB'),        (1, 'B')    ]    if n == 1:        return '1 B'    for factor, suffix in abbrevs:        if n >= factor:            break    # noinspection PyUnboundLocalVariable    return f'{n / factor:.{precision}f} {suffix}'
3. yield参考:

其实类似于程序的断电,比如程序运行到那里其实是返回一个生成器,然后当你下一步是才会执行,比较节省内存

from typing import Listdef new(size: int = 1024 * 1024):    yield new_data(size)def new_data(size: int) -> List[int]:    return [0] * sizedata = new()print(type(data))print(len(next(data)))  # 只能执行一次 next不然报错'''<class 'generator'>1048576'''
脚本base64输出
echo "aGVsbG8gcHl0aG9uCg==" | python -c "import sys,base64; print(sys.stdin.read())"->echo "aGVsbG8gcHl0aG9uCg==" | python -c "import sys,base64; print(base64.b64decode(sys.stdin.read()))"-> stdout:b'hello python\n'
文件操作r ,w,x ,a四种类型(a: append, w=truncate+create, x=truncate+create if not exit)b,t 文件类型

第一列可以和第二列文件类型组合,第一列不允许并存

import oswith open("file.log", "w") as file:    for x in range(0, 100):        file.write("hello world"+os.linesep)with open("file.log","r") as file:    for line in file.readlines():        print(line)
json
import jsonprint(json.dumps({"k1": "v1", "k2": [1, 2, 3]}))print(json.loads('{"k1": "v1", "k2": [1, 2, 3]}'))

如果是class,需要继承JSONEncoder和JSONDecoder实现子类 ,或者

import json, datetimeclass Demo(object):    def __init__(self, name: str, age: int, birthday: datetime.date):        self.name = name        self.agw = age        self.birthday = birthday    def to_json(self, _):        return {"name": self.name, "age": self.agw, "birthday": self.birthday.strftime("%Y-%m-%d")}data = Demo("tom", 18, datetime.date(2001, 1, 1))print(json.dumps(data, default=data.to_json))
typing (申明类型)

官方文档:

可以参考这篇文章:

对于喜欢静态类型的语言,我觉得是非常nice的

from typing import Dict, Listdef test(data: Dict[str, str]) -> List[str]:    return [x for x in data]print(test({"k1": "v1", "k2": "v2"}))

标签: #python笔记考点 #pygmentscss