python学习1.3数

一、整数:在Python中,可对整数执行 加(+)减(-)乘(*)除(/)整除(//)和取余(%)(取余公式为被除数-除数*商)与运算( & ),或( | )运算,而且还支持运算次序;

1、eg:输入:

message=2 + 3 * 4
print(message)

输出:

14

2、用两个(*)表示乘方,三个(*)会报错

eg:输入:

message=3**3
print(message)

输出: 

27

二、浮点数:通常情况下会按照你的操作执行

eg:输入:

message=0.2*2
print(message)

输出:

0.4

注:但是小数位数可能不确定

eg:输入:

message = 0.2*0.1
print(message)

输出:

0.020000000000000004

注:此时可以使用模块

输入:

from decimal import Decimal
message = 0.2*0.1
print(message)
print(Decimal("0.2")*Decimal("0.1"))

输出: 

0.020000000000000004
0.02

三、整数和浮点数:

1、将两个数相除,结果总是浮点数

eg:输入:

message=4/2
print(message)

输出:

2.0

2、在任何运算中,如果其中一个是浮点数,结果也总为浮点数

eg:输入:

message=0.2/2
print(message)

输出:

0.1

四、数中的下划线:书写很大的数时,可用下划线将数字分组,使它清晰易读

eg:输入:

message=14_000_00000_0
print(message)

输出(不会打印下划线):

14000000000

五、同时给三个变量赋值:

eg:输入:

a,b,c=1,2,3
print(a)
print(b)
print(c)

输出:

1
2
3

注:交换a和b的值

输入:

a, b = 1, 2
print(a, b)
a, b = b, a
print(a, b)

输出: 

1 2
2 1

六、常量,python没有内置的常量类型,也没有C里面的宏定义,可以使用全大写来指出某个变量视为常量;

七、注释:在python中注释用#表示

eg:输入:

# cy cy cy
a,b,c=1,2,3
print(a)
print(b)
print(c)

输出:

1
2
3

八、Python之禅命令:import this

输入:

import this

输出:

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

注:

(1)左移位( << ,后跟移动位数)(高位溢出,低位补0,相当于 *2),右移位( >>,后跟移动位数 )(低位截断,高位补零,相当于/2)

(2)运算符的优先级:算术运算符 > 位运算符 > 比较运算符 > 布尔运算符(and , or)> 赋值运算符

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值