Course Content
Python Indentation, Comments and Variables
0/2
Object Oriented Programming in Python
0/1
Exception Handling in Python
0/1
Sending emails with Python
0/1
Unit test in python programming
0/1
Python programming (zero to advance!!!)
About Lesson

Type casting in Python refers to the conversion of one data type to another. Python provides several built-in functions for type casting, allowing you to convert variables from one type to another when necessary. Here are the commonly used type casting functions in Python:

1. int(): Converts a value to an integer data type.

“`python
x = 10.5
y = int(x)
print(y) # Output: 10
“`

2. float(): Converts a value to a floating-point data type.

“`python
x = 10
y = float(x)
print(y) # Output: 10.0
“`

3. str(): Converts a value to a string data type.

“`python
x = 10
y = str(x)
print(y) # Output: ’10’
“`

4. bool(): Converts a value to a boolean data type.

“`python
x = 0
y = bool(x)
print(y) # Output: False
“`

5. list(): Converts a sequence (e.g., tuple, string, set) to a list.

“`python
x = (1, 2, 3)
y = list(x)
print(y) # Output: [1, 2, 3]
“`

6. tuple(): Converts a sequence (e.g., list, string, set) to a tuple.

“`python
x = [1, 2, 3]
y = tuple(x)
print(y) # Output: (1, 2, 3)
“`

7. set(): Converts a sequence (e.g., list, tuple, string) to a set.

“`python
x = [1, 2, 2, 3]
y = set(x)
print(y) # Output: {1, 2, 3}
“`

These type casting functions are handy when you need to convert data from one type to another to perform specific operations or meet certain requirements in your Python programs.

Join the conversation