Python 中的类型转换

在本教程中,我们将学习如何在 Python 中进行类型转换。

我们遇到不同类型的算术运算,其中涉及多种数据类型,然后相应地获得结果。

在这里我们将讨论两者,

  1. 隐式类型转换
  2. 显式类型转换

让我们借助程序来理解它们-

隐式类型转换

在隐式类型转换期间,用户不应该在转换期间提及任何特定的数据类型。

下面的程序说明了如何用 Python 来实现。


#program to demonstrate implicit type conversion
#initializing the value of a
a = 10
print(a)
print("The type of a is ", type(a))
#initializing the value of b
b = 4.5
print(b)
print("The type of b is ", type(b))
#initializing the value of c
c = 4.0
print(c)
print("The type of c is ", type(c))
#initializing the value of d
d = 5.0
print(d)
print("The type of d is ", type(d))
#performing arithmetic operations
res = a * b
print("The product of a and b is ", res)
add = c + d
print("The addition of c and d is ", add)

输出:

10
The type of a is <class 'int'>
4.5
The type of b is <class 'float'>
4.0
The type of c is <class 'float'>
5.0
The type of d is <class 'float'>
The product of a and b is  45.0
The addition of c and d is  9.0

解释-

让我们看一下这个节目的解说。

  1. 为了检查这些值在执行操作时是如何转换的,我们初始化了 a、b、c 和 d 的值。
  2. 在此之后,我们检查了它们每个的数据类型。
  3. 最后,我们对变量 a 和 b 执行加法,对变量 c 和 d 执行乘法。
  4. 在执行上述程序时,我们可以观察到,在产品的情况下,最终结果是一个浮点值(a 是整数,b 是浮点)。

现在,是时候继续下一件事了,那就是显式类型转换。

显式类型转换

在这种类型转换中,用户应该在函数中传递值以获得所需的数据类型。

int()、float()和 str()主要用于显式类型转换。

考虑下面给出的程序,


#program to demonstrate explicit type conversion
#initializing the value of a
a=10.6
print("The type of 'a' before typecasting is ",type(a))
print(int(a))
print("The type of 'a' after typecasting is",type(a))
#initializing the value of b
b=8.3
print("The type of 'b' before typecasting is ",type(b))
print(int(b))
print("The type of 'b' after typecasting  is",type(b))
#initializing the value of c
c=7
print("The type of 'c' before typecasting is ",type(c))
print(float(c))
print("The type of 'c' after typecasting is",type(c))

输出:

The type of 'a' before typecasting is  <class 'float'>
10
The type of 'a' after typecasting is <class 'float'>
The type of 'b' before typecasting is  <class 'float'>
8
The type of 'b' after typecasting  is <class 'float'>
The type of 'c' before typecasting is  <class 'int'>
7.0
The type of 'c' after typecasting is <class 'int'>

解释-

让我们了解一下我们在这个项目中做了什么。

  1. 我们已经初始化了 a、b 和 c 的值。
  2. 我们在这个程序中使用了 int()和 float()来查看显式类型转换是如何发生的。
  3. 在执行这个程序时,我们可以验证数据类型是如何变化的。

最后,让我们看一下本文的最后一个程序,它涵盖了显式类型转换的可能类型。


#program to demonstrate explicit type conversion
#initializing the value of a
a=10
print("The type of 'a' before typecasting is ",type(a))
print(str(a))
print("The type of 'a' after typecasting is",type(a))
#initializing the value of b
b='8'
print("The type of 'b' before typecasting is ",type(b))
print(int(b))
print("The type of 'b' after typecasting  is",type(b))
#initializing the value of c
c='7.9'
print("The type of 'c' before typecasting is ",type(c))
print(float(c))
print("The type of 'c' after typecasting is",type(c))

输出:

The type of 'a' before typecasting is  <class 'int'>
10
The type of 'a' after typecasting is <class 'int'>
The type of 'b' before typecasting is  <class 'str'>
8
The type of 'b' after typecasting  is <class 'str'>
The type of 'c' before typecasting is  <class 'str'>
7.9
The type of 'c' after typecasting is <class 'str'>

解释-

该方法类似于上面的程序,在这个程序中,我们包含了使用所有三个 int()、str()和 float()的转换。

结论

在本教程中,我们学习了什么是 python 中的类型转换,它的类型是什么,以及如何在 Python 中执行。


原创文章,作者:HEYWN,如若转载,请注明出处:https://www.506064.com/n/127529.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
HEYWN的头像HEYWN
上一篇 2024-10-03 23:15
下一篇 2024-10-03 23:16

相关推荐

  • Python列表中负数的个数

    Python列表是一个有序的集合,可以存储多个不同类型的元素。而负数是指小于0的整数。在Python列表中,我们想要找到负数的个数,可以通过以下几个方面进行实现。 一、使用循环遍历…

    编程 2025-04-29
  • 如何查看Anaconda中Python路径

    对Anaconda中Python路径即conda环境的查看进行详细的阐述。 一、使用命令行查看 1、在Windows系统中,可以使用命令提示符(cmd)或者Anaconda Pro…

    编程 2025-04-29
  • Python计算阳历日期对应周几

    本文介绍如何通过Python计算任意阳历日期对应周几。 一、获取日期 获取日期可以通过Python内置的模块datetime实现,示例代码如下: from datetime imp…

    编程 2025-04-29
  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • Python中引入上一级目录中函数

    Python中经常需要调用其他文件夹中的模块或函数,其中一个常见的操作是引入上一级目录中的函数。在此,我们将从多个角度详细解释如何在Python中引入上一级目录的函数。 一、加入环…

    编程 2025-04-29
  • Python字典去重复工具

    使用Python语言编写字典去重复工具,可帮助用户快速去重复。 一、字典去重复工具的需求 在使用Python编写程序时,我们经常需要处理数据文件,其中包含了大量的重复数据。为了方便…

    编程 2025-04-29
  • Python清华镜像下载

    Python清华镜像是一个高质量的Python开发资源镜像站,提供了Python及其相关的开发工具、框架和文档的下载服务。本文将从以下几个方面对Python清华镜像下载进行详细的阐…

    编程 2025-04-29
  • python强行终止程序快捷键

    本文将从多个方面对python强行终止程序快捷键进行详细阐述,并提供相应代码示例。 一、Ctrl+C快捷键 Ctrl+C快捷键是在终端中经常用来强行终止运行的程序。当你在终端中运行…

    编程 2025-04-29
  • Python程序需要编译才能执行

    Python 被广泛应用于数据分析、人工智能、科学计算等领域,它的灵活性和简单易学的性质使得越来越多的人喜欢使用 Python 进行编程。然而,在 Python 中程序执行的方式不…

    编程 2025-04-29
  • int类型变量的细节与注意事项

    本文将从 int 类型变量的定义、声明、初始化、范围、运算和类型转换等方面,对 int 类型变量进行详细阐述和讲解,帮助读者更好地掌握和应用 int 变量。 一、定义与声明 int…

    编程 2025-04-29

发表回复

登录后才能评论