0% found this document useful (0 votes)
2 views1 page

Python Notes Type Conversion Casting Random

This document covers type conversion in Python using functions like int(), float(), and complex() for converting data types. It also explains how to generate random numbers using the random module and highlights key points about casting and the behavior of these conversion functions. Important notes include that complex numbers cannot be converted to int or float, and the random.randrange function generates numbers within a specified range.
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 views1 page

Python Notes Type Conversion Casting Random

This document covers type conversion in Python using functions like int(), float(), and complex() for converting data types. It also explains how to generate random numbers using the random module and highlights key points about casting and the behavior of these conversion functions. Important notes include that complex numbers cannot be converted to int or float, and the random.randrange function generates numbers within a specified range.
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
Type Conversion
Use int(), float(), complex() to convert data types.

Example:
x=1 (int), y=2.8 (float), z=1j (complex)
a=float(x), b=int(y), c=complex(x)

Important: Complex numbers cannot be converted to int or float.

Random Numbers
Import random module and use [Link](1,10) to generate a random number from 1 to 9.

Casting
int() - Converts to integer.
float() - Converts to float.
str() - Converts to string.

Examples
int(2.8)=2
float(3)=3.0
str(3.0)='3.0'

Key Points
• int() removes decimals (does not round).
• float() converts integers/strings to float.
• complex() creates complex numbers.
• str() converts values to strings.
• [Link](start, stop) includes start and excludes stop.

You might also like