【Python3 笔记】《笨办法学Python》

环境

  • 平台:Windows 10
  • Python 版本:3.8
  • 电子书资源受版权原因无法上传 /sad

总结

  • 字符串表示:' '" " 可以相互嵌套使用;

    // 示例
    print("I'd much rather you 'not'.")
    print('I "said" do not touch this.')
    

    运行结果

  • 字符串嵌入变量:

    法一:在引号(单双引号都行)前添加字母 f 表示 format ,变量用 {} 嵌入;
    法二:引号中使用 {argc1} {argc2} 在引号后添加 .format({argc1=value1}, {argc1=value2});

    // 示例
     my_height = 168  # cm
     my_weight = 57  # kg
     total = my_age + my_height + my_weight
     print(f'If I add {my_age}, {my_height}, {my_weight} I get {total}.')
     print("If I add {age}, {height}, {weight} I get {tot}."
     		.format(age=my_age, weight=my_weight, height=my_height, tot=total))
    

    运行结果

  • 函数使用*args接收不确定数量的参数:

    // 示例
    def print_multi(*args):
        for i, arg in enumerate(args):
            print(f"arg_{i}: {arg}")
    
    print_multi("Shaw", "Feed", "jjj")
    print_multi("Morton", "Wang")
    

    在这里插入图片描述

  • 命名规则:

    Class —— “camel case” : SuperGoldFactory

    Functions —— “underscore format” : my_awesome_hair

  • Code a little, run a little, fix a little.

  • Only idiots are slaves to rules all the time.

原文摘录

When to Use Inheritance or Composition

The question of “inheritance versus composition” comes down to an attempt to solve the problem of reusable code. You don’t want to have duplicated code all over your software, since that’s not clean and efficient. Inheritance solves this problem by creating a mechanism for you to have implied features in base classes. Composition solves this by giving you modules and the capability to call functions in other classes. If both solutions solve the problem of reuse, then which one is appropriate in which situations? The answer is incredibly subjective, but I’ll give you my three guidelines for when to do which:

  1. Avoid multiple inheritance at all costs, as it’s too complex to be reliable. If you’re stuck with it, then be prepared to know the class hierarchy and spend time finding where everything is coming from.

  2. Use composition to package code into modules that are used in many different unrelated places and situations.

  3. Use inheritance only when there are clearly related reusable pieces of code that fit under a single common concept or if you have to because of something you’re using.

Do not be a slave to these rules. The thing to remember about object-oriented programming is that it is entirely a social convention programmers have created to package and share code. Because it’s a social convention, but one that’s codified in Python, you may be forced to avoid these rules because of the people you work with. In that case, find out how they use things and then just adapt to the situation.

How to Learn Any Programming Language

  1. Get a book or some introductory text about the language.
  2. Go through the book and type in all of the code making all of it run.
  3. Read the book as you work on the code, taking notes.
  4. Use the language to implement a small set of programs you are familiar with in another language.
  5. Read other people’s code in the language, and try to copy their patterns.

The final thing to remember about learning a new language is this: Don’t be a stupid tourist. A stupid tourist is someone who goes to another country and then complains that the food isn’t like the food at home. “Why can’t I get a good burger in this stupid country!?” When you’re learning a new language, assume that what it does isn’t stupid, it’s just different, and embrace it so you can learn it.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
办法 Python (第三版) 欢迎阅读《办法 Python》第三版。本书中译本发布于 https://learn-python-the-hard-way-zh_cn-translation.readthedocs.org 英文原版地址为 http://learnpythonthehardway.org/book/ Contents: 前言:办法更简单 习题0:准备工作 习题1:第一个程序 习题2:注释和井号 习题3:数字和数计算 习题4:变量(variable)和命名 习题5:更多的变量和打印 习题6:字符串(string)和文本 习题7:更多打印 习题8:打印,打印 习题9:打印,打印,打印 习题10:那是什么? 习题11:提问 习题12:提示别人 习题13:参数、解包、变量 习题14:提示和传递 习题15:读取文件 习题16:读写文件 习题17:更多文件操作 习题18:命名、变量、代码、函数 习题19:函数和变量 习题20:函数和文件 习题21:函数可以返回东西 习题22:到现在你到了哪些东西? 习题23:读代码 习题24:更多练习 习题25:更多更多的练习 习题26:恭喜你,现在可以考试了! 习题27:记住逻辑关系 习题28:布尔表达式练习 习题29:如果(if) 习题30: Else和If 习题31:作出决定 习题32:循环和列表 习题33: While循环 习题34:访问列表的元素 习题35:分支和函数 习题36:设计和调试 习题37:复习各种符号 习题38:列表的操作 习题39:字典, 可爱的字典 习题40:模块、类、对象 习题41:物以类聚 习题42:对象、类、以及从属关系 习题43:来自Percal 25号行星的哥顿人(Gothons) 习题44:继承 (Inheritance) VS合成(Composition) 习题45:你来制作一个游戏 习题46:一个项目骨架 习题47:自动化测试 习题48:更复杂的用户输入 习题49:创建句子 习题50:你的第一个网站 习题51:从浏览器中获取输入 习题52:创建你的web游戏 下一步 老程序员的建议

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值