python中的数据类型中int表示_python 中基础数据类型的运算

int: 加, 减, 乘, 除, 整除, 乘方, 取模

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

c = a + b

d = a - b

e = a * b

f = a / b

g = a // b

h = a % b

print(type(a), a, b, c, d, e, f, g, h, sep='\n')

代码执行结果:

1

2

3

-1

2

0.5

0

1

小数: float, decimal, fractions 和小数的取整方法fractions --- 分数 - Python 3.9.1 文档

import math

from decimal import Decimal

a, b = float(1.1), Decimal("2.2")

# 四舍五入取整

c = round(a)

# 向下取整

d = int(a)

# 向上取整

e = math.ceil(a)

浮点数在计算机中能否精确表示 = 0.1 + 0.1 + 0.1 - 0.3 == 0

decimal能否精确表示小数 = Decimal("0.1") + Decimal("0.1") + Decimal("0.1") - Decimal("0.3") == Decimal("0")

print(type(a), a, b, c, d, e, 浮点数在计算机中能否精确表示, decimal能否精确表示小数, sep='\n')fractions --- 分数 - Python 3.9.1 文档​docs.python.orgdecimal --- 十进制定点和浮点运算 - Python 3.9.1 文档​docs.python.org

代码执行结果:

1.1

2.2

1

1

2

False

True

bool:

a = True

b = False

c = not a

# bool 值常常用于 if-else, 连接符是 or 和 and, 用 or 连接的条件只要有一个为真, 整个表达式被判断为 True

# 用 and 连接的条件只要有一个为加, 整个表达式被判断为 False

# not 操作符能让 True 变成 False, 或者将 False 变成 True

if 0 < 1 and 2 <= 2 and 1 != 2 and 3 > 2:

print('上述条件中的数据都会被 python 判断为 True, 这句话会被打印')

if True and 1 and [0] and (0,):

print('上述条件中的数据都会被 python 判断为 True, 这句话会被打印')

if {} or dict() or None or [] or list() or set() or () or tuple() or 0:

print('上述条件中的数据都会被 python 判断为 False, 这句话不会被打印')

print(a, b, c, sep='\n')

代码执行结果:

上述条件中的数据都会被 python 判断为 True, 这句话会被打印

上述条件中的数据都会被 python 判断为 True, 这句话会被打印

True

False

False

str:

a = str(' coder ')

b = a.replace('c', '0')

c = a.split('d')

d = a.strip()

e = a.lstrip()

f = a.rstrip()

g = a.find('co')

h = a.casefold()

i = a.capitalize()

j = a.center(20, '-')

k = a.startswith(' co')

l = a.endswith('er ')

m = a.lower()

n = "{0}, {1}".format('coder1', 'coder2')

o = len(a)

print(type(a), a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, sep='\n')

代码执行结果:

coder

0oder

[' co', 'er ']

coder

coder

coder

1

coder

coder

------ coder -------

True

True

coder

coder1, coder2

7

list:

a = list('coder')

b = a[0]

c = a[0:1]

d = a[:-1]

e = a[::-1]

f = a.index('e')

g = len(a)

h = [i+'-' for i in a]

print(type(a), a, b, c, d, e, f, g, h, sep='\n')

代码执行结果:

['c', 'o', 'd', 'e', 'r']

c

['c']

['c', 'o', 'd', 'e']

['r', 'e', 'd', 'o', 'c']

3

5

['c-', 'o-', 'd-', 'e-', 'r-']

tuple:

a = 0, 1

b = a[0]

c = a[1]

d = a.index(0)

e = len(a)

print(type(a), a, b, c, d, e, sep='\n')

代码执行结果:

(0, 1)

0

1

0

2

dict:

a = dict()

a.update({'name': 'coder'})

a.setdefault('name', 'coder1')

a.setdefault('name2', 'coder2')

b = a.items()

c = a.keys()

d = a.values()

print(type(a), a, b, c, d, sep='\n')

代码执行结果:

{'name': 'coder', 'name2': 'coder2'}

dict_items([('name', 'coder'), ('name2', 'coder2')])

dict_keys(['name', 'name2'])

dict_values(['coder', 'coder2'])

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值