Data types in Python Programming language (Part 3/6)
Experience the Beauty of Python: The Easiest Language to Learn and Use
Lets start with a joke:
“Why did the Python programmer bring a ladder to the data type party? 🤔
Because they heard there was going to be a lot of ‘floats’ and they didn’t want to get ‘tripped’ up!” 😄🎉
Table of Content
- What is Data type?
- Data types in python
- Built-in data types in Python
What is Data type?
In programming languages, a data type is an attribute or characteristic that defines the type of data that a variable, expression, or value can hold. It specifies the nature of the data and the operations that can be performed on it.
Data types in python
In Python, there are two main categories of data types: built-in data types and custom data types.
- Built-in Data Types: Python provides several built-in data types that are readily available for use without requiring additional libraries or modules. These have defined structures, attributes, and behaviors specific to your problem domain. Some of the commonly used built-in data types in Python include:
- Numeric Types: int, float, complex
- Sequence Types: str, list, tuple
- Mapping Type: dict
- Set Types: set, frozenset
- Boolean Type: bool
- Other Type: None
2. Custom Data Types: Python also allows you to define your own data types using classes and objects, following the principles of object-oriented programming (OOP). With custom data types, you can define your own structures, attributes, and behaviors specific to your problem domain. You can create classes that encapsulate data and functions (methods) that operate on that data.
We will cover only built-in data types in python in this article. We will cover OOPS concepts in upcoming articles to understand custom data types
Built-in data types in Python
Let understand built-in data type with an example.
So lets takea = 10
b = 20
c = a + b
Here ‘a’ and ‘b’ are two ‘int’ data-type. We can do addition operation on them and assign the result to new ‘int’ data type called ‘c’. As ‘int’ is a build-in data type, so python already have some set of operations that can be perform on them(in this case addition and assignment).
In Python, there are several built-in data types that you can use to store and manipulate different kinds of data. Some of the commonly used data types in Python are:
1. Numeric Types:
- int: Integer values, such as 1, -5, or 1000.
- float: Floating-point values with decimal points, like 3.14 or -2.5.
- complex: Complex numbers in the form a + bj, where a and b are floats, and j represents the imaginary unit.
# int
x = 5
print(x) # Output: 5
# float
y = 3.14
print(y) # Output: 3.14
# complex
z = 2 + 3j
print(z) # Output: (2+3j)
2. Sequence Types:
- str: String of characters, such as “Hello, World!” or “Python”.
- list: Ordered collection of elements, enclosed in square brackets ([]), e.g., [1, 2, 3].
- tuple: Immutable ordered collection of elements, enclosed in parentheses (()), e.g., (1, 2, 3).
# str
message = "Hello, World!"
print(message) # Output: Hello, World!
# list
numbers = [1, 2, 3]
print(numbers) # Output: [1, 2, 3]
# tuple
coordinates = (4, 5)
print(coordinates) # Output: (4, 5)
3. Mapping Type:
- dict: Collection of key-value pairs, enclosed in curly braces ({}), e.g., {‘name’: ‘John’, ‘age’: 25}.
# dict
person = {'name': 'John', 'age': 25}
print(person) # Output: {'name': 'John', 'age': 25}pyt
4. Set Types:
- set: Unordered collection of unique elements, enclosed in curly braces ({}), e.g., {1, 2, 3}.
- frozenset: Immutable set, similar to a set but cannot be modified after creation.
# set
my_set = {1, 2, 3}
print(my_set) # Output: {1, 2, 3}
# frozenset
frozen = frozenset({4, 5, 6})
print(frozen) # Output: frozenset({4, 5, 6})
5. Boolean Type:
- bool: Represents truth values, either True or False.
# bool
is_true = True
is_false = False
print(is_true) # Output: True
print(is_false) # Output: False
6. Other Types:
- None: Represents the absence of a value or null.
# None
value = None
print(value) # Output: None
Lets end with a smile
Did you hear about the Python data type that wanted to become an actor? It tried out for every role: int, float, str, even bool! But no matter how hard it auditioned, it always ended up playing ‘None’ of the characters. 🎭😄
This is part 3, and there will be 3more part. This part was about data-types and built-in data-types in Python Programming Language. In part 4 we will learn about Python Conditional statements.
Link for Part 1: https://medium.com/@siddp6/python-programming-language-part-1-6-8b937f7297bf
Link for Part 2: https://siddp6.medium.com/python-programming-language-part-2-6-403dabaa7c6a
Link for Part 3: https://medium.com/@siddp6/python-programming-language-part-3-6-ab0af8000e27
Link for Part 4: https://medium.com/@siddp6/conditionals-and-loops-python-programming-language-part-4-6-b5b1a8c9521e
Link for Part 5: https://siddp6.medium.com/functions-in-python-programming-language-part-5-6-5c2c5b1df5fe
Link for Part 6: https://siddp6.medium.com/classes-and-object-oriented-programming-oop-in-python-programming-language-part-6-6-4e2fca5e1eb9
Copyright © 2023 Siddhartha Purwar. All rights reserved. Portions of this content were enhanced grammatically and refined with the assistance of ChatGPT, an AI language model by OpenAI.