Variables

相比与把变量比喻成盒子,变量更应该比喻成标签,可以贴在数据上的标签

It’s much better to think of variables as labels that you can assign to values. You can also say that a variable references a certain value.

Strings

大小写转换

  • .title() 首字母大写
  • .upper() 大写
  • .lower() 小写

    f-string格式化

    1. first_name = 'ada'
    2. last_name = 'lovelace'
    3. full_name = f"{first_name} {last_name}"
    4. print(full_name)

    Tab and newline

    1. print("Languages: \n\tPython\n\tC\n\tJavaScript")

    去除空格

    ```python favorite_language = ‘ python ‘ print(favorite_language.lstrip()) print(favorite_language.rstrip()) print(favorite_language.strip())

赋值给变量才会真正改变它的值

favorite_language = favorite_language.strip() print(favorite_language)

  1. <a name="o5SSo"></a>
  2. ### Numbers
  3. add (+), subtract (-), multiply (*), and divide (/) <br />指数exponents()
  4. ```python
  5. 2 ** 3 # 2的3次方

浮点数失真(几乎存在每种编程语言中)

  1. >>> 0.2 + 0.1
  2. 0.30000000000000004

除法永远得到浮点数

  1. >>> 4 / 2
  2. 2.0

多重赋值

  1. >>> x,y,z = 0, 1, 2

常量用大写

  1. MAX_CONNECTIONS = 5000

comment

the hash mark (#) indicates a comment.

Zen of Python

  1. >>> import this
  2. The Zen of Python, by Tim Peters
  3. Beautiful is better than ugly.
  4. Explicit is better than implicit.
  5. Simple is better than complex.
  6. Complex is better than complicated.
  7. Flat is better than nested.
  8. Sparse is better than dense.
  9. Readability counts.
  10. Special cases aren't special enough to break the rules.
  11. Although practicality beats purity.
  12. Errors should never pass silently.
  13. Unless explicitly silenced.
  14. In the face of ambiguity, refuse the temptation to guess.
  15. There should be one-- and preferably only one --obvious way to do it.
  16. Although that way may not be obvious at first unless you're Dutch.
  17. Now is better than never.
  18. Although never is often better than *right* now.
  19. If the implementation is hard to explain, it's a bad idea.
  20. If the implementation is easy to explain, it may be a good idea.
  21. Namespaces are one honking great idea -- let's do more of those!

Don’t try to write perfect code; write code that works, and then decide whether to improve your code for that project or move on to something new.