Lesson #3: Data Types
There are various data types built-in by default in Python. Some of the important types are listed below.
Numeric Types: Python Numbers
There are three numeric types in Python. Integers, floating-point numbers, and complex numbers fall under the Python numbers category. They are defined as int
, float
, and complex
classes in Python.
Example
product_price = 29.99 # float
imaginary_part = 4j # complex
💡 Note: The type()
function is used to verify the type of any object in Python.
Int
Int, or integer, is a whole number, without decimals, and can be either positive or negative.
To define an integer, use the following syntax:
number_of_products = 3240
files_uploaded = 88430
y = -10
Float
Float or floating-number is a number, positive or negative, containing one or more decimals, and can be either positive or negative.
To define a float, use the following syntax:
temperature_in_celsius = 19.1
y = -10.65
Complex
In python, you can put ‘j’ or ‘J’ after a number to make it imaginary, so you can write complex literal easily.
To define a complex, use the following syntax:
b = 47j
c = 10 + 5j
Sequence Types: Python String, Python List, Python Tuple
Sequences allow storing multiple values in an organized and efficient way. The sequence types in Python are string
, list
, tuple
.
Example
fruits = [“kiwi”, “banana”, “cherries”] # List
cars = (“tesla”, “jeep”, “audi”) # Tuple
String
In python, a String is a sequence of characters. You can use single quotes or double quotes to represent strings. You can also assign a multi-line string to a variable by using three quotes:
To define a String, use the following syntax:
product_description = “100% cotton”
product_review = “Highly recommended”
List
In python, List is an ordered sequence of items. Lists are very similar to arrays. They can contain any type of variable and it is one of the most used data types.
💡 Note: All the items in a list do not need to be of the same type.
To define a List, use the following syntax:
programming_languages = [“python”, “javascript”, “java”]
cars = [“tesla”, “volvo”, “volkswagen”]
Tuple
In python, Tuple is an ordered sequence of items same as a list. The only difference is that the Tuples are immutable.
💡 Note: Tuples once created cannot be modified.
To define a Tuple, use the following syntax:
programming_languages = (“python”, “javascript”, “java”)
cars = (“tesla”, “volvo”, “volkswagen”)
Mapping Type: Python Dict
A dictionary is a data type similar to arrays, but works with keys and values instead of indexes. It is generally used when we have a huge amount of data. Dictionaries are optimized for retrieving data. We must know the key to retrieve the value.
💡 Note: A dictionary is a collection that is ordered, changeable, and does not allow duplicates.
To define a Dictionary, use the following syntax:
“name”: “Nick”,
“age”: 29,
“address”: “52 Hefner Acres”
}
Boolean Type: Python Bool
Booleans represent one of two values: True
or False
. You can evaluate any expression in Python, and get one of two answers, True
or False
.
💡 Note: True and False with capital ‘T’ and ‘F’ are valid booleans otherwise python will throw an error.
print( 10 == 9) # False
print(10 < 9) # False