Python Crash Course读书笔记 - 第2章:Variables and Simple Data Types

变量

文件hello_world.py中, .py是python文件的后缀,因此会用Python interpreter解析。

$ cat hello_world.py
print("Hello Python world!")

现在改为变量形式,message即变量,每个变量都与一个值关联:

message = "Hello Python world!"
print(message)

变量名首先要有意义,其次要短。并且不能用关键字如print。另外,和车牌一样,避免用l和O,以免和1和0混淆。例如student_name强于s_nname_length强于length_of_persons_name。最后都用小写,因为大写在Python中有特殊的含义。

The best way to understand new programming concepts is to try using them in your programs.

是这么个理,learning by doing
变量不是装东西的盒子,而是标签。就是reference,pointer。

String类型

string是一系列字符,引号中的数据就是string,引号可以是单引号或双引号。这样有时就不需要转义(escape)了。如果需要,转义符仍为\
以下使用了方法(method),方法通过.调用,其后总是跟()的。方法可以嵌套。

$ cat name.py
name = "long long ago"
print(name.title())
print(name.upper())
print(name.lower())
print(name.lower().upper())
$ python name.py
Long Long Ago
LONG LONG AGO
long long ago
LONG LONG AGO

string中可以带变量,称为f-string,包含在{}之间,类似于C语言printf中的%s
f-string在python 3.6后开始支持。因此需要用python3运行。

$ cat fullname.py
first_name = "ming"
last_name = "xiao"
full_name = f"Hello {first_name} {last_name}!"
print(full_name)
print(f"Hello, {full_name.title()}!")
$ python3 fullname.py
Hello ming xiao!
Hello, Hello Ming Xiao!!
$ vi fullname.py

string中可以加入不可打印字符,如\n\t

>>> print "Just\tDo\tit!\n"
Just    Do      it!

空格的处理:

>>> language=' python '
>>> lang = ' python '
>>> lang.rstrip()
' python'
>>> lang.lstrip()
'python '
>>> lang.strip()
'python'

Number类型

分为integer和float。带小数点的都是float。Number类型支持加减乘除操作(+-*/)。
integer和float的混合操作结果为float,两个integer相除结果为float。
为增加可读性,数字中可带下划线(_),类似于Excel中的逗号(,),此特性python 3.6开始支持:

$ python3
Python 3.6.8 (default, Aug  7 2019, 08:02:28)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39.0.1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a=1_234_567
>>> b=1_234.567
>>> a
1234567
>>> b
1234.567

多变量同时赋值:

>>> x, y, z = 0, 0.1, "ok"

常数,变量名建议全部大写:


>>> TIMEOUT=500

注释

hash mark (#)之后的内容都是注释。
写注释不仅是为自己看懂,更重要的是让别人看懂:

If you want to become a professional programmer or collaborate with other programmers, you should write meaningful comments.

注释要清晰,准确:

Writing clear, concise comments in your code is one of the most beneficial habits you can form as a new programmer.

先写注释再删胜过之前没写后面来补。

Python之禅()

The Zen of Python是编写Python代码的一系列指导原则。
输入import this查看这些原则,我把部分解读放到了原则上方的注释中:

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
# 明示胜过猜
Explicit is better than implicit.
# 简单胜过复杂
Simple is better than complex.
# 如果简单不可得,从复杂中选最简单的
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
# 可读性很重要
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
# 尽量复用代码,用函数
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
# 不要追求完美,先让其可行,再提升
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

这里特别提一下Now is better than never,好像是Martin Fowler说的:

first make it run, then make it faster

也可以说“及时的80分胜过迟到的100”,都是这个意思。

Python Crash Course读书笔记 - 第2章:Variables and Simple Data TypesPython Crash Course读书笔记 - 第2章:Variables and Simple Data Types dingdingfish 博客专家 发布了352 篇原创文章 · 获赞 42 · 访问量 55万+ 他的留言板 关注
上一篇:Python教程-1 Python介绍


下一篇:Dart for循环以及循环遍历List