Class 12 : Review Of Python Basics

Home Page class 12 @ Python

Class 12 : Python

Python Basics
Data Types
String
List
Tuple
Dictionary
Flow of Execution
Mutable and Immutable Types
Type Casting

Class 12 : Python Revision MCQs

Revision Tour MCQs

Class 12 | Revision Tour 12

By: Archana Shukla and Rajesh Shukla

Type Casting(conversion)

We can convert from one data type to another. Such conversions can happen in two ways. Explicit (forced) and implicit.

Explicit conversion

This type of conversions happen when the user or the programmer forces it in the program. there is a risk of data loss in this type of conversion as we are forcing the expression to be of a specific type.

Example

x = 3 # int
y = 4.5 # float
z = 5j # complex
#convert from int to float:
a = float(x)
#convert from float to int:
b = int(y)
#convert from int to complex:
c = complex(x)
print(a)
print(b)
print(c)
print(type(a))
print(type(b))
print(type(c))

output :
3.0
4
(3+0j)
<class ‘float’>
<class ‘int’>
<class ‘complex’>
>>>

Implicit conversion

These are also known as coercion. This conversions are done automatically by the python interpreter on the basis of requirement.

Example

#implicit conversion from int to float
n1=10
n2=55.0
sum1=n1+n2
print(sum1)
print(type(sum1))

output:
65.0
<class ‘float’>
>>>

Number Of Visitors

0270450