【Python】简单的数据类型——int、float、bool、str

目录

1. 整数类型 int

2. 浮点数类型 float

3. 布尔类型 bool

4. 字符串 str

5. 类型转换

5.1 隐式类型转换

5.2 显示类型转换

6. 输出

6.1 print函数

6.2 格式化输出

7. 输入


1. 整数类型 int

a = 10
print(type(a))
print(type(-2))
'''
<class 'int'>
<class 'int'>
'''

测试整型能表示的最大值:

print(9**999999)
'''
ValueError: Exceeds the limit (4300 digits) for integer string conversion;
use sys.set_int_max_str_digits() to increase the limit
'''

整数字符串转换超过限制(4300位);使用sys.set_int_max_str_digits()来增加这个限制。

sys.set_int_max_str_digits(maxdigits)
# 设置解释器所使用的整数字符串转换长度限制
# maxdigits必须为0或大于640,为0时表示没有限制
import sys

sys.set_int_max_str_digits(0)
print(9**999999)
# 能够正常打印

整数有十进制、十六进制、八进制、二进制表示。

# 十进制
print(10)
# 十六进制
print(0x10)  # 或0X10
# 八进制
print(0o10)  # 或0O10
# 二进制
print(0b10)  # 或0B10
'''
10
16
8
2
'''

整型占的字节数随着数字增大而增大,即整型是变长的,每次增量是4个字节。

可以用sys.getsizeof计算对象的大小,单位是字节。

import sys

print(sys.getsizeof(0))
print(sys.getsizeof(1))
print(sys.getsizeof(2))
print(sys.getsizeof(2**15))
print(sys.getsizeof(2**30))
print(sys.getsizeof(2**128))
'''
28
28
28
28
32
44
'''

2. 浮点数类型 float

x = 123.4    # 小数形式
y = 1.234e2  # 指数形式 e2、E2、e+2、E+2都可以
# 以上两种写法表示的效果相同

z = 0.000056  # 小数形式 写成.000056也可以
k = 5.6e-5    # 指数形式 e-5、E-5都可以
# 以上两种写法表示的效果相同

print(type(x))
print(type(y))
print(type(z))
print(type(k))
'''
<class 'float'>
<class 'float'>
<class 'float'>
<class 'float'>
'''

浮点型有大小限制。

import sys

# 可表示的最大正有限浮点数
print(sys.float_info.max)
# 可表示的最小正规范化浮点数
print(sys.float_info.min)
'''
1.7976931348623157e+308
2.2250738585072014e-308
'''

浮点型计算有精度损失,可以使用Decimal类进行精确计算。

from decimal import Decimal

print(8.1 / 3)
print(Decimal("8.1") / Decimal("3"))
'''
2.6999999999999997
2.7
'''

3. 布尔类型 bool

bool类型只有两个常量实例:True和False。

bool1 = 1 < 2
bool2 = 1 > 2
print(bool1)
print(bool2)
print(type(bool1))
print(type(bool2))
'''
True
False
<class 'bool'>
<class 'bool'>
'''

内置函数bool()可将任意值转换为布尔值,如果该值可以被解读为逻辑值的话。

print(type(bool(2)))
print(type(bool(0)))
'''
<class 'bool'>
<class 'bool'>
'''

bool是int的子类。在许多数字场景下,False和True的行为分别与整数0和1类似。 但是,不建议这样使用;请使用int()显式地执行转换。

print(False + 1)
print(True + 1)
print(int(False) + 1)
print(int(True) + 1)
'''
1
2
1
2
'''

4. 字符串 str

s1 = "hello"
s2 = 'world'  # 单引号和双引号都可以创建字符串
s3 = s1 + ", " + s2  # 加号可以连接字符串
print(s1)
print(s2)
print(s3)
print(type(s1))
print(type(s2))
print(type(s3))
'''
hello
world
hello, world
<class 'str'>
<class 'str'>
<class 'str'>
'''

单引号可以嵌套双引号,双引号可以嵌套单引号;单引号不能嵌套单引号,双引号不能嵌套双引号。

str1 = "'1'"  # ok '1'
str2 = ""2""  # err
str3 = '"3"'  # "3"
str4 = ''4''  # err

成对的三个单引号或双引号的作用:

  • 注释(文档字符串)
  • 多行字符串,可以使字符串内容保持原样输出
