1.1.3 Type Conversion (int() / float() / str())
Type conversion allows you to change a value from one data type to another.
This is useful when working with user input, calculations, and formatted output.
Common Conversion Functions
Python provides built-in functions for type conversion:
int()→ converts a value to an integerfloat()→ converts a value to a floating-point numberstr()→ converts a value to a string
When Type Conversion Is Needed
Type conversion is commonly required when:
- Reading input from users (input is always a string)
- Combining numbers and text
- Ensuring correct calculation behavior
Conversion Rules and Errors
- Not all strings can be converted into numbers
- Converting a float to an integer removes the decimal part
- Invalid conversions raise errors
Understanding these rules helps prevent unexpected crashes and logic bugs.
📌 Note:
When a program behaves unexpectedly, checking and converting data types is often the first step in debugging.