Python学习笔记1

Python学习笔记1

C++,难学更难用,设计如此。

Python 这个名字是从 Monty Python来的,而不是蟒蛇这种动物。 我们可以看到有一些Python书籍把蟒蛇作为封面,原因并非它的名字,而是因为这门语言有一些特性和蟒蛇比较相似.(Monty Python 是一个英国喜剧团体)。国外的一些大学将Python作为新手的入门教学语言。

什么是Python?谁在用它?能用它做什么?

  • Python 是一种 解释性的,面向对象的,带有 动态语义的高级程序设计语言。可以花较少的代价实现想要的功能,并且编写的程序清晰易懂,缺点是运行速度慢,得到一些就会失去另外一些。
    • 它是很多Linux发行版的重要组成部分
    • Google用它实现网络爬虫和搜索引擎中的很多组件
    • 应用于计算机游戏和生物信息
    • 数据挖掘和数据分析
    • 爬虫,爬取网站上的图片等
    • 新闻聚合
    • 聊天室
    • 电子公告板
    • DIY街机游戏

Linux下安装

绝大多数Linux和Unix只要安装完毕,Python解释器已经默认存在。

1
2
3
4
5
jiang@jiang-ThinkPad-E450:~$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

Ctrl - D 退出交互式解释器。

如果没有安装,会看到如下错误信息

1
bash: python : command not found

Debian 系安装

1
sudo apt-get install python

Hello,World!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
>>> print "Hello,World!"
Hello,World!
>>>

//其他计算机语言,可能会每行以 ; 结束,Python不用,一行就是一行(对我来说,你就是;)

//获取帮助
>>> help()

Welcome to Python 2.7! This is the online help utility.

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/2.7/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules. To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, or topics, type "modules",
"keywords", or "topics". Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as "spam", type "modules spam".

help>

基本概念

输入输出函数

1
2
3
4
5
6
7
>>> print("你好!")
你好!
>>> name=input("Enter your name:")
Enter your name:"1-riverfish"
>>> name
'1-riverfish'
>>>

if 语句

1
2
3
4
>>> if name == '1-riverfish' : print("欢迎!")
... else : print("EXIT")
...
欢迎!

函数(内建函数和自定义函数)

1
2
3
pow()  乘方
abs() 绝对值
round()四舍五入

模块,增强Python功能的拓展

1
2
3
4
5
6
7
8
9
10
11
12
13
>>> import math
>>> math.floor(32.1)
32.0
>>> math.ceil(32.1)
33.0
#import导入模块,按照 模块.函数 格式使用这个模块的函数

#from 模块 import 函数,可以直接调用函数而不用写模块名前缀
>>> from math import sqrt
>>> sqrt(9)
3.0
#可以用变量名引用函数
foo = math.sqrt

nan Not a Number

1
2
3
4
5
>>> import cmath 
>>> sqrt(-1)
1j
#cmath 即 complex math,除非真的需要 from 这个形式,否则应该坚持使用 普通的 import
#1j代表i,Python语言本身就提供了对复数的支持

时光机 __future__

传言说Guido van Rossum(Python之父)拥有一架时光机,因为在人们要求增加语言新特性的时候,这个特性就已经实现了。当然,我等凡夫俗子是不允许进入这架时光机的。但是善良的Guido将时光机的一部分以__future__这个充满魔力的模块的形式融入了Python.通过它可以导入那些在未来会成为标准Python组成部分的新特性。

保存并执行程序 IDLE 新建文件

通过命令提示符运行python脚本

1
$ python hello.py

注释符号,程序员的第一戒律就是“Thou Shalt Comment 汝应注释” 而不是 如果难写,就该难读

注释符号 #

字符串

  • str repr和反引号是将Python值转换为字符串值的三种方法
  • input 和 raw_input 的比较
    • input 会假设用户输入的是合法的Python表达式,如果以字符串作为输入,程序运行没有问题
    • raw_input 会把所有的输入当作原始数据(raw input),然后将其放入字符串中
1
2
3
4
5
6
>>> input("What's your name? ")
What's your name? '1-riverfish'
'1-riverfish'
>>> raw_input("What's your name? ")
What's your name? 1-riverfish
'1-riverfish'
  • 长字符串 原始字符串 Unicode
    • 长字符串 需要写一个非常非常长的字符串,它需要跨越多行,使用三个`代替普通`
    • 原始字符串 原始字符串对于反斜线并不会特殊对待,但保留转义功能 r’string’
    • Unicode对象 Python中的普通字符串在内部是以8位的ASCII码形式存储的,而Unicode字符串则存储为16位Unicode字符,这样能够表示更多的字符集 u’string’

Any question please contact 1-riverfish