"""
注释
"""
content = """内容:
s1 = "hello"
s2 = 'world' # 单引号和双引号都可以创建字符串
s3 = s1 + ", " + s2 # 加号可以连接字符串
print(s1)
print(s2)
print(s3)
print(type(s1))
print(type(s2))
print(type(s3))
'''
hello
world
hello, world
<class 'str'>
<class 'str'>
<class 'str'>
'''
"""
print(content)

在字符串前面加'r'可以使整个字符串不会被转义:

print('换行符是\n')
print(r'换行符是\n')
'''
换行符是

换行符是\n
'''

 只由空格分隔的多个字符串字面值会合并成一条字符串。

str1 = "Apple" "Orange" "Banana"
print(str1)
'''
AppleOrangeBanana
'''

5. 类型转换

5.1 隐式类型转换

  • 根据变量的当前值决定其类型
x = 2
print(type(x))
x = 8.8
print(type(x))
x = False
print(type(x))
x = "hello"
print(type(x))
'''
<class 'int'>
<class 'float'>
<class 'bool'>
<class 'str'>
'''
  • 运算时,低精度向高精度转换
x = 1
y = 1.1
z = x + y
print(type(z))
'''
<class 'float'>
'''

5.2 显示类型转换

a = int("20")       # 默认以10进制转换
print(a)
print(type(a))

b = int("10", 16)   # 以16进制转换
print(b)
print(type(b))

c = int("0xa", 16)  # 以16进制转换
print(c)
print(type(c))

d = int(3.14)       # float -> int
print(d)
print(type(d))

'''
20
<class 'int'>
16
<class 'int'>
10
<class 'int'>
3
<class 'int'>
'''
a = float(33)
print(a)
print(type(a))

b = float("10.2")
print(b)
print(type(b))

'''
33.0
<class 'float'>
10.2
<class 'float'>
'''
a = str(100)
print(a)
print(type(a))

b = str(88.88)
print(b)
print(type(b))

'''
100
<class 'str'>
88.88
<class 'str'>
'''

6. 输出

6.1 print函数

print(*objects, sep=' ', end='\n', file=None, flush=False)
# 将objects打印输出至file指定的文本流,以sep分隔并在末尾加上end
print("apple")
print("apple", "orange")
print("apple", "orange", sep="@")
print("apple", "orange", end="%%")
print("apple", "orange", sep="@", end="%%")
print("")

6.2 格式化输出

  • 格式控制符(和C语言printf的格式控制符一样)
  • format函数
  • f-string(格式化字符串)
name = '赵敏'
age = 20
gender = "女"
score = 99.9
print("个人信息:%s-%d-%s-%.2f" % (name, age, gender, score))
print("个人信息:{}-{}-{}-{}".format(name, age, gender, score))
print(f"个人信息:{name}-{age}-{gender}-{score}")
'''
个人信息:赵敏-20-女-99.90
个人信息:赵敏-20-女-99.9
个人信息:赵敏-20-女-99.9
'''

7. 输入

input([prompt])
# 如果存在prompt实参,则将其写入标准输出,末尾不带换行符。
# 接下来,该函数从输入中读取一行,将其转换为字符串(除了末尾的换行符)并返回。
# 当读取到EOF时,则触发EOFError。
name = input("请输入姓名:")
age = input("请输入年龄:")
score = input("请输入成绩:")
print(f"name: {name} type: {type(name)}")
print(f"name: {age} type: {type(age)}")
print(f"name: {score} type: {type(score)}")
'''
请输入姓名:李莲花
请输入年龄:30
请输入成绩:99.99
name: 李莲花 type: <class 'str'>
name: 30 type: <class 'str'>
name: 99.99 type: <class 'str'>
'''
name = input("请输入姓名:")
age = int(input("请输入年龄:"))
score = float(input("请输入成绩:"))
print(f"name: {name} type: {type(name)}")
print(f"name: {age} type: {type(age)}")
print(f"name: {score} type: {type(score)}")
'''
请输入姓名:李相夷
请输入年龄:18
请输入成绩:99.9999
name: 李相夷 type: <class 'str'>
name: 18 type: <class 'int'>
name: 99.9999 type: <class 'float'>
'''
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值