一、启动python
[root@localhost ~]# python
Python 2.7.5 (default, Feb 11 2014, 07:46:25)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> ##提示符
输入与下命令看是否正常工作:
>>> print "Hello world!"
按下回车键,若有以下输出则正常:
Hello world!
二、数字和表达式
1、加减法
>>> 2+2 ##加减一样
4
>>> 123424+435464
558888
2、除法
>>> 1/2 ##除法只能整除
0
>>>
整除的解决办法:
(1)用实数(包含小数点的十进制数)进行运算
实数再python中被称为浮点数,只要其中一个为浮点数,其结果亦为浮点数
>>> 1/2
0
>>> 1.0/2.0
0.5
>>> 1/2.0
0.5
>>> 1.0/2
0.5
>>> 1/2.
0.5
(2)让python改变除法的执行方式
>>> from _future_ import division ##future前后是两个下划线
(3)命令行可使用命令开关-Qnew
[root@localhost ~]# python -Qnew
Python 2.7.5 (default, Feb 11 2014, 07:46:25)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 1/2
0.5
此时单斜线不再用作整除,如果需要整除,可以使用双斜线//,就算是浮点数也会整除
>>> 1//2
0
>>> 1.0//2.0
0.0
>>>
3、取余
>>> 1%2
1
>>> 10/3
3
>>> 10%3
1
>>> 9%3
0
>>> 2.75%0.5
0.25
4、幂运算
>>> 2**3 ##2的3次方等于8
8
>>> -3**2 ##-(3的平方)等于-9
-9
>>> (-3)**2 ##括号是汉语的括号,不是英语的括号,会报错
File "<stdin>", line 1
(-3)**2
^
SyntaxError: invalid syntax
>>> (-3)**2 ##(-3)的平方等于9
9
>>>
三、长整型数
在大数字后面加一个L表示长整型数,最大不超过2147483647
>>> 987867656853544671263817391831923
987867656853544671263817391831923L
四、十六进制和八进制
>>> 0xAF
175
>>> 010
8
>>>
五、变量
变量基本上就是代表某值的名字
>>> x=3
>>> x*2
6
>>>
使用变量前需要先赋值,变量名可以包括字母、数字、下划线(_),但是不能一数字开头。
六、语句
语句与表达式的区别:
表达式是某事,而语句是做某事(换句话说就是告诉计算机做什么)
七、获取用户输入
1、input函数获取输入值
>>> input("The meaning of life:")
The meaning of life:42
42
>>> x=input("x:")
x:12
>>> y=input("y:")
y:32
>>> print(x*y)
384
>>>
八、函数
1、幂运算函数pow函数
>>> 2**3
8
>>> pow(2.3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: pow expected at least 2 arguments, got 1
>>> pow(2,3)
8
>>>
注意,2,3是英语里的逗号,不是点 . 。
pow函数是内建函数,可以直接调用不用自己定义,当然也可以根据自己的需要,自己定义函数。
结合函数和运算符可以创建更复杂的表达式
>>> 10+pow(2,3*15)/3.0 ##等于10+2**(3*15)/3.0
11728124029620.666
>>>
九、模块
可以把模块想象成倒入到Python以增强其功能的扩展。
使用import命令倒入模块
如:math模块(包含floor函数)
>>> import math
>>> math.floor(32.9) ##floor函数向下取整
32.0
>>>
(不常用)可以使用“from模块import函数”,之后可以直接使用函数,而不需要模块名作为前缀
>>> from math import sqrt
>>> sqrt(9) ##没有加模块名前缀
3.0 ##sqrt为开平方函数
>>>
也可以使用变量来引用函数
如:
>>> foo=math.sqrt
>>> foo(4)
2.0
>>>
十、cmath和复数
>>> sqrt(-1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: math domain error
>>>
当对一个负数开平方是回报错,这是因为sqrt函数只能处理浮点数,而负数的平方根是虚数(实数和虚数之和为复数),复数就需要cmath(complex math,复数)来处理:
>>> import cmath
>>> cmath.sqrt(-1)
1j ##j表示是虚数
>>>
注意:python中没有单纯的虚数,他们都被看作实数部分为0的复数
十一、保存并执行程序
1、编写程序任何一个文本编辑器都可以,当然使用专用的软件肯定比较方便,我这里安装的是
PyCharm Community Edition 2016.2.3
Build #PC-162.1967.10, built on September 7, 2016
JRE: 1.8.0_112-release-b343 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
保存的文件名需以.py结尾
2、也可以通过命令提示符运行python脚本
$ python hello.py
3、让脚本像普通程序一样运行
有时运行脚本需要注意python二进制文件的路径
解决办法:可以在脚本的首行加上:
#!/usr/bin/env python
这样不管python二进制文件爱你在那里,程序都可以自动执行
但是再实际运行脚本之前,必须让脚本具有可执行的属性:
$chmod a+x hello.py
$hello.py 或者 $./hello.py
十二、注释
以#开头表示注释行,不会被执行
十三、字符串
ptint "Hello world!"
其中的"Hello world!"就是字符串,多用来表示一些文本
1、单引号字符串和转义引号
字符串是值,就像数字一样
>>> "Hello world!"
'Hello world!' ##可以直接输出
>>>
单引号是为了防止句中出现双引号或单引号的情况
>>> "let's go"
"let's go"
>>> '"Hello world" she said'
'"Hello world" she said'
>>> 'let's go'
File "<stdin>", line 1
'let's go'
^
SyntaxError: invalid syntax ##报错,它会认为'let'已经结束,后面的s不知道怎么处理
>>>
还有就是可以使用反斜线转义\
>>> 'let\'s go'
"let's go"
>>>
这样python就会明白中间的单引号是字符串中的一个字符,而不是字符串的结束标记
双引号也可以进行转义
2、拼接字符串
>>> "hello"+"world" ##就像加法一样
'helloworld'
>>> x="hello"
>>> y="world"
>>> x+y
'helloworld'
>>>
3、字符串表示
通过str函数,把值转换为合理性是的字符串,以便用户可以理解
>>> print str("hello world")
hello world ##没有双引号括起来
>>>
repr函数会创建一个字符串,他以合法的python表达式的形式来表示值
>>> print repr("hello world")
'hello world' ##字符串
>>> print repr(10000L)
10000L ##数字
如果想要打印一个包含数字的句子时 可以使用`x`(反引号)=repr(x)
>>> print "The temprature is "+temp
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects ##字符串型和整型不能运算
>>> print "The temprature is "+`temp`
The temprature is 42
>>>
反引号可以解决类型不匹配的问题
4、input和raw_input比较
>>> name=input("what is your name?")
what is your name?li ##输入的名字没有加"",报错
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'li' is not defined ##name 'li'没有定义,感觉有点像把li当作了变量的意思
>>> name=input("what is your name?")
what is your name?"li" ##输入的名字加了"",是一个字符串
>>>
这是由于python认为用户输入的是合法的表达式
这样用户在输入名字是就会特别麻烦
此时raw_input 就会把所与的输入当作原始数据,然后放入字符串中
>>> input("input a number:")
input a number:3
3
>>> raw-input("input a number:")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'raw' is not defined
>>> raw_input("input a number:")
input a number:3
'3'
>>> raw_input("input name:")
input name:li
'li'
>>>
所以:尽可能使用raw_input
5、长字符串、原始字符串
(1)长字符串
如果需要写一个很长的,需要跨越多行的字符串,可以使用三个单引号或双引号括起来:
>>> print '''asfsdfds
... asfdssf
... awfdegfrfsfd.
... aserewfaEWFD.
... WERDWE.'''
asfsdfds
asfdssf
awfdegfrfsfd.
aserewfaEWFD.
WERDWE.
>>>
也可以使用反斜线换行:
>>> 1+2+\
... 4+5
12
>>>
(2)原始字符串
当我们需要再字符串中使用\n时,如果不加以处理,python会认为这是换行符,将结果进行换行,这个时候就需要使用原始字符串,再原始字符串中输入的每个字符都会与书写的方式保持一致
>>> print r'C:\nowhere'
C:\nowhere
>>> print r'C:\nowhere'
KeyboardInterrupt
>>> print r'C:\nowhere\nowhere\nowhere'
C:\nowhere\nowhere\nowhere
>>>
原始字符串以r开头
注意:原始字符串最后一个字符不能时反斜线\,如果最后一个字符是反斜线,那么python就不知道是否应该结束字符串
>>> print r"sfesgrthth\segfdhd\nseferg\"
File "<stdin>", line 1
print r"sfesgrthth\segfdhd\nseferg\"
^
SyntaxError: EOL while scanning string literal
>>>
如果真的需要最后一个字符是反斜线,那么就需要将反斜线单独拿出来处理
>>> print r"sfesgrthth\segfdhd\nseferg" "\\"
sfesgrthth\segfdhd\nseferg\
>>>
十四、函数
ads(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) 将值转换为字符串