Allen Downey, Think Python 2e
简明通俗的入门书
Python for everybody,全球知名的 Python 在线教程,新手友好。
Learn X in Y minutes
https://learnxinyminutes.com/docs/python3/
已经掌握若干门语言的同学,可以通过此提纲快速入门
安装了 Python 之后,在命令行界面可以直接进入 Python 的交互模式:
$ python3
Python 3.8.9 (default, May 6 2021, 11:23:41)
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
IPython 是一个提供了额外交互功能的环境:
# apt install ipython3
$ ipython3
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.20.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]:
import sys
sys.version
'3.8.9 (default, May 6 2021, 11:23:41) \n[GCC 10.2.0]'
2+1
3
2*7, 2**7, 3/2, 3//2, 3%2
(14, 128, 1.5, 1, 1)
import math
math.factorial(10) #10!
3628800
这个操作并没有硬件的底层支持,是 Python 的软件实现,以一些性能损失为代价给予用户便利。
math.factorial(666)
1010632056840781493390822708129876451757582398324145411340420807357413802103697022989202806801491012040989802203557527039339704057130729302834542423840165856428740661530297972410682828699397176884342513509493787480774903493389255262878341761883261899426484944657161693131380311117619573051526423320389641805410816067607893067483259816815364609828668662748110385603657973284604842078094141556427708745345100598829488472505949071967727270911965060885209294340665506480226426083357901503097781140832497013738079112777615719116203317542199999489227144752667085796752482688850461263732284539176142365823973696764537603278769322286708855475069835681643710846140569769330065775414413083501043659572299454446517242824002140555140464296291001901438414675730552964914569269734038500764140551143642836128613304734147348086095123859660926788460671181469216252213374650499557831741950594827147225699896414088694251261045196672567495532228826719381606116974003112642111561332573503212960729711781993903877416394381718464765527575014252129040283236963922624344456975024058167368431809068544577258472983979437818072648213608650098749369761056961203791265363665664696802245199962040041544438210327210476982203348458596093079296569561267409473914124132102055811493736199668788534872321705360511305248710796441479213354542583576076596250213454667968837996023273163069094700429467106663925419581193136339860545658673623955231932399404809404108767232000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
2**1000
10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
负数整除:向小的方向截断。
5 // 3, -5 // 3, 5.0 // 3.0, -5.0 // 3.0
(1, -2, 1.0, -2.0)
5 % 3, -5 % 3, 5.0 % 3.0, -5.0 % 3.0
(2, 1, 2.0, 1.0)
(-5 // 3) * 3 + (-5 % 3) == -5
True
Python 的设计目标是符合直觉的
not True, not False
(False, True)
True and False, False or True
(False, True)
# True 实际上是 1, False 实际上是 0
True + True, True * False
(2, 0)
True * 8, False - 5
(8, -5)
通过例子了解一些关系运算
1 == 1, 2 == 1
(True, False)
1 != 1, 2 != 1
(False, True)
1 < 10, 1 > 10, 2 <= 2, 2 >= 2
(True, False, True, True)
type(1)
int
type(1.5)
float
type('Hello')
str
# 单个字符也是字符串
type("a")
str
# 汉字也可以作为字符串
type("你好")
str
与高精度整数一样,字符串也没有硬件的对应,是 Python 的软件实现。这极大方便了使用 Python 进行文本处理。
"今天" + "下雨了"
'今天下雨了'
"1" + "2"
'12'
输出"Hello World!"
print("Hello World!")
Hello World!
message = "This is an new era. 新时代"
print(message)
This is an new era. 新时代
字符串的操作相对于硬件调用,是一项高级功能。Python 有强大的字符串处理工具。
len('123456'), len('654321') # 字符串长度
(6, 6)
"{} 乘以 {} 等于 {}".format(3, 5, 3*5) # 把变量嵌入字符串
'3 乘以 5 等于 15'
b=50
f"b 的取值是 {b}"
'b 的取值是 50'
s = "我正在上课。"
s[0], s[2:4], s[-1] # 字符串可以取子串
('我', '在上', '。')
"啊" * 10
'啊啊啊啊啊啊啊啊啊啊'
None 是一个特殊的值,代表空、无、无法表达或者非法的结果
print(None)
None
x = None # None 可以被赋值
1 is None, x is None # 可以被判断
(False, True)
bool(None) # None 也可以当作“假”被判断
False
None 比较抽象,更多的例子我们遇到再讲
message + 1
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-3-f40f0b3453ee> in <module> ----> 1 message + 1 TypeError: can only concatenate str (not "int") to str
IMPORT SYS
File "<ipython-input-4-0caf82149b0b>", line 1 IMPORT SYS ^ SyntaxError: invalid character in identifier
print()
默认向标准输出写input()
默认从标准输入读q = input() # 下面由现场输入
frog
print(q)
frog
.py
结尾$ cat hello.py
#!/usr/bin/python3
print("Hello, thank you. Thank you very much!")