• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
james1207

博客园    首页    新随笔    联系   管理    订阅  订阅

python入门

1:Using Python as a Calculator

>>> 2+2
4
>>> # This is a comment
... 2+2
4
>>> 2+2  # and a comment on the same line as code
4
>>> (50-5*6)/4
5.0
>>> 8/5 # Fractions aren't lost when dividing integers
1.6


 

To do integer division and get an integer result, discarding any fractional result, there is another operator,//:

 

>>> # Integer division returns the floor:
... 7//3
2
>>> 7//-3
-3


Complex numbers are also supported; imaginary numbers are written with a suffix of j or J. Complex numbers with a nonzero real component are written as (real+imagj), or can be created with the complex(real,imag) function.

 

>>> 1j * 1J
(-1+0j)
>>> 1j * complex(0, 1)
(-1+0j)
>>> 3+1j*3
(3+3j)
>>> (3+1j)*3
(9+3j)
>>> (1+2j)/(1+1j)
(1.5+0.5j)


To extract these parts from a complex number z, usez.real andz.imag.

>>> a=1.5+0.5j
>>> a.real
1.5
>>> a.imag
0.5


the last printed expression is assigned to the variable _

>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06


2:Strings

>>> 'spam eggs'
'spam eggs'
>>> 'doesn\'t'
"doesn't"
>>> "doesn't"
"doesn't"
>>> '"Yes," he said.'
'"Yes," he said.'
>>> "\"Yes,\" he said."
'"Yes," he said.'
>>> '"Isn\'t," she said.'
'"Isn\'t," she said.'

Continuation lines can be used, with a backslash as the last character on the line indicating that the next line is a logical continuation of the line:

>>> hello="this is not \n\
asdfasdf\n\
aksdfasd"
>>> print(hello);
this is not 
asdfasdf
aksdfasd

 

a “raw” string:

>>> hello=r"this is not \n\
asdfasdf\n\
aksdfasd"
>>> print(hello);
this is not \n\
asdfasdf\n\
aksdfasd

 

Strings can be concatenated (glued together) with the + operator, and repeated with *:

>>> hello='Hello';
>>> world='World';
>>> hello=hello+world;
>>> hello
'HelloWorld'
>>> '<'+hello*5+'>'
'<HelloWorldHelloWorldHelloWorldHelloWorldHelloWorld>'


slices work like this:


 

>>> hello[0]
'H'
>>> hello[0:2]
'He'
>>> hello[2:4]
'll'

Lists

>>> a = ['spam', 'eggs', 100, 1234]
>>> a
['spam', 'eggs', 100, 1234]
>>> a[0]
'spam'
>>> a[3]
1234
>>> a[-2]
100
>>> a[1:-1]
['eggs', 100]
>>> a[:2] + ['bacon', 2*2]
['spam', 'eggs', 'bacon', 4]
>>> 3*a[:3] + ['Boo!']
['spam', 'eggs', 100, 'spam', 'eggs', 100, 'spam', 'eggs', 100, 'Boo!']


list operation:

>>> # Replace some items:
... a[0:2] = [1, 12]
>>> a
[1, 12, 123, 1234]
>>> # Remove some:
... a[0:2] = []
>>> a
[123, 1234]
>>> # Insert some:
... a[1:1] = ['bletch', 'xyzzy']
>>> a
[123, 'bletch', 'xyzzy', 1234]
>>> # Insert (a copy of) itself at the beginning
>>> a[:0] = a
>>> a
[123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
>>> # Clear the list: replace all items with an empty list
>>> a[:] = []
>>> a
[]


综合:Fibonacci例子

>>> a,b=0,1;
>>> while(b<10):
	print(b,end=',');
	a,b=b,a+b;

	
1,1,2,3,5,8,






 

 

 

 

 

posted @ 2013-08-17 23:25  Class Xman  阅读(247)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3