笨办法学python3pdf完整版,笨办法学python3电子书

本篇文章给大家谈谈learn python the hard way (笨办法学python),以及笨办法学python 3电子书下载,希望对各位有所帮助,不要忘了收藏本站喔。

环境

  • 平台: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.