Python for Kids

Typecasting & Type Function

1. What is Typecasting?

  • Typecasting means converting one data type into another
  • Functions used:
    • int()
    • float()
    • str()
    • bool()

2. Checking Data Type

  • Use type()

Example:

print(type(age))

It will give you the type of the variable AGE which is an integer.

3. Converting Types

Float → Integer:

height = int(5.5)

Integer → Float:

age = float(25)

Integer → String:

age = str(age)

4. Boolean Conversion

  • Non-empty values → True
  • Empty values → False

5. Summary

  • Checking type
  • Converting data types
  • Importance of typecasting

6. Task

  • Convert int → float
  • Convert float → int
  • Convert int → string