Python笔记

本文由Python之父Guido van Rossum撰写,鼓励年轻程序员勇于梦想,并提供了实用的Python编程技巧,包括集合操作、动态参数处理、闭包使用、类方法详解及变量可变性讨论。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Letter to a young programmer

Dear (insert name here),

I heard you enjoy a certain programming language named Python. Programming is a wonderful activity. I am a little jealous that you have access to computers at your age; when I grew up I didn't even know what a computer was! I was an electronics hobbyist though, and my big dream was to build my own electronic calculator from discrete components. I never did do that, but I did build several digital clocks, and it was amazing to build something that complex and see it work. I hope you dream big too -- programmerscanmake computers (and robots!) do amazing things, and this is a great time to become a programmer. Just imagine how much faster computers will be in five or ten years, and what you will be able to do with your skills then!


--Guido van Rossum (inventor of Python)      Thursday, October 24, 2013


What's the Best Way for a Programmer to Learn a New Language?

set:

https://docs.python.org/2/library/sets.html

>>>a = set(['br-tun', 'br-ex'])

>>>b = set(['br-int','br-ex'])

>>> a.difference_update(b)

>>>a

set(['br-tun'])



1、

def Dyanmic(**dic):

    for i in dic.keys():
        locals()[i] = dic[i]
    print locals()
    print dic.keys()

Dyanmic(a=2,d=0,c=1)

------------------------------->

{'i': 'd', 'a': 2, 'c': 1, 'd': 0, 'dic': {'a': 2, 'c': 1, 'd': 0}}
['a', 'c', 'd']


2、from collections import namedtuple



3、 Closure

 def func1(a):

       def func2(b):

             return a+b

       return func2

-----> 

p = func1(11)

q = func1(22)

print p(100), q(100)   ---->  111,122


4、 Sort fun



5、 一个简单的例子说明classmethod,classmethod跟运行时的那个cls相关,而staticmethod可以理解为普通模块的函数,只不过被放到了一个class内而已,为这一类class独有。

class A(object):

    @classmethod

    def func1(cls):

        print "A.func1", cls.__name__

    @classmethod

    def func2(cls):

        cls.func1()

        print "func2"cls.__name__

class B(A):

    @classmethod

    def func1(cls):

        print "B.func1", cls.__name__


A.func2()

B.func2()

输出:

A.func1 A

func2 A

B.func1 B

func2 B


6、 mutable & immutable

http://blog.youkuaiyun.com/hsuxu/article/details/7785835

在python里面哪些是immutable的呢?

numbers, strings, tuples, frozensets

(set & frozenset: blog.youkuaiyun.com/fall221/article/details/8478574 )

其实,还有一种特殊情况,就是自定义的类型

一般情况下,程序员自定的python类型都是mutable的,但是如果想定制immutable的数据类型,那么必须要重写object的__setattr__, __delattr__的方法,

class Immutable(object):

    def __setattr__(self, *args):

        raise TypeError("can't modify the value of immutable instance")

    __delattr__ = __setattr__

    def __init__(self, value):

        super(Immutable, self).__setattr__("value", value)


### Python 笔记 Markdown 格式 学习资料 #### 使用 Markdown 编辑 Python 笔记的优势 Markdown 的语法非常简洁,适合编写技术文档和技术笔记。对于非技术人员来说也容易上手[^4]。通过简单的标记符号可以快速创建结构化的笔记内容。 #### 创建 Python 笔记的具体方法 为了更好地管理和展示 Python 笔记,建议按照如下方式组织: 1. **标题和子标题** 利用 `#` 来定义不同级别的标题,这有助于构建清晰的文章框架。 2. **代码片段高亮显示** 当记录 Python 代码时,可以通过三对反引号包裹代码,并指定编程语言来启用语法高亮[^1]: ```python def hello_world(): print("Hello, world!") ``` 3. **列表项** 可以使用星号(*) 或者减号(-) 来创建无序列表;使用数字加句点(1., 2.) 形式的编号来创建有序列表。 4. **超链接与图片嵌入** 插入外部资源如教程页面或图表图像,增强笔记的信息量。格式为 `[描述](URL)` 和 `![替代文字](图片地址)`。 5. **表格制作** 表格可以帮助整理数据对比等内容,利用管道符(|)分隔列,短横线(-)划分行。 6. **特殊字符处理** 对于一些特殊的 HTML 实体字符可以直接输入,比如版权符号©、注册商标®等,甚至表情符号也可以直接粘贴进来[^5]。 7. **保存与分享** 完成后的 Markdown 文件(.md),既可以在本地查看,也能上传至 GitHub Pages 等平台发布为静态网站[^2]。 8. **转换为其他格式** 如果希望进一步加工这些笔记,则可借助第三方库如 Python-Markdown 将其转码为更通用的 HTML 文档[^3]。 ```html <script> $.get((searchMap.md || 'readme.md'), function(text) { document.write(markdownit().render(text)); }); </script> ``` 上述脚本展示了如何动态加载并渲染 Markdown 文件中的内容,在浏览器端即时预览效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值