- 推荐入门书籍:Python编程:从入门到实践
- 推荐IDE:Pycharm 社区版 (书中推荐使用sublime,个人觉得Pycharm更友好,一个最大的原因是Pycharm有intelisense,可以自动帮你补全关键字,这可以极大的提高编码效率)
本文内容主要对应书中第一章和第二章,主要是对一些重点的提炼以及对书中一些未提及细节的解释和补充。(是的,虽然我觉的这书写的还不错,中文版的翻译也不错,但是这书对一些细节点仍然处理的不够严谨。而在入门类技术书籍中,我认为细节的完整性至关重要,它可以提前提醒读者在特殊情况下容易犯哪些错误,以及如何处理。遗憾的是本书没能做到这点,因此这书我只能打4星)
Python版本检查
windows系统中打开cmd,输入python后回车查看版本,确保版本不低于3.6
Hello world
建一个.py结尾的文件,敲入以下代码:
message = "Hello world"
print(message)
运行(pycharm在windows中的快捷键Shift+F10)
变量名的命名规则
- 变量名只能包含字母、数字和下划线
- 变量名不能以数字开头
- 变量名不能包含空格
- 变量名不能使用关键字(python关键字,函数名等)
- 推荐使用蛇形命名(student_name)
字符串
用引号(单引号,双引号,三引号。。。)括起来的都是字符串。
与其他语言不同,python的字符串用单引号或双引号甚至多引号都可以:
student_name = "Bill Gates"
teacher_name = 'Ellon Mask'
text_message='He said : "Bill, come on!"'
这种灵活性能够让你方便的处理字符串中的引号。
但是,如果字符串中既包含单引号又包含双引号该怎么办呢?
比如这句话 Bill said "Elon, I'm going to kill you"
这种情况可以有两种处理方式:
1. 使用转义符\
text_message = " I'm gonna make it say \"hello world\""
2. 使用三引号(三单引号或三双引号)
text_message = '''I'm gonna make it say "hello world"'''
注意本例子中不能使用三双引号因为字符串的结尾是双引号。
如果字符串出现单引号头双引号尾或双引号头单引号尾的话就只能使用转义符\来处理了。
在字符串中使用变量
first_name="Elon"
last_name="Mask"
full_name=f"{first_name} {last_name}"
full_name_old_way="{} {}".format(first_name,last_name)
text_message=f"His name is {full_name}"
text_message_old_way="His name is {}".format(full_name_old_way)
print(text_message)
print(text_message_old_way)output:
His name is Elon Mask
His name is Elon Mask
数字
数中的下划线
这是一个与其他语言不太一样的地方,python中可以使用下划线将数字分组,使其更清晰易读。
number1 = 120
number2 = 2_3_0
number3 = 1.5_0
number4=10_000
print(number1 + number2)
print(number1 * number3)
print(number1*number4)
print(number3*number4)
print(number1/number4)
output
350
180.0
1200000
15000.0
0.012
注意,当需要在数值中使用下划线来分组时,下划线不能出现在数值的第一位和最后一位。当数值中有小数点时,小数点前和小数点后的2个数值部分也不能出现首部或尾部的下划线。
错误的下划线使用方式:
'''
invalid_number1=_120
invalid_number2=120_
invalid_number3=12_.3
invalid_number4=12._3
invalid_number5=12.3_
'''
注释
单行注释在行首加上#号(在pycharm中会提示#号后的注释内容与#号之间需要有一个空格,这只是一个规范,不遵守并不会出现语法错误)
多行注释可以在每行行首加#号,也可以用三引号作为注释的开头和结尾。(理论上三单引号和三双引号都可以,但是pycharm提示建议使用三双引号)
# This lien is a comment
# This lien is a comment too
"""
This is a comment which has
multiple linesand it was
considered as a document comment
"""
Python之禅
在代码中加入以下代码即可在输出中看到多条关于编程的建议,该建议被称作The Zen of Python,算是Python中的一个彩蛋吧。
import this
output:
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!