GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.dremendo.com — Typecasting in Python Programming Language | Dremendo
?>
Announcements
No new announcements.
Python Programming Tutorial
Table of Contents
Typecasting in Python Programming LanguageIn 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 TypecastingIn Python program the process of changing one data type into another data type is known as Typecasting. There are two types of typecasting:
Video Explanation
Click the button below to watch the explanation.
Please wait while the video is loading....
Implicit TypecastingImplicit 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
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 TypecastingExplicit 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
Example 1 (Explicit Typecasting from integer to
float using float() function)
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)
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)
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)
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)
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)
Here, you can see that we have converted the string value "57.49" into float value 57.49 using the float() function. Test Your KnowledgeAttempt the questions in the exercise to check whether you have understood the lesson clearly.
We care about your privacyDremendo 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 PreferencesChoose which types of cookies Dremendo may use.
Necessary Cookies
Allow
Required for essential website functionality and security.
Analytics Cookies
Allow
Help us understand how visitors use Dremendo.
Marketing Cookies
Allow
Help us measure advertising performance and understand interactions with our advertisements.
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. |