您的位置:首页 > 编程语言 > Python开发

《Python基础教程(第2版 修订版)》 第1章 快速改造:基础知识(学习笔记)

2015-09-29 23:12 555 查看
1.1安装Python

前文已经介绍,这里不赘述,前文已经介绍了在windows7的配置

配置完成后输入命令 “python” ,得到以下信息

Python 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>


1.2 交互式解释器
当输入“python”时会出现以上信息,交互式解释器相信大家都不陌生了,如果使用过Linux,那么交互式的思想你已经相当具备了,交互式按我的理解,就是人和机器的一问一答模式,你输入一个指令,机器告诉你相应的信息。

来吧,打印python下的 "Hello,World!"

Python 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>print "Hello,World!"
Hello,World!
>>>
你没有看错,就是一行,print "Hello,World!",没有头文件,没有标准输入输出,没有main函数,python就是这么任性

1.3 算法是什么

关于算法的概念就不解释了吧,估计你想吐了吧

1.4 数字和表达式

不废话,直接运行实例

Python 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>2 + 2
4
>>> 53672 + 235253
288925
>>> 1/2
0
>>> 1.0/2.0
0.5
>>>
前面两个加法不解释,在3.0版本之前1/2是整数除法,即1/2==0

如果想要得到0.5这个结果,可以利用浮点数的特性

如下

Python 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>2 + 2
4
>>> 53672 + 235253
288925
>>> 1/2
0
>>> 1.0/2.0
0.5
>>> 1.0/2
0.5
>>> 1/2.
0.5
>>>


"//"双斜线提供实现整除的操作符,对浮点数也一样

>>> 1//2
0
>>> 1.0//2.0
0.0
>>>


“%”取余运算
>>> 10/3
3
>>> 10%3
1
>>> 9/3
3
>>> 9%3
0
>>> 2.75%0.5
0.25
>>>


幂(乘方运算符)

>>> 2**3
8
>>> -3**2
-9
>>> (-3)**2
9
>>>


1.4.1 长整数
python可以处理非常大的整数,而大家知道,在C/C++进行大数运算,可能需要借助字符串进行转换,自己编写函数的话,代码量还是有的,而python可以很快捷地处理大数运算。

>>> 123456789101112131415161718192021*12345678901011121314151617181920
1524157876392179798183044712001221195380805499108933421449460320L
>>>
被吓到了吧,谁说不是呢,而可以想象C++大整数乘法的代码量,是不是觉得python方便多了

1.5 变量

没啥好说的,看段小代码

>>> x=3
>>> x * 2
6
>>>
注意:变量名可以包括字母、数字和下划线(_)。变量不能以数字开头,所以Plan9是合法变量名,而9Plan不是

1.6语句

也不废话,运行实例

>>> x=3
>>> x * 2
6
>>>x=3
>>> print x
3
>>> 2 * 2
4
>>> print 2*2
4
>>>
注意:在python3.0中,print是函数,打印42,需要写成print(42),而不是print 42

1.7获取用户输入

运行实例,注意“input”的用法

>>> input("The meaning of life:")
The meaning of life:42
42
>>> x = input("x: ")
x: 34
>>> y = input("y: ")
y: 42
>>> print x * y
1428
>>>


1.8函数
幂运算函数pow

>>> 2**3
8
>>> pow(2,3)
8
>>> 10 + pow(2,3.5)/3.0
13.771236166328254
>>> 10 + pow(2,3*5)/3.0
10932.666666666666
>>>


绝对值函数 abs

>>> abs(-10)
10


近似值函数 round

>>> 1/2
0
>>> round(1/2)
0.0
>>> round(1.0/2.0)
1.0
>>>


1.9模块
可以把模块想象成导入到Python以增强其功能的扩展。按照“模块.函数”格式使用这个模块的函数。

实例如下

>>> import math
>>> math.floor(32.9)
32.0
math.floor将32.9转化为整数32

import命令的另外一种形式

>>> import math
>>> math.sqrt(9)
3.0
>>> from math import sqrt
>>> sqrt(9)
3.0
前提是不会导入多个同名函数的情况下,不希望每次都写模块名,可以这样使用,建议使用第一种形式

1.10 保存并执行程序

使用编辑器如EditPlus,编写一段python代码,以.py后缀名结尾

如在E盘根目录下有一个 Hello.py

内容如下

print "Hello,World!"
在cmd中到达E盘根目录下,运行python HelloWorld.py,结果如下
python HelloWorld.py

Hello,World!


下一个实例,对脚本进行扩展

HelloWorld.py内容如下

print "Hello,World!"
name = raw_input("What is your name? ")
print 'Hello. ' + name + '!'


运行效果如下

E:\>python HelloWorld.py
Hello,World!
What is your name? Nick
Hello. Nick!

1.10.3注释

井号(#)在Pyhon中有注释的作用

>>> #打印圆的周长
>>> print 2 * 3.1415926 * 2
12.5663704
>>> #获得用户名
>>> user_name = raw_input("What is your name?")
What is your name?Tom

1.11字符串

>>> "Hello,World!"
'Hello,World!'
>>> print "Hello,World!"
Hello,World!


1.11.1 单引号字符串和转义引号
不解释,看运行实例来体会区别

>>> "Hello,World!"
'Hello,World!'
>>> "Let's go"
"Let's go"
>>> '"Hello,World!" she said'
'"Hello,World!" she said'
>>> 'Let's go'   #这里发生了错误
<span style="color:#ff0000;">SyntaxError: invalid syntax</span>
>>> 'Let\'s go'  #这里用"\"转义,避免歧义
"Let's go"
Python会明白中间的单引号是字符串中的一个字符,而不是字符串的结束标记。

1.11.2 拼接字符串

>>> "Hello." + "World!"
'Hello.World!'
>>> x = "Hello."
>>> y = "World!"
>>> x + y
'Hello.World!'

1.11.3 字符串表示,str和repr

>>> "Hello, world!"
'Hello, world!'
>>> 10000L
10000L
>>> print "Hello,world!"
Hello,world!
>>> print 10000L
10000
>>> print repr("Hello,world!")#repr函数,创建一个字符串,以合法python表达式形式表示值
'Hello,world!'
>>> print repr(10000L)
10000L
>>> print str("Hello,world!")#str函数,把值转换为合理形式的字符串
Hello,world!
>>> print str(10000L)
10000

1.11.4 inpu和raw_input的比较

运行一个实例

>>> input("Enter a number: ")
Enter a number: 3
3
>>> raw_input("Enter a number: ")#raw_input函数会把所有的输入当作原始数据(raw data),然后将其放入字符串中
Enter a number: 3
'3'      #这边是‘3’
注意:除非对input有特别的需要,否则应该尽可能使用raw_input函数

1.11.5 长字符串,原始字符串和Unicode

1.长字符串 使用三个引号代替普通引号

>>> print '''This is a long string
It contains here....."Hello,World!"...'''
This is a long string
It contains here....."Hello,World!"...
2.原始字符串

>>> print 'Hello.\nworld'
Hello.
world
>>> print 'Hello.\nworld' #\n换行符
Hello.
world
>>> path = 'C:\nowhere' <span style="color:#ff0000;">#这里表示C盘下一个目录nowhere</span>
>>> path
'C:\nowhere' <span style="color:#ff0000;">#这边没啥问题</span>
>>> print path  <span style="color:#ff0000;">#结果发生了问题</span>
C:
owhere
>>> print 'C:\\nowhere'<span style="color:#ff0000;">#利用引号进行转义</span>
C:\nowhere
>>>
原始字符串以r开头
>>> print r'C:\nowhere'
C:\nowhere
>>> print r'Let's go'
SyntaxError: invalid syntax
>>> print r'Let\'s go'
Let\'s go


3.Unicode字符串
>>> u'Hello,world!'
u'Hello,world!'
>>> #注意,在Python3.0中,所有字符串都是Unicode字符串

1.12.1 本章的新函数

abs(number)                                      #返回数字的绝对值
cmath.sqrt(number)                               #返回平方根,也可以适用于复数
float(object)                                    #将字符串和数字转换为浮点数
help()                                           #提供交互式帮助
input(prompt)                                    #获取用户输入
int(object)                                      #将字符串和数字转换为整数
long(object)                                     #将字符串和数字转换为长整型数
math.ceil(number)                                #返回数的上入整数,返回值的类型为浮点数
math.floor(number)                               #返回数的下舍整数,返回值的类型为浮点数
math.sqrt(number)                                #返回平方根,不适用与负数
pow(x,y[,z])                                     #返回x的y次幂(所得结果对Z取模)
raw_input(prompt)                                #获取用户输入,结果被看作原始字符串
repr(object)                                     #返回值的字符串表示形式
round(number[,ndigits])                          #根据给定的精度对数字进行四舍五入
str(object)                                      #将值转化为字符串</strong></span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python