In this article, we will go over the essential data types in Python
. Before we get started, if you don’t know how to write comments, please go through the following article: Write Comments in Python.
In Python, variables are objects, data types are the classes, and these variables are the instances of these classes. There are different kinds of data types in Python that specify what kind of values can be assigned to a variable. We don’t need to declare the datatypes while assigning variables, Python being a dynamic programming language, will do that automatically
Text Type
String (str
)
Definition: A string is identified as a continuous set of characters wrapped in quotation marks. Python allows either a pair of single or double quotes. Strings are an immutable sequence data type, i.e., each time one makes any changes to a string, an entirely new string object is created.
You can either go for single quotes or double quotes, but you should be consistent with it in the entire program.
1 2 3 4 5 |
name = "Alice" message = 'Hello, World!' print(type(name)) # <class 'str'> print(name.upper()) # ALICE |
You can also perform various actions on Strings
1 2 3 4 |
>>> print(name[0]) # to print the first letter A >>> print(message[0:5]) # to print first five letters Hello |
Numeric Types
In Python, there are 3 numeric data types – Integer, Float, and Complex. Integer and Float being the most used datatype.
Integers (int
)
Definition: Any whole number can be classified as an Integer; sometimes it is addressed as an int. There is no limit on how long the integer value can be.
1 2 3 4 5 |
x = 100 y = -42 z = 123456789012345678901234567890 print(type(x)) # <class 'int'> |
Float (float
)
Definition: Any real number along with a decimal can be classified as a Float number. 12 is an integer, but 12.0 is a float.
1 2 3 4 5 |
x = 12.0 # This is a float, not an int y = -3.14 z = 0.00001 print(type(y)) # <class 'float'> |
Complex (complex
)
Definition: Complex numbers are written in the form x + yj, where x is the real part and y is the imaginary part.
We don’t need to declare the datatypes while assigning variables, Python being a dynamic language, will do that automatically. When you enter a number without a decimal, Python will treat it as an integer, and when you enter a number with a decimal, then Python will treat it as a floating number. We can assign numbers to variables directly,
1 2 3 4 |
z = 5 + 3j print(z.real) # 5.0 print(z.imag) # 3.0 print(type(z)) # <class 'complex'> |
Boolean Type
Bool (bool
)
Definition: A boolean data type can have one of the following two values: True or False. We can get Boolean (bool) output on basic math operations:
1 2 3 4 |
>>> 6 > 9 False >>> 6 < 9 True |
Sequence Types
List (list
)
Definition: A list contains items separated by commas and enclosed within square brackets []. Lists are almost similar to arrays in C. One difference is that all the items belonging to a list can be of different data types.
Each value inside a list is called an item, Lists are a very flexible data type because they are mutable, which means items can be added, deleted, or updated in a list. We can make a list of similar data types,
1 2 3 |
int_list = [ 1, 2, 3, 4] string_list = [ "this" , "is" , "a", "list" ] float_list = [ 0.1, 0.2, 0.3, 0.4] |
We can print them out by calling the print function. Additionally, we can also make a list of different data types.
1 2 3 |
>>> list = [12345, "some_text", 123.456, "moretext"] >>> print(list) [12345, 'some_text', 123.456, 'moretext'] |
Tuple (tuple
)
Definition: Similar to lists, tuples are also used to group data of the same or different data types, but they immutable ordered sequence of items, i.e., like a list, we can’t update or delete items of a tuple. Tuples are enclosed in parentheses (). A tuple looks like:
1 2 3 |
>>> my_tuple = (2, 2.8, "sometext") >>> print(my_tuple) (2, 2.8, 'sometext') |
Range (range
)
Definition: A sequence of numbers generated from a start to an end (not including the end). Commonly used in loops.
1 2 |
for i in range(1, 4): print(i) # 1 2 3 |
Mapping Type
Dictionary (dict
)
Definition: A Dictionary is an unordered pair of keys and values. It is used to store related data. The dictionary is heavily optimized for storing data and retrieving it from keys. It is enclosed in curly braces {}, and values can be assigned and accessed using square brackets[]. A dictionary looks like this :
1 |
user = {'email' : 'test@gmail.com'} |
Apart from curly braces, there is a colon (:) in the dictionary. The word to the left of the colon is Key, and the word to the right is the Value of that key. We can use the key to retrieve the data, but not the other way around. To retrieve data from the dictionary, we need to pass the key in square brackets[] like this,
1 2 |
>>> print(user['email']) test@gmail.com |
Don’t forget to wrap the keys in either single or double quotes. A large dictionary can look something like this:
1 |
user = { 'email' : 'test@gamil.com', 'password' : 'somepass', 'country' : 'Bangladesh', 'name' : 'Ahmed' } |
Each key-value pair is separated by a comma(,). We can retrieve all the keys and values like this:
1 2 3 4 5 |
>>> print(user.keys()) dict_keys(['email', 'password', 'country', 'name']) >>> print(user.values()) dict_values(['test@gamil.com', 'somepass', 'Bangladesh', 'Ahmed']) |
To Print specific value we need to pass the key in square brackets like this:
1 2 |
>>> print(user['country']) Bangladesh |
Set Types
set
Definition: Unordered collection of unique items.
1 2 3 |
colors = {"red", "green", "blue"} colors.add("yellow") print("red" in colors) # True |
Duplicates are removed automatically:
1 2 |
s = {1, 2, 2, 3} print(s) # {1, 2, 3} |
frozenset
Definition: Immutable version of set.
1 2 |
immutable_set = frozenset([1, 2, 3]) # immutable_set.add(4) # This will raise an AttributeError |
Binary Types
bytes
Definition: An immutable sequence of bytes (8-bit values from 0 to 255). Used when you need an unchangeable sequence of binary data—such as reading data from a file or sending over a network.
1 2 3 4 |
b = bytes([65, 66, 67]) # ASCII for 'A', 'B', 'C' print(b) # b'ABC' print(b[0]) # 65 print(type(b)) # <class 'bytes'> |
bytearray
Definition:A mutable sequence of bytes. You can modify its content after creation. Ideal when you need to change individual bytes after creation (e.g., working with a mutable buffer or image processing).
1 2 3 4 5 |
ba = bytearray([65, 66, 67]) ba[0] = 68 # Change 'A' to 'D' print(ba) # bytearray(b'DBC') print(ba.decode()) # DBC print(type(ba)) # <class 'bytearray'> |
memoryview
Definition:A memory-efficient view object that allows you to access the internal data of binary objects (like bytes, bytearray) without copying. Useful when working with large datasets or for high-performance computing where copying data is expensive.
1 2 3 4 5 6 |
data = bytearray("hello", "utf-8") mv = memoryview(data) print(mv[0]) # 104 (ASCII value of 'h') mv[0] = 72 # Change 'h' to 'H' print(data) # bytearray(b'Hello') print(mv.tobytes()) # b'Hello' |
None Type
NoneType
Definition: Represents the absence of a value or a null value. Use Case: To initialize variables before assignment, To signal “no result” or “empty” return, As a default value for function arguments
1 2 3 4 5 6 7 8 |
def greet(name=None): if name is None: print("Hello, Guest!") else: print(f"Hello, {name}!") greet() # Hello, Guest! greet("Alice") # Hello, Alice! |
Next Recommended Article: How To Use String Formatting In Python
Leave a Comment