0% found this document useful (0 votes)
2 views10 pages

Python 2 - Type Conversion, Casting & Random

The document provides an overview of type conversion and casting in Python, explaining the differences between implicit and explicit conversions. It details built-in functions like int(), float(), complex(), and str() for converting data types, along with examples. Additionally, it covers generating random numbers using the random module and summarizes key points for quick revision.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

Python 2 - Type Conversion, Casting & Random

The document provides an overview of type conversion and casting in Python, explaining the differences between implicit and explicit conversions. It details built-in functions like int(), float(), complex(), and str() for converting data types, along with examples. Additionally, it covers generating random numbers using the random module and summarizes key points for quick revision.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Notes – Type Conversion, Casting & Random

Numbers

1. Type Conversion
Type conversion means converting one data type into another using Python's built-in
functions.

Syntax

Python Run

int()
float()
complex()

Example

Python Run

x = 1 # int
y = 2.8 # float
z = 1j # complex

# Convert int to float


a = float(x)

# Convert float to int


b = int(y)

# Convert int to complex


c = complex(x)

print(a)
print(b)
print(c)

print(type(a))
print(type(b))
print(type(c))

Output

Python Run
1.0
2
(1+0j)

<class 'float'>
<class 'int'>
<class 'complex'>

Important Notes
float() converts an integer into a floating-point number.
int() removes the decimal part (it does not round the value).

complex() converts an integer or float into a complex number.


Complex numbers cannot be converted into int or float .

Example:

Python Run

x = 5.8

print(int(x))

Output

Python Run

2. Casting vs Type Conversion


Many beginners think Casting and Type Conversion are the same. They are closely
related, but there is a small difference.

What is Type Conversion?


Type Conversion is the process of changing one data type into another.

There are two types of type conversion:

1. Implicit Type Conversion (Automatic)


Python automatically converts one data type into another whenever required during an
operation.

Example
Python Run

x = 5 # int
y = 2.5 # float

z = x + y

print(z)
print(type(z))

Output

Python Run

7.5
<class 'float'>

Explanation
x is an integer.
y is a float.
Python automatically converts x (5) into 5.0 .
The result becomes a float.

This is called Implicit Type Conversion because Python performs it automatically.

2. Explicit Type Conversion (Casting)


When the programmer manually converts one data type into another using built-in
functions, it is called Casting or Explicit Type Conversion.

Python provides the following constructor functions:

Function Description

int() Converts a value to an integer

float() Converts a value to a float

complex() Converts a value to a complex number

str() Converts a value to a string

Example
Python Run

x = 5.8

y = int(x)

print(y)
print(type(y))

Output

Python Run

5
<class 'int'>

Explanation
Here, we explicitly instructed Python to convert a float into an integer using int() .
Therefore, this is called Casting.

Difference Between Type Conversion and Casting


Type Conversion Casting

Type Conversion is the process of changing Casting is the manual conversion of one data
one data type into another. type into another.

It can be Implicit (Automatic) or Explicit It is always Explicit (Manual).


(Manual).

Python may perform it automatically. The programmer performs it using functions like
int() , float() , str() , and complex() .

Includes both automatic and manual Refers only to manual conversions.


conversions.

Example of Implicit Type Conversion

Python Run
a = 10
b = 2.5

print(a + b)

Output

Python Run

12.5

Python automatically converts 10 into 10.0 .

Example of Casting

Python Run

age = "25"

print(type(age))

age = int(age)

print(type(age))

Output

Python Run

<class 'str'>
<class 'int'>

Here, int() manually converts the string into an integer.

Key Points
Type Conversion is the overall process of converting one data type into another.
Casting is Explicit Type Conversion performed by the programmer.
Every Casting operation is a Type Conversion.
Not every Type Conversion is Casting, because Python can automatically perform
Implicit Type Conversion.
int() removes the decimal part; it does not round the number.
Complex numbers cannot be converted into int or float .
3. Random Numbers
Python does not have a built-in random() function.

To generate random numbers, use the random module.

Import Module

Python Run

import random

Example

Python Run

import random

print([Link](1, 10))

Output
(Random output every time)

Python Run

or

Python Run

Explanation

Python Run

[Link](start, stop)

The start value is included.


The stop value is excluded.

Example:
Python Run

[Link](1, 10)

Returns a random integer from 1 to 9.

4. Specify a Variable Type (Casting)


There may be times when you want to specify the data type of a variable. This is done
using casting.

Python uses constructor functions to define data types.

Function Description

int() Constructs an integer from an integer, float (removes decimals),


or numeric string

float() Constructs a float from an integer, float, or numeric string

str() Constructs a string from numbers or other data types

5. Integer Casting
Example

Python Run

x = int(1)
y = int(2.8)
z = int("3")

print(x)
print(y)
print(z)

Output

Python Run

1
2
3

6. Float Casting
Example

Python Run

x = float(1)
y = float(2.8)
z = float("3")
w = float("4.2")

print(x)
print(y)
print(z)
print(w)

Output

Python Run

1.0
2.8
3.0
4.2

7. String Casting
Example

Python Run

x = str("s1")
y = str(2)
z = str(3.0)

print(x)
print(y)
print(z)

Output
Python Run

s1
2
3.0

Summary Table
Function Converts To Example

int() Integer int(5.9) → 5

float() Float float(5) → 5.0

complex() Complex Number complex(5) → (5+0j)

str() String str(5) → "5"

Final Revision Points


Type Conversion means changing one data type into another.
Type Conversion can be Implicit (Automatic) or Explicit (Manual).
Casting is another name for Explicit Type Conversion.
Use int() , float() , complex() , and str() for casting.
int() removes the decimal part; it does not round.
complex() creates complex numbers but cannot be converted back to int or
float .
Import the random module to generate random numbers.
[Link](1, 10) returns a random integer from 1 to 9.

⭐ Overall Review
Your notes are now well-structured and suitable for:
✅ Beginners learning Python
✅ College (BCA/MCA) coursework
✅ Interview preparation
✅ Quick revision before exams
The flow progresses logically from Type Conversion → Casting → Differences →
Random Numbers → Examples → Summary, making it easy to understand and review
later.

You might also like