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 < 1 data 2 > 1 data 3 > 1 """2. 异常x = -1 try: 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. 推导式(比较晦涩难懂)参考: https://www.cnblogs.com/desireyang/p/12160332.html
推导式好处: 效率更高,底层是c执行 1. 列表推导式
一共两种形式:(参考: https://zhuanlan.zhihu.com/p/139621170) , 它主要是输出是列表( 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"] """
最佳实践: 参考(https://github.com/httpie/httpie/blob/master/httpie/core.py#L235) 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 args print(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 os from collections.abc import Iterable with 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) pass with 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: , isinstance: True, line: line 1 type: , isinstance: True, line: line 2 type: , isinstance: True, line: line 3 """
这里面 _io.TextIOWrapper 实现了 __next__() 方法
比如我们自己实现一个可迭代的对象 下面可以看到我使用了类型申明 List[str] 其实这个python运行时并不会检测,需要工具进行检测!
变量默认都是 Any 类型 ,具体可以看 https://docs.python.org/zh-cn/3/library/typing.htmlfrom typing import List class 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 self data = Items(["1", "2", "3"]) for x in data: print(x) """ 1 2 3 """5. 包管理from ..a import foo # 上级目录 from .a import foo_a # 当前目录 import sys # 引用源码或者lib from copy import deepcopy # 引用源码或者lib from pygments.formatters.terminal import TerminalFormatter # 引用 lib.lib.file import demo.utils.a def 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 # int y = 1.1 # float z = 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) """ 1.0 0.0 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]) """ hello hello hello hell hello hello hlo """ # format print("My name is {} and age is {}".format("tom", 18)) """ My name is tom and age is 18 """ quantity = 3 itemno = 567 price = 49.95 myorder = "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. """ # func str = "hello world! " print(str.upper()) print(str.lower()) print(str.strip()) print(str + " ...") """ HELLO WORLD! hello world! hello world! hello world! ... """ # format myorder = "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 + num print(add(10)(10)) """ 20 """
lanbda 例子2 import json class Obj: def __init__(self): self.name = "tom" self.age = 1 print(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. listmylist = ["apple", "banana", "cherry"] # 切片 print(mylist[0]) print(mylist[2]) print(mylist[-1]) print(mylist[0:3:2]) """ apple cherry cherry ["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) """ apple banana cherry orange """ for index in range(len(mylist)): print("index: %d" % index) """ index: 0 index: 1 index: 2 index: 3 index: 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 str print(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. mapthisdict = {"brand": "Ford", "model": "Mustang", "year": 1964} for key, value in thisdict.items(): print("key: {}, value: {}".format(key, value)) """ key: brand, value: Ford key: model, value: Mustang key: year, value: 1964 """ for key in thisdict: print("key: {}, value: {}".format(key, thisdict[key])) """ key: brand, value: Ford key: model, value: Mustang key: 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 0 x is 1 x is 2 x is 3 x is 4 x is 5 """ for x in range(2, 6): print("x is %d" % x) """ x is 2 x is 3 x is 4 x is 5 """ for x in range(1, 6, 2): print("x is %d" % x) """ x is 1 x is 3 x is 5 """方法1. 定义一个空方法def func_1(): pass # 空方法必须申明pass func_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: tom age: 10 len(args): 0 len(kwargs): 0, type: len(kwargs): 2, type: kwargs key: args, value: 1 kwargs key: kwargs, value: 2 """ # 这里注意由于dict所以不能申明kv func_1("tom", 10, "1", "2", args="1", kwargs="2") """ name: tom age: 10 len(args): 2, type: args value: 1 args value: 2 len(kwargs): 2, type: kwargs key: args, value: 1 kwargs key: kwargs, value: 2 """3. 类型
申明输入输出类型 from typing import List, Union 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 ]类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.name p = Person(name="tome", age=1) print(p.my_name())2. 类型的继承import json 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.name p = 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/python import json class 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. 测试<<<
可以参考文件: https://segmentfault.com/q/1010000010389542 , 属于 doctest def humanize_bytes(n, precision=2): # Author: Doug Latornell # Licence: MIT # URL: https://code.activestate.com/recipes/577081/ """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参考: https://zhuanlan.zhihu.com/p/268605982
其实类似于程序的断电,比如程序运行到那里其实是返回一个生成器,然后当你下一步是才会执行,比较节省内存 from typing import List def new(size: int = 1024 * 1024): yield new_data(size) def new_data(size: int) -> List[int]: return [0] * size data = new() print(type(data)) print(len(next(data))) # 只能执行一次 next不然报错 """ 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 "文件操作r ,w ,x ,a 四种类型(a: append, w=truncate+create, x=truncate+create if not exit)b ,t 文件类型
第一列可以和第二列文件类型组合,第一列不允许并存 import os with 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)jsonimport json print(json.dumps({"k1": "v1", "k2": [1, 2, 3]})) print(json.loads("{"k1": "v1", "k2": [1, 2, 3]}"))
如果是class,需要继承 JSONEncoder和JSONDecoder实现子类 ,或者import json, datetime class 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 (申明类型)
官方文档: https://docs.python.org/zh-cn/3/library/typing.html
可以参考这篇文章: https://sikasjc.github.io/2018/07/14/type-hint-in-python/
对于喜欢静态类型的语言,我觉得是非常nice的 from typing import Dict, List def test(data: Dict[str, str]) -> List[str]: return [x for x in data] print(test({"k1": "v1", "k2": "v2"}))
7月31日爱威影音玫瑰公爵壹号演示活动预告爱威影音从2021年下半年起,隆重推出玫瑰公爵系列组合,经过精心搭配与多次聆听,最终选择了发烧音响领域的乌托邦,法国Focal劲浪搭配超过50年历史,源自剑桥的英国剑桥Cambri
爱威影音2021广州音响展精彩预告2021年广州音响展即将于5月2830日在东方宾馆和广州大酒店举行,爱威影音此次一共有三处展位,分别展示爱威影音旗下各款产品,欢迎广大爱好者发烧友,全国各地经销商和媒体朋友光临体验
HiFi音響封面故事Viennaacoustics全新贝多芬扬声器HiFi音响HiFiReview是香港最具公信力之音响及音乐评论刊物,于1986年4月创刊,至今逾35载,以月刊形式发行。自置两所不同规模之试音试音室,以便定时有系统地进行各类评测
CambridgeAudioEVO150新时代来临早前到CambridgeAudio香港总坛,参加EVO150EVO75发布会,看到它们的功能,为现时品牌多款现役产品的重新整合,尤其是来自旗舰前级EdgeNQ的数码功能,以及同轴旋
七月将现身SoulnoteX3频率产生器日本CSR旗下的HiEnd音响品牌Soulnote,即将推出他们第一部频率产生器X3。目前他们家的高阶的讯源产品,包括S3Ver。2SACDCD唱盘,以及D3D2两款数字模拟转换器
爱威影音正式推出玫瑰公爵组合壹号爱威影音从2021年下半年起,隆重推出玫瑰公爵系列组合,经过精心搭配与多次聆听,最终选择了发烧音响领域的乌托邦,法国Focal劲浪搭配超过50年历史,源自剑桥的英国剑桥Cambri
比特币真的成为巴西的法定货币了吗?比特币真的成为巴西的法定货币了吗?此前有报道称,在上月底,又有一个国家巴西,批准了比特币法案,将比特币作为该国的法定货币。但巴西中央银行行长罗伯托坎波斯内托(RobertoCamp
Facebook的五年元宇宙计划元宇宙概念爆火的时候,Facebook创始人扎克伯格说,元宇宙就是下一个互联网,并称Facebook将在未来五年内打造一个全新的元宇宙世界。Facebook的五年元宇宙计划为构建理
以太坊,一直在超越和创新数据显示,当前以太坊的市值已达到173,844,441,594。24美元(约1738亿美元),创历史新高,以太坊总市值目前为4232亿美元。说到以太坊,当下还有很多的人拿它跟比特币
盼盼食品品牌代言人隋文静韩聪在加拿大夺冠北京时间10月31日,20212022赛季世界花样滑冰大奖赛加拿大站比赛在温哥华落幕。盼盼食品品牌代言人隋文静韩聪,作为唯一参赛的中国双人滑组合,以总分224。05分力压来自美国和
新蛋中国严正申明近期,陆续有公司个人向本公司反映,有部分公司冒充新蛋中国跨国开店假借新蛋中国员工名义从事商业活动,开展线上线下宣讲活动,向卖家收取开店入驻费用等,对新蛋良好的社会声誉造成了负面影响