Converting between data types in python
When you convert a variable/object to another type. For example:
my_string = "0" # my_string is a str type type(my_string) == str # Returns True since my_string is a str type(my_string) == int # Returns False since my_string is a str my_string = int(my_string) # type casting my_string to an int type(my_string) == str # Returns False since my_string is now an int type(my_string) == int # Returns True since my_string is now an int
Is the mechanism in python that allows you to know what type a variable is. Each created object in python has a type including the builtin types like dict and list. See this cheatsheet as reference for full details here.
Is a method of validating that a variable is of a certain expected type. There are multiple ways of doing this. For Example:
my_list = ["hello"] # Returns <class 'list'> type(my_list) == list # Returns True
Some types require a more exotic form of type checking such as generators, to type check generators for example:
import types def yield_numbers(amount = 10): for i in range(amount): yield i isinstance(yield_numbers(), types.GeneratorType) # Returns True
In this repo you can see the demo code and actually run it by running
python type_casting.py or
Real World Applications
KEEP IN MIND type casting is expensive in terms of computation so avoid using it as much as possible. If a process can be done with the type already provided avoid doing the unnecessary type casting.