Data Types
Types communicate the behavior and characteristics of an object in Python. The type provides information to the interpreter on the values the object is allowed to possess, and different operations that can be done with that specific object type. Python is dynamically typed, where it is not necessary to explicitly define the type of variable. Dynamically typed refers to runtime values having a type, and that the type of variable is able to change during that runtime. In other programming languages that are statically typed, there is a strong emphasis placed on the type of value assigned to a defined variable.
In Python, there are 5 primitive types used to represent values: integers, floating-point numbers, booleans, strings, and None for specifying a lack of value. Each of the primitive types possesses different attributes providing unique functionality. Primitive types are predefined objects in the language, that all other additional types are constructed of. For example, in the Chinese language, certain sentences will be constructed depending on the specific combination of characters used. A type may be explicitly defined as well:
explicitly_defined_type: int = 1
print(explicitly_defined_type)
>>> 1
Python possesses support for multiple additional types, on top of the 5 primitive types, and utilities to perform operations and manipulate that object data. A table of all the types in Python is shown below.
Types
Python provides a collection of primitive data types, documented in the Table below. The first column provides the common name of the basic data type, the second column provides the syntactic type in Python, the third column lists whether the type of data object is mutable or not, and the fourth column gives examples of each of the types:
You are also able to create your own Data Types, other than the basic ones provided by Python.
Using the Type() Function
Every value data object in Python has a type that you can check with the predefined Python function, type()
. If you are ever unsure of what the type of a variable or value is, just use this keyword to check:
print(type('Hello World'))
print(type(1))
print(type(1.0))
print(type(True))
>>> <class 'str'>
>>> <class 'int'>
>>> <class 'float'>
>>> <class 'bool'>
The output, <class ‘type’>
, represents the category of the type that the value is associated with. It is common to check the type of a variable or value when you are dealing with large codebases or programs as they contain many different defined variables.
Integers
In Python, objects that are integers, type int
, represent positive and negative real whole numbers. For example, integers consist of digits such as -1, 0, 1, 100, -1000
:
print(type(-1))
print(type(0))
print(type(1))
print(type(100))
print(type(-1000))
>>> <class 'int'>
>>> <class 'int'>
>>> <class 'int'>
>>> <class 'int'>
>>> <class 'int'>
In Python, larger integers, 10100
, cannot be separated by commas like they are in mathematics, 10,100. Commas in between numbers, 10,100
, signify to Python that the data object is a tuple while underscores can be used to represent large integers 10_100
. Underscores are only used for code readability:
tuple_with_commas = 10,100
integer_with_underscores = 10_100
print(tuple_with_commas)
print(type(tuple_with_commas))
print(integer_with_underscores)
print(type(integer_with_underscores))
>>> (10, 100)
>>> <class 'tuple'>
>>> 10100
>>> <class 'int'>
Integer values cannot have a 0
followed directly by another digit, 5
, without throwing an error:
print(01)
>>> SyntaxError: leading zeros in decimal integer literals are not >>> permitted; use an 0o prefix for octal integers
You can specify a negative integer by placing a -
in front of a digit, 1
:
print(-1)
>>> -1
Placing a +
in front of a digit, 1
, results in the same positive integer as just using the digit itself without a sign:
print(+1)
>>> 1
You can perform multiple different mathematical operations on integers such as adding, subtracting, multiplying, etc.:
int_one = 1
int_two = 2
add_two_ints = int_one + int_two
multiply_two_ints = int_one * int_two
subtract_two_ints = int_one - int_two
print(add_two_ints)
print(type(add_two_ints))
print(multiply_two_ints)
print(type(multiply_two_ints))
print(subtract_two_ints)
print(type(subtract_two_ints))
>>> 3
>>> <class 'int'>
>>> 2
>>> <class 'int'>
>>> -1
>>> <class 'int'>
More operations will be covered in the next part. Depending on the value of an integer, it will occupy varying amounts of memory starting at 24 bytes.
Floating point numbers
In Python, objects that are floating-point numbers, type float
, have values referencing positive and negative decimal numbers, and rational numbers. Examples of floats are 1.0
, -5.0
, and 1.111111
.
float_one = 1.0
float_two = -5.0
float_three = 1.111111
float_one_type = type(float_one)
float_two_type = type(float_two)
float_three_type = type(float_three)
print(float_one_type)
print(float_two_type)
print(float_three_type)
>>> <class 'float'>
>>> <class 'float'>
>>> <class 'float'>
If you were to divide two integers 1 and 1, would you expect to get the integer 1? In Python, when two integers are divided you are left with a float instead.
int_one = 1
int_two = 1
divide_two_ints = int_one / int_two
print(divide_two_ints)
>>> 1.0
Dividing an integer 1
by a float 1.0
, would get you the same resulting type of float:
int_one = 1
float_one = 1.0
divide_int_by_float = int_one / float_one
print(divide_int_by_float)
print(type(divide_int_by_float))
>>> 1.0
>>> <class 'float'>
The variable int_one
represents a different data object type from variables float_one
and divide_int_by_float
. Almost any calculation or operation that can be performed on an integer can be done on a float as well:
int_one = 2
int_two = 2
float_one = 2.0
float_two = 2.0
add_two_ints = int_one + int_two
add_two_floats = float_one + float_two
print(add_two_ints)
print(add_two_floats)
>>> 4
>>> 4.0
In Python, floats occupy 24 bytes of memory consisting of the underlying double-precision value, pointer to the object type, and a reference count. Floats allow you to assign variables precise values with high decimal places:
high_decimal_value = 3.141592653589793
print(high_decimal_value)
>>> 3.141592653589793
Complex Numbers
In Python, complex numbers are a data type, consisting of real and imaginary numbers, and occur when finding the square root of a negative number. Python follows the electrical engineering convention using a j
to denote complex numbers instead of the i
which is commonly used in mathematics. Complex numbers are represented in the notation, a + bj
. The square root of -1
would be represented as 1j
in Python. You can check that the type of the complex number 1 +
1j
:
a = 1
b = 1j
print(a + b)
print(type(a + b))
>>> (1+1j)
>>> <class 'complex'>
Python also has a built-in function used for implementing complex numbers, complex()
. The complex()
function takes two numbers, one real and one imaginary, and outputs a complex number:
a = 1
b = 3
complex_number = complex(a, b)
print(complex_number)
print(type(complex_number))
>>> (1+3j)
>>> <class 'complex'>
Complex numbers in Python are represented by 32 bytes of memory.
Booleans
In Python, the Boolean (bool
) data type acts as a logical operator representing two values, either True
or False
. The first letter, T
or F
, is always capitalized. Check the types of True
and False
:
print(type(True))
print(type(False))
>>> <class 'bool'>
>>> <class 'bool'>
Boolean data objects are commonly used for comparisons, or if the logic of an expression is sound. Check if one number, 2, is larger than another number, 1. Evaluates to True
:
first_number = 2
second_number = 1
greater_than = first_number > second_number
print(greater_than)
print(type(greater_than))
>>> True
>>> <class 'bool'>
Evaluates to False
:
first_number = 1
second_number = 2
greater_than = first_number > second_number
print(greater_than)
print(type(greater_than))
>>> False
>>> <class 'bool'>
Strings
In Python, the String (str
) data type consists of a sequence of characters enclosed within either single, ‘’
, or double, “”
, quotation marks:
string_with_single_quotes = 'Single quotes'
string_with_double_quotes = "Double quotes"
print(string_with_single_quotes)
print(string_with_double_quotes)
>>> Single quotes
>>> Double quotes
Quotation marks need to be consistent and single and double quotes cannot be used in conjunction together to form a string. For example, a string cannot be formed using ‘my_string”
or “my_string’
without throwing a syntax error:
mixed_quotes_one = 'my_string"
mixed_quotes_two = "my_string'
print(mixed_quotes_one)
print(mixed_quotes_two)
>>> SyntaxError: EOL while scanning string literal
Although, single and double quotes can be used inside one another:
single_inside_double = "'Single quotes inside double quotes'"
double_inside_single = '"Double quotes inside single quotes"'
print(single_inside_double)
print(double_inside_single)
>>> 'Single quotes inside double quotes'
>>> "Double quotes inside single quotes"
Strings can be only a single keyboard character:
single_character = 's'
print(single_character)
>>> s
Consist of multiple different words forming a sentence:
multiple_words = 'This string has many different words.'
print(multiple_words)
>>> This string has many different words.
Or even be just an empty blank space:
empty_string = ''
print(empty_string)
>>>
In First Principles Python Part 1, you already printed a string, ‘Hello World’
:
print('Hello World')
>>> Hello World
Many different manipulations and operations can be performed on strings and will be covered in a later part.
NoneType
In Python, you can assign the special value of None
, that has type NoneType
, to a variable if you want to be left empty. Check the type of None
:
print(type(None))
>>> <class 'NoneType'>
Assign None
to a variable, non_type
, and print out the value and type to the terminal:
none_type = None
print(none_type)
print(type(none_type))
>>> None
>>> <class 'NoneType'>
Type Conversion
You can change the type of, or typecast, a data object value by using a type keyword function such as int()
, bool()
, float()
, str()
, etc., since python is dynamically typed. In other programming languages that are statically typed, the variable cannot change its type once it is defined.
Using int() to convert the type of a floating-point number will truncate the decimal point and keep the first digit whole number:
float_to_int_1 = int(1.9)
float_to_int_2 = int(100.1)
float_to_int_3 = int(3.14)
float_to_int_4 = int(1.0e2)
print(float_to_int_1)
print(type(float_to_int_1))
print(float_to_int_2)
print(type(float_to_int_2))
print(float_to_int_3)
print(type(float_to_int_3))
print(float_to_int_4)
print(type(float_to_int_4))
>>> 1
>>> <class 'int'>
>>> 100
>>> <class 'int'>
>>> 3
>>> <class 'int'>
>>> 100
>>> <class 'int'>
Python also has automatic type conversion when mixing different numeric types of values. When combining a float and an int, Python will not truncate the value in order to retain information that could possibly be lost:
a = 1
b = 1.1
type_conversion_addition = a + b
type_conversion_subtraction = a - b
print(type_conversion_addition)
print(type(type_conversion_addition))
print(type_conversion_subtraction)
print(type(type_conversion_subtraction))
>>> 2.1
>>> <class 'float'>
>>> -0.10000000000000009
>>> <class 'float'>
Converting an int to float will expand the whole number by adding a .0 to the digit:
int_to_float_1 = float(1)
int_to_float_2 = float(150)
print(int_to_float_1)
print(type(int_to_float_1))
print(int_to_float_2)
print(type(int_to_float_2))
>>> 1.0
>>> <class 'float'>
>>> 150.0
>>> <class 'float'>
Converting an int to int, or a type to itself, will not change the type, or output:
int_to_int = int(1)
float_to_float = float(1.0)
print(int_to_int)
print(type(int_to_int))
print(float_to_float)
print(type(float_to_float))
>>> 1
>>> <class 'int'>
>>> 1.0
>>> <class 'float'>
You can convert a float to a string, str(1.1):
convert_float_to_string = str(1.1)
print(convert_float_to_string)
print(type(convert_float_to_string))
>>> 1.1
>>> <class 'str'>
But you will get an error converting a string containing decimals:
convert_string_with_decimals = int('1.1')
print(convert_string_with_decimals)
>>> convert_string_with_decimals = int('1.1')
>>> ValueError: invalid literal for int() with base 10: '1.1'
Or an exponent:
convert_string_with_exponent = int('1.0e2')
print(convert_string_with_exponent)
>>> convert_string_with_exponent = int('1.0e2')
>>> ValueError: invalid literal for int() with base 10: '1.0e2'
Booleans that are cast to ints will evaluate to 1 for True and 0 for False:
true_bool_to_int = int(True)
false_bool_to_int = int(False)
print(true_bool_to_int)
print(type(true_bool_to_int))
print(false_bool_to_int)
print(type(false_bool_to_int))
>>> 1
>>> <class 'int'>
>>> 0
>>> <class 'int'>
And typecasting an integer 1 or 0 to a boolean will result in the same reciprocal values of True and False:
int_to_true_bool = bool(1)
int_to_false_bool = bool(0)
print(int_to_true_bool)
print(type(int_to_true_bool))
print(int_to_false_bool)
print(type(int_to_false_bool))
>>> True
>>> <class 'bool'>
>>> False
>>> <class 'bool'>
Typecasting a float, 1.0 or 0.0, to a boolean results in a similar output as an int:
float_to_true_bool = bool(1.0)
float_to_false_bool = bool(0.0)
print(float_to_true_bool)
print(type(float_to_true_bool))
print(float_to_false_bool)
print(type(float_to_false_bool))
>>> True
>>> <class 'bool'>
>>> False
>>> <class 'bool'>
You can convert strings representing whole digit numbers to integers:
str_to_int_1 = int('1')
str_to_int_2 = int('-1')
str_to_int_3 = int('1_000')
print(str_to_int_1)
print(type(str_to_int_1))
print(str_to_int_2)
print(type(str_to_int_2))
print(str_to_int_3)
print(type(str_to_int_3))
>>>1
>>> <class 'int'>
>>> -1
>>> <class 'int'>
>>> 1000
>>> <class 'int'>
You will get an error if you try to convert the type of an empty string to int()
:
empty_string_conversion = int('')
int_and_string_conversion = int('1 and a string does not work')
print(empty_string_conversion)
print(int_and_string_conversion)
>>> empty_string_conversion = int('')
>>> ValueError: invalid literal for int() with base 10: ''
Or a non-digit character, int(‘x’)
:
convert_non_digit = int('x')
print(convert_non_digit)
>>> convert_non_digit = int('x')
>>> ValueError: invalid literal for int() with base 10: 'x'
Or an int followed by numerous different characters:
# empty_string_conversion = int('')
int_and_string_conversion = int('1 and a string does not work')
# print(empty_string_conversion)
print(int_and_string_conversion)
>>> int_and_string_conversion = int('1 and a string does not work')
>>> ValueError: invalid literal for int() with base 10: '1 and a >>> string does not work'
Next Part
Throughout the next part and its sections, more beginner and core programming concepts will be covered. The next topic will pertain to arithmetic and operators, and will be discussed in-depth with plenty of code examples and explanations.
Previous Parts
First Principles Python Part 0
A brief introduction to one of the most popular programming languages in the world, Python, can be found here.
First Principles Python Part 1
Create a Python program, write code in a .py file, successfully compile a program without error, and output the text “Hello, World!” to the terminal here.
First Principles Python Part 2
Learn how to create variables with meaningful names and proper syntax here.
If you found this article to be useful please share it with your peers.
Or follow me on social media: