python笔记(1)

>>> a=5
>>> b=3
>>> a+b
8
>>> a='5'
>>> b='3'
>>> a+b
'53'
 
★三重引号定义多行字符串
long_string = """Sing a song of sixpence, a pocket full of rye,Four and twenty blackbirds baked in a pie.When the pie was opened the birds began to sing.Wasn't that a dainty dish to set before the king?"""
所用的引号可以是双引号也可以是单引号
 
>>> print 3.0 / 2
1.5
在 Python 2 中,做除法的方式称为“整除”(floor division)。但 Python 3
的工作方式不一样,在 Python 3 中,如果你使用常规的除法操作符(前斜杠
“/”),则做的是常规除法,而不是整除。
>>> print 3/2
1.5
要在 Python 3 中做整除,需要使用两个前斜杠(//):
>>> print 3//2
1
这是 Python 2 和 Python 3 最显著的区别之一,也是导致很多 Python 2 程
序与 Python 3 程序不兼容的原因
 
★表示3∧5
>>> print 3 ** 5
243
 
利用 ** 还可以用非整数作为指数,如下:
>>> print 3 ** 5.5
420.888346239
要想利用乘法来做到这一点可不容易
 
 
 
★自增自减
>>> number = 7
>>> number += 1
>>> print number
8
number increased by 1
>>> number = 7
>>> number -= 1
>>> print number
6
 
 
★数据类型转换
>>> a = 24
>>> b = float(a)
>>> a
24
>>> b
24.0
 
>>> e = 54.99
>>> f = int(e)
>>> print e
54.99
>>> print f
54
Python 还提供了函数 type(),它可以明确地告诉我们变量的类型。
posted @ 2020-02-04 11:23  wuqi1003  阅读(131)  评论(0编辑  收藏  举报