Converting between data types in python

language-essentials

Description

Type Casting

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

Type hinting

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.

Type Checking

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

Usage

In this repo you can see the demo code and actually run it by running python type_casting.py or python3 type_casting.py.

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.

Files

type_casting_demo.py
""" Description: A demo of type casting in python 3. The preferred method of type casting is to use the type hint\ as a function around the variable you want to cast """ __author__ = "Canadian Coding" my_string = "1" print(type(my_string) == str) # returns True # Convert my_string to an int my_string = int(my_string) print(type(my_string) == int) # returns True # When something cannot be type casted it will raise an exception my_string = list(my_string) # raises TypeError # An example where the error is caught my_string = "hello" try: my_string = int(my_string) # raises value error except ValueError: print("HEY, {} can't be made into an int".format(my_string))