Python 的特点和用途¶

  • Python 是一门“解释型语言”,相对于“编译型语言”更易调试。
  • Python 的语法风格简明,即使对外行也易读,大大降低了程序设计的门槛
  • Python 可以直接调用 Fortran, C/C++, R 等语言库,因此也叫“胶水”语 言,即把不同的程序粘合在一起。
  • Python 是一个通用语言,不仅在科学研究,在生活中的方方面面都会有用。
    • 操作系统生成器和管理器 (Gentoo Portage)
    • 网站 (Django)

学习资料¶

  • Allen Downey, Think Python 2e

    简明通俗的入门书

  • http://py4e.com/

    Python for everybody,全球知名的 Python 在线教程,新手友好。

  • Learn X in Y minutes

    https://learnxinyminutes.com/docs/python3/

    已经掌握若干门语言的同学,可以通过此提纲快速入门

Python 环境¶

安装了 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 增强的互动环境¶

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]:
In [1]:
import sys
sys.version
Out[1]:
'3.8.9 (default, May  6 2021, 11:23:41) \n[GCC 10.2.0]'

代数基本运算¶

  • + 加,- 减,* 乘,/ 除
  • // 整除,% 取余,** 乘方
  • 其它运算由函数来定义
In [80]:
2+1
Out[80]:
3
In [77]:
2*7, 2**7, 3/2, 3//2, 3%2
Out[77]:
(14, 128, 1.5, 1, 1)
In [21]:
import math
math.factorial(10) #10!
Out[21]:
3628800

整数是高精度的¶

这个操作并没有硬件的底层支持,是 Python 的软件实现,以一些性能损失为代价给予用户便利。

In [52]:
math.factorial(666)
Out[52]:
1010632056840781493390822708129876451757582398324145411340420807357413802103697022989202806801491012040989802203557527039339704057130729302834542423840165856428740661530297972410682828699397176884342513509493787480774903493389255262878341761883261899426484944657161693131380311117619573051526423320389641805410816067607893067483259816815364609828668662748110385603657973284604842078094141556427708745345100598829488472505949071967727270911965060885209294340665506480226426083357901503097781140832497013738079112777615719116203317542199999489227144752667085796752482688850461263732284539176142365823973696764537603278769322286708855475069835681643710846140569769330065775414413083501043659572299454446517242824002140555140464296291001901438414675730552964914569269734038500764140551143642836128613304734147348086095123859660926788460671181469216252213374650499557831741950594827147225699896414088694251261045196672567495532228826719381606116974003112642111561332573503212960729711781993903877416394381718464765527575014252129040283236963922624344456975024058167368431809068544577258472983979437818072648213608650098749369761056961203791265363665664696802245199962040041544438210327210476982203348458596093079296569561267409473914124132102055811493736199668788534872321705360511305248710796441479213354542583576076596250213454667968837996023273163069094700429467106663925419581193136339860545658673623955231932399404809404108767232000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In [53]:
2**1000
Out[53]:
10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376

整除的基本约定¶

负数整除:向小的方向截断。

In [46]:
5 // 3, -5 // 3, 5.0 // 3.0, -5.0 // 3.0
Out[46]:
(1, -2, 1.0, -2.0)
In [50]:
5 % 3, -5 % 3, 5.0 % 3.0, -5.0 % 3.0
Out[50]:
(2, 1, 2.0, 1.0)
In [51]:
(-5 // 3) * 3 + (-5 % 3) == -5
Out[51]:
True

布尔运算:真与假¶

Python 的设计目标是符合直觉的

In [57]:
not True, not False
Out[57]:
(False, True)
In [59]:
True and False, False or True
Out[59]:
(False, True)
In [65]:
# True 实际上是 1, False 实际上是 0
True + True, True * False
Out[65]:
(2, 0)
In [66]:
True * 8, False - 5
Out[66]:
(8, -5)

条件判断¶

通过例子了解一些关系运算

In [72]:
1 == 1, 2 == 1
Out[72]:
(True, False)
In [73]:
1 != 1, 2 != 1
Out[73]:
(False, True)
In [75]:
1 < 10, 1 > 10, 2 <= 2, 2 >= 2
Out[75]:
(True, False, True, True)

数据类型¶

  • int 指整型,没有上限
  • float 是浮点型,一般为双精度
  • str 是字符串
In [23]:
type(1)
Out[23]:
int
In [24]:
type(1.5)
Out[24]:
float
In [25]:
type('Hello')
Out[25]:
str
In [26]:
# 单个字符也是字符串
type("a")
Out[26]:
str
In [76]:
# 汉字也可以作为字符串
type("你好")
Out[76]:
str

字符串¶

与高精度整数一样,字符串也没有硬件的对应,是 Python 的软件实现。这极大方便了使用 Python 进行文本处理。

In [9]:
"今天" + "下雨了"
Out[9]:
'今天下雨了'
In [10]:
"1" + "2"
Out[10]:
'12'

入门 Python¶

输出"Hello World!"

In [29]:
print("Hello World!")
Hello World!

变量¶

In [2]:
message = "This is an new era. 新时代"
print(message)
This is an new era. 新时代

Python 字符串与变量的联合操作¶

字符串的操作相对于硬件调用,是一项高级功能。Python 有强大的字符串处理工具。

In [9]:
len('123456'), len('654321') # 字符串长度
Out[9]:
(6, 6)
In [1]:
"{} 乘以 {} 等于 {}".format(3, 5, 3*5) # 把变量嵌入字符串
Out[1]:
'3 乘以 5 等于 15'
In [2]:
b=50
f"b 的取值是 {b}"
Out[2]:
'b 的取值是 50'
In [5]:
s = "我正在上课。"
s[0], s[2:4], s[-1] # 字符串可以取子串
Out[5]:
('我', '在上', '。')
In [6]:
"啊" * 10
Out[6]:
'啊啊啊啊啊啊啊啊啊啊'

None 值¶

None 是一个特殊的值,代表空、无、无法表达或者非法的结果

In [20]:
print(None)
None
In [22]:
x = None # None 可以被赋值
1 is None, x is None # 可以被判断
Out[22]:
(False, True)
In [25]:
bool(None) # None 也可以当作“假”被判断
Out[25]:
False

None 比较抽象,更多的例子我们遇到再讲

阅读英语提示¶

  • 人生第一次遇到英语不是“屠龙之技”的场景,值得庆贺。
  • 下一个场景:阅读英语科技文献
  • 再下一个场景:与国际同行讨论争吵
In [3]:
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
In [4]:
IMPORT SYS
  File "<ipython-input-4-0caf82149b0b>", line 1
    IMPORT SYS
             ^
SyntaxError: invalid character in identifier

标准输入输出¶

  • 标准输出默认与屏幕连接,print() 默认向标准输出写
  • 标准输入默认与键盘连接,input() 默认从标准输入读
In [8]:
q = input() # 下面由现场输入
frog
In [9]:
print(q)
frog

Python 脚本¶

  • 世界上本没有脚本,把输入的命令记录下来就成了脚本
  • Python 脚本一般以 .py 结尾
  • 例子
$ cat hello.py 
#!/usr/bin/python3

print("Hello, thank you.  Thank you very much!")