【整理】Python基础(一)

数据类型与简单数据结构


为了方便编程,整理一些Python的基础知识,以供随时参考。

数字

整数

整数类型:int
int(a)可以把变量a的类型转变为整型。str(a)则把a变为字符型。
可执行运算:加(+)、减(-)、乘(*)、除(/)、乘方(**)
除(/)永远返回浮点数类型,如果除法运算要得到整数结果,即忽略小数,就用“//”运算。
%:计算余数。

>>> 17 / 3  # classic division returns a float
5.666666666666667
>>>17 // 3  # floor division discards the fractional part
5
 >>>17 % 3  # the % operator returns the remainder of the division
2
 >>>5 * 3 + 2  # result * divisor + remainder
17
>>> 5 ** 2  # 5 squared
25
>>> 2 ** 7  # 2 to the power of 7
128

复数:使用后缀 j 或者 J 就可以表示虚数部分(例如 3+5j )。

浮点数

有小数部分的数。
浮点数类型:float

字符串

1.字符串的表达:单引号(‘…’)、双引号(“…”)
2.反斜杠 \ 用来转义,可以表示字符类型的引号、换行等。配合print语句使用,效果奇佳。

>>> 'spam eggs'  # single quotes
'spam eggs'
>>> 'doesn\'t'  # use \' to escape the single quote...
"doesn't"
>>> "doesn't"  # ...or use double quotes instead
"doesn't"
>>> '"Yes," they said.'
'"Yes," they said.'
>>> "\"Yes,\" they said."
'"Yes," they said.'
>>> '"Isn\'t," they said.'
'"Isn\'t," they said.'
>>> '"Isn\'t," they said.'
'"Isn\'t," they said.'
>>> print('"Isn\'t," they said.')
"Isn't," they said.
>>> s = 'First line.\nSecond line.'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值