In [1]:
1/2
Out[1]:
0
In [2]:
1/2.
Out[2]:
0.5
In [6]:
 #可在程序前都端加入以下语句,或在解释器上执行
In [7]:
from __future__ import division
In [8]:
1/2
Out[8]:
0.5
In [9]:
#如果通过命令行运行python,还可以使用命令开关-Qnew
In [10]:
#此时整除用双斜杠//代替
In [11]:
1//2
Out[11]:
0
In [12]:
#幂运算符为××而不是^
In [13]:
(-3)**2
Out[13]:
9
In [14]:
#注意:取反运算符-比幂运算符优先级低
-3**2
Out[14]:
-9
In [16]:
#普通整数不能大于2 147 483 647(也不能小于-2 147 483 648)
#如果需要更大的数则需要使用长整数,即在整数末尾加如字母l或L(通常用的是L)
#2.2版本以后的python会自动将超出范围的整数转化为长整数
1000000000000000000000000000000
Out[16]:
1000000000000000000000000000000L
In [19]:
#print在python3中是作为函数,在python2中则为语句
print(2*2)
print 2*2
4
4

In [22]:
#获取用户输入
x = input("Please input:")
Please input:42

In [23]:
x
Out[23]:
42
In [24]:
#if语句的简单使用
In [25]:
if 1==2: print'One equal two'
In [26]:
if 1==1: print 'One equal one'
One equal one

In [28]:
#指数运算函数pow()
2**3
Out[28]:
8
In [29]:
pow(2,3)
Out[29]:
8
In [30]:
#绝对值函数abs()
abs(-4)
Out[30]:
4
In [32]:
#取近似round()
#python2中为四舍五入
round(1.0/2.0)
Out[32]:
1.0
In [33]:
#模块的导入和调用
In [34]:
#导入模块,调用其中的函数
In [35]:
import math
In [36]:
#floor函数向下取整(结果仍为浮点数)
math.floor(32.9)
Out[36]:
32.0
In [37]:
#可以使用int函数强制转换
int( math.floor(32.9) )
Out[37]:
32
In [38]:
#导入模块中的某函数,直接调用
In [39]:
from math import sqrt
In [40]:
#sqrt函数实现开方(返回值为浮点数)
sqrt(9)
Out[40]:
3.0
In [41]:
#math模块只能处理实数
sqrt(-1)   #会报错
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-41-94b2bd564ad1> in <module>()
      1 #math模块只能处理实数
----> 2 sqrt(-1)   #会报错

ValueError: math domain error
In [42]:
#如果需要处理复数,可以使用cmath模块
import cmath
In [43]:
cmath.sqrt(-1)
Out[43]:
1j
In [44]:
#虚数部分以j或J结尾(而不是i),注意即使虚数部分的数字为1,也不能省略!
cmath.sqrt(-1+1j)
Out[44]:
(0.45508986056222733+1.09868411346781j)
In [46]:
#单引号和双引号都能用来表示字符串
#某些情况下有特殊的作用
In [47]:
'let's go'
  File "<ipython-input-47-135c98564c19>", line 1
    'let's go'
         ^
SyntaxError: invalid syntax
In [48]:
"let's go"
Out[48]:
"let's go"
In [49]:
#此处也可以使用反斜杠(\)来转义
'let\'s go'
Out[49]:
"let's go"
In [50]:
#反斜杠的表示方法并不直观,应该尽量避免
In [51]:
#拼接字符串
In [52]:
#两个字符串常量前后用空格隔开会自动拼接
"Let's say" '"Hello world"'
Out[52]:
'Let\'s say"Hello world"'
In [55]:
#两个变量则不行
x="Hello,"
y="worlde."
In [56]:
x y
  File "<ipython-input-56-eee9f509dce8>", line 1
    x y
      ^
SyntaxError: invalid syntax
In [57]:
x+y
Out[57]:
'Hello,worlde.'
In [58]:
#str和repr
#str会把值转化为合理形式的字符串,便于用户理解
#repr会创建一个字符串,以合理的python表达式的形式来表示
In [59]:
print( str("Hello world") )
Hello world

In [60]:
print( repr("Hello world") )
'Hello world'

In [61]:
print( str(10000L) )
10000

In [62]:
print( repr(10000L) )
10000L

In [64]:
#互动解释器采用repr显示,而print采用str显示
In [65]:
1000L
Out[65]:
1000L
In [66]:
print 1000L
1000

In [67]:
#打印包含数字的句子
In [68]:
temp = 42
In [69]:
print "The temperature is " + temp
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-69-31dc07136a5e> in <module>()
----> 1 print "The temperature is " + temp

TypeError: cannot concatenate 'str' and 'int' objects
In [70]:
print "The temperature is " + repr(temp)
The temperature is 42

In [71]:
#python2中可以用反引号``代替repr,但是python3不支持
In [72]:
#input接受的数据认为是合法的python表达式
In [73]:
name = input("What is your name?")
print("Hello, " + name + "!")
What is your name?zk

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-73-592b5811b29e> in <module>()
----> 1 name = input("What is your name?")
      2 print("Hello, " + name + "!")

/usr/lib/python2.7/dist-packages/IPython/kernel/zmq/ipkernel.pyc in <lambda>(prompt)
    362         if content.get('allow_stdin', False):
    363             raw_input = lambda prompt='': self._raw_input(prompt, ident, parent)
--> 364             input = lambda prompt='': eval(raw_input(prompt))
    365         else:
    366             raw_input = input = lambda prompt='' : self._no_raw_input()

/usr/lib/python2.7/dist-packages/IPython/kernel/zmq/ipkernel.pyc in <module>()

NameError: name 'zk' is not defined
In [74]:
name = input("What is your name?")
print("Hello, " + name + "!")
What is your name?"zk"
Hello, zk!

In [75]:
#而raw_input可以把接受的数据作为原始数据
In [76]:
name = raw_input("What is your name?")
print("Hello, " + name + "!")
What is your name?zk
Hello, zk!

In [77]:
#如果特殊需要,一般使用raw_input
In [78]:
#长字符串,用三个引号包含起来,可跨行
In [79]:
print '''This is a long string.
lalalalallalalalla!
hahahahahhaha!!!'''
This is a long string.
lalalalallalalalla!
hahahahahhaha!!!

In [80]:
#但是,如果行末出现反斜杠,回车键会被转义
In [81]:
print '''This is a long string.
lalalalallalalalla!\
hahahahahhaha!!!'''
This is a long string.
lalalalallalalalla!hahahahahhaha!!!

In [82]:
#\[Enter]表示本行语句未结束,连续到下一行
In [83]:
1 + 2 + 3\
4 + 5 + 6
Out[83]:
48
In [84]:
#原始字符串可以使反斜杠不被转义,在书写路径时非常有用
In [85]:
print("c:\\program files\\found\\foo\\bar\\baz\\frozz\\bozz")
c:\program files\found\foo\bar\baz\frozz\bozz

In [86]:
print(r"c:\program files\found\foo\bar\baz\frozz\bozz")
c:\program files\found\foo\bar\baz\frozz\bozz

In [87]:
#原始字符串最后一个字符不能是反斜杠
print(r"This is illegal\")
  File "<ipython-input-87-0fb87d06238d>", line 2
    print(r"This is illegal\")
                             ^
SyntaxError: EOL while scanning string literal
In [88]:
print(r"This is illegal" "\\")
This is illegal\

In [89]:
#python2默认使用8位的ASCII字符串,python3默认使用16位的Unicode字符串
In [90]:
u"Hello world!"
Out[90]:
u'Hello world!'
In []: