1.4数字和表达式
>>> 1/2
0
两种解决方案
1用实数(包含小数点的十进制数)而不是证书进行运算
>>> 1.0/2.0
0.5
2 如果希望python只执行普通的除法
>>> from __future__ import division
>>> 1/2
0.5
整除://
>>> 1//2
0
取余运算符:%
>>> 1%2
1
幂预算:**
>>> 2**8
256
-3**2=-(3**2)
1.4.1 长整型:L
普通整数不能大于2147483647也不能小于-2147483648,长整型和整数书写一致,在后面加上L
1.4.2 十六进制和八进制
>>> 0xAF
175
>>> 010
8
1.5 变量
>>> x=3
这样的操作成为赋值,值3赋给了变量x,或者将变量x绑定到了值3上面,赋值后,可以在表达式中使用变量
>>> x**2
9
1.6语句
表达式是‘某事’ 语句是‘做某事’,换句话就是告诉计算机要做什么
1.7 获取用户输入
>>> input("the meaning of life:")
the meaning of life:42
42
>>> x=input("x:")
x:42
>>> y=input("y:")
y:42
>>> print x*y
1764
1.8 函数
幂:pow()
>>> pow(2,3)
8
>>> 2**3
8
函数就像用来实现特定功能的小程序,也可以自己自定义函数,通常把pow等的标准函数成为内建函数
绝对值:abs
>>> abs(-10)
10
四舍五入
>>> round(5.0/9.0)
1.0
如果想截取给定数值向下的整数?可以使用函数floor
1.9模块
可以把模块想象成导入到python以增强其功能的扩展,需要使用命令import来导入模块
>>> math.floor(5.0/9.0)
0.0
1.9.1 cmath和复数
sqrt函数用于计算一个数的平方根
1.10.1在实际运行脚本之前:chmod a+x hello.py
现在就可以这样运行代码了:hello.py或者试试./hello.py
1.11字符串
主要用法就是表示一些文本
1.11.1 单引号字符串和转义引号
>>> 'let's go'
SyntaxError: invalid syntax
使用反斜线对字符串中的引号进行转义
>>> 'let\'s go'
"let's go"
1.11.2 拼接字符串
>>> "Hello,"+"world"
'Hello,world'
>>> x="hello,"
>>> y="world"
>>> x+y
'hello,world'
1.11.3 字符串表示
前面通过python打印机的字符串还是被引号括起来的,如果你希望用户看不到状态,使用pring就不一样了
>>> print "Hello,world!"
Hello,world!
>>> 1000L
1000L
>>> print 1000L
1000
这里讨论的实际上是值被转换为字符串的两种机制,可以通过以下两个函数来使用这两种机制:
1、str 把值转换为合理形式的字符串
2、repr 会创建一个字符串,以合法的python表达式的形式来表示值
>>> print repr(1000L)
1000L
>>> print repr("Hello, world!")
'Hello, world!'
>>> print str(1000L)
1000
>>> print str("Hello, world!")
Hello, world!
下面第一print语句无法工作,那是因为不可以将字符串和数字进行相加,,后面两个可以,是因为已经将temp转换为字符串”42“
>>> temp=42
>>> print "the temperature is " + temp
Traceback (most recent call last):
File "<pyshell#37>", line 1, in <module>
print "the temperature is " + temp
TypeError: cannot concatenate 'str' and 'int' objects
>>> print "the temperature is " + repr(temp)
the temperature is 42
>>> print "the temperature is " + str(temp)
the temperature is 42
str repr和反引号是将python值转换为字符串的3种方法
1.11.4 input和raw_input的比较
name=input("what is your name?")
what is your name?gumby
Traceback (most recent call last):
File "<pyshell#41>", line 1, in <module>
name=input("what is your name?")
File "<string>", line 1, in <module>
NameError: name 'gumby' is not defined
name=input("what is your name?")
what is your name?"gumby"
>>> print "hello,"+name+"!"
hello,gumby!
问题在于input会假设用户输入的是合法的python表达式(看起来和repr函数相反),以字符串输入则没有问题
使用raw_input会把所有的输入当做原始数据(raw data)然后将其放入字符串中:
>>> name=raw_input("what is your name?")
what is your name?gumby
>>> print "hello,"+name+"!"
hello,gumby!
1.11.5 长字符串、原始字符串和Unicode
1、长字符串
如果需要写一个非常长的字符串,可以使用三个引号代替普通引号,也可以使用三个"""代替
print '''this is a very long string
it continues here
and it's not over yet
"hello, world!"
still here.'''
this is a very long string
it continues here
and it's not over yet
"hello, world!"
still here.
提示:
>>> print "hello, \
world!"
hello, world!
>>> 1+2+\
4+5
12
>>> print \
"hello,world!"
hello,world!
2原始字符串
原始字符串不会把反斜线当做特殊字符
>>> path=r'c:\program files\fnord\foo'
>>> print path
c:\program files\fnord\foo
原始字符串的最后一个字符不可以是\
>>> path=r'c:\program files\fnord\'
SyntaxError: EOL while scanning string literal
如果希望原始字符串以\结束的话:
>>> path=r'c:\program files\fnord''\\'
>>> print path
c:\program files\fnord\
1.12小结:
算法:描述如何完成一项任务的方法
表达式:是计算机程序的组成部分,用于表示值。2+2就是表达式,表示数值4
变量:是一个名字,表示某个值
语句:告诉计算机做某些事情的指令。可能涉及变量、向屏幕打印内容,导入模块或者其他大量复杂的操作
函数:python中的函数就像数学中的函数:可以带有参数,并且返回值
模块:模块是扩展。可以导入到python中,从而扩展python的功能
字符串:就是几段文本
1.12.1 本章的新函数
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) 将值转换为字符串
>>> pow(2,3,5)
3
>>> 8%5
3