博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python入门
阅读量:5219 次
发布时间:2019-06-14

本文共 2663 字,大约阅读时间需要 8 分钟。

1:Using Python as a Calculator

>>> 2+24>>> # This is a comment... 2+24>>> 2+2  # and a comment on the same line as code4>>> (50-5*6)/45.0>>> 8/5 # Fractions aren't lost when dividing integers1.6

 

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

 

>>> # Integer division returns the floor:... 7//32>>> 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.real1.5>>> a.imag0.5

the last printed expression is assigned to the variable _

>>> tax = 12.5 / 100>>> price = 100.50>>> price * tax12.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 asdfasdfaksdfasd

 

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+'>''
'

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,

 

 

 

 

 

转载于:https://www.cnblogs.com/james1207/p/3265476.html

你可能感兴趣的文章
软件测试-----Graph Coverage作业
查看>>
django ORM创建数据库方法
查看>>
创建Oracle synonym 详解
查看>>
php7 新特性整理
查看>>
RabbitMQ、Redis、Memcache、SQLAlchemy
查看>>
linux查看端口占用
查看>>
hdu - 1226 超级密码 (bfs)
查看>>
Qt重写paintEvent方法遇到的问题
查看>>
Sql常见面试题 受用了
查看>>
知识不是来炫耀的,而是来分享的-----现在的人们却…似乎开始变味了…
查看>>
CSS背景颜色、背景图片、平铺、定位、固定
查看>>
口胡:[HNOI2011]数学作业
查看>>
我的第一个python web开发框架(29)——定制ORM(五)
查看>>
中国剩余定理
查看>>
基础笔记一
查看>>
uva 10137 The trip
查看>>
Count Numbers
查看>>
编写高质量代码改善C#程序的157个建议——建议110:用类来代替enum
查看>>
网卡bond技术
查看>>
UITabbarController的UITabbarItem(例:"我的")点击时,判断是否登录
查看>>