GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • C programming
  • C++ programming
  • Java programming
  • Python programming
  • HTML
  • CSS
  • Codenora IDE (opens in a new tab)
  • About us
  • Pricing
  • Contact us
  • Privacy policy
  • Terms of service
  • Refund and Cancellation

This view

  • Original page
  • New address

GeezerVison is showing www.dremendo.com — Typecasting in Python Programming Language | Dremendo

  • Dremendo
  • C
  • C++
  • Java
  • Python
  • HTML
  • CSS
  • More
    • Home
    • C Tutorial
    • C++ Tutorial
    • Java Tutorial
    • Python Tutorial
    • HTML Tutorial
    • CSS Tutorial
?>
profile image
Announcements
No new announcements.
mentora icon
Python Programming Tutorial
  • Python Basic Concepts
    • Introduction
    • Variable
    • Data Types
    • Naming Conventions
    • Typecasting
  • Operators in Python
    • Operators
    • Assignment Operators
    • Arithmetic Operators
    • Relational Operators
    • Logical Operators
    • Identity Operators
    • Membership Operators
    • Operator Precedence
  • Input and Output in Python
    • Environment Setup
    • print() Function
    • input() Function
    • Comments
  • Decision Making in Python
    • Decision Making
    • if Statement
    • if else Statement
    • if elif Statement
  • Mathematical Functions
    • Mathematical Functions
  • Loops in Python
    • Loops
    • for Loop
    • while Loop
  • String in Python
    • String
    • String Methods
  • Built-in Data Structures in Python
    • Lists
    • Tuples
    • Sets
    • Dictionaries
  • Arrays in Python
    • Arrays
  • Searching in Python
    • Linear Search
    • Binary Search
  • Sorting in Python
    • Bubble Sort
    • Selection Sort
    • Insertion Sort
  • Function in Python
    • Function
    • Recursive Function
    • Lambda Function
    • Closures
    • Decorators
    • Generators
  • OOPs in Python
    • Classes and Objects
    • Instance Variables
    • Instance Methods
    • Class Variables
    • Static Methods
    • Inheritance
    • Types of Inheritance
    • MRO
    • Polymorphism
    • Abstract Classes
  • User-Defined Data Structures in Python
    • Stack
    • Queue
    • Circular Queue
    • Double Ended Queue
    • Singly Linked List
    • Doubly Linked List
    • Stack using Linked List
    • Queue using Linked List
  • Modules in Python
    • Modules
    • Time Module
    • Datetime Module
    • OS Module
    • Shutil Module
    • Sys Module
    • JSON Module
    • Random Module
  • Regular Expression in Python
    • Regular Expression
  • Exceptions in Python
    • Exception Handling
  • File Handling in Python
    • Text File Handling
    • Binary File Handling
    • CSV File Handling

bookmark
Table of Contents

    Typecasting in Python Programming Language

    In this lesson, we will learn about what is Typecasting and different types of it. We will also go through some examples and take a quiz at the end.

    What is Typecasting

    In Python program the process of changing one data type into another data type is known as Typecasting. There are two types of typecasting:

    • Implicit Typecasting
    • Explicit Typecasting
    Video Explanation

    Click the button below to watch the explanation.

    Please wait while the video is loading....
    video image

    Implicit Typecasting

    Implicit typecasting, also known as automatic type conversion, occurs when the Python interpreter automatically converts a value from one data type to another without the need for programmer intervention. This typically happens when a smaller data type is assigned to a larger data type.

    Example
    a = 5        # integer
    b = 2.5      # float
    c = a + b    # Python automatically converts 'a' to a float and the result will be 7.5

    Here, you can see that the first variable, a, is an integer because its value is a whole number without a decimal point. On the other hand, the second variable, b, is a float because it contains a number with a decimal point.

    In this case, Python implicitly converts the integer variable a to a float before performing the addition. This automatic type conversion happens when a smaller data type (like an integer) needs to be converted to a larger or more complex data type (like a float) to avoid data loss. This type of conversion is known as Implicit Casting.

    Note

    In the above example, # are called comments which are used to provide information about lines of code. We will learn more about comments in our upcoming lessons.

    Explicit Typecasting

    Explicit typecasting, also known as manual type conversion, occurs when the programmer explicitly converts a value from one type to another using built-in type conversion functions such as int(), float(), str(), etc.. The syntax for explicit type casting is:

    Syntax
    datatype_name(value)
    Example 1 (Explicit Typecasting from integer to float using float() function)
    a = 8
    b = float(a)    # result of b will be 8.0

    Here, you can see that we have converted the integer value 8 into the float value 8.0 using the float() function.

    Example 2 (Explicit Typecasting from float to integer using int() function)
    a = 5.8
    b = int(a)    # result of b will be 5

    Here, you can see that we have converted the float value 5.8 into integer value 5 using the int() function.

    Example 3 (Explicit Typecasting from integer to string using str() function)
    a = 456
    b = str(a)    # result of b will be 456 but it will be treated as string.

    Here, you can see that we have converted the integer value 456 into string value "456" using the str() function.

    Example 4 (Explicit Typecasting from string to integer using int() function)
    a = "18"
    b = int(a)    # result of b will be 18 and it will be an integer type value.

    Here, you can see that we have converted the string value "18" into integer value 18 using the int() function.

    Example 5 (Explicit Typecasting from float to string using str() function)
    a = 24.19
    b = str(a)    # result of b will be 24.19 but it will be treated as string.

    Here, you can see that we have converted the float value 24.19 into string value "24.19" using the str() function.

    Example 6 (Explicit Typecasting from string to float using float() function)
    a = "57.49"
    b = float(a)    # result of b will be 57.49 and it will be a float type value.

    Here, you can see that we have converted the string value "57.49" into float value 57.49 using the float() function.

    Test Your Knowledge

    Attempt the questions in the exercise to check whether you have understood the lesson clearly.

    Exercise
    Test Your Knowledge
    Previous Next

    We care about your privacy

    Dremendo uses cookies and similar technologies that are necessary for the site to work properly, understand how you interact with our website, and measure the effectiveness of our marketing. By clicking Accept All, you consent to the use of non-essential cookies and similar technologies for analytics, advertising, and ad measurement, as described in our Privacy Policy.

    Cookie Preferences

    Choose which types of cookies Dremendo may use.

    Necessary Cookies

    Required for essential website functionality and security.

    Allow
    Analytics Cookies

    Help us understand how visitors use Dremendo.

    Allow
    Marketing Cookies

    Help us measure advertising performance and understand interactions with our advertisements.

    Allow
    Mentora Question Asked: 0 / 20

    Hi, I’m Mentora!

    I’m your personal AI tutor from Dremendo. Ask me anything, and I’ll help you understand it in a simple and easy way.

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.