Overview of Python Data Types

Numeric data types with example 

Numeric data types in Python include:

int (integer): represents whole numbers, e.g.

x = 42

y = -100

float (floating point real numbers): represents fractional numbers, e.g.

x = 3.14

y = -0.9

complex (complex numbers): represents numbers with real and imaginary parts, e.g.

x = 3 + 4j

y = 5j


Sequence data types with example

Sequence data types in Python include:

str (string): represents a sequence of characters, e.g.

x = "Hello, World!"

y = "John Doe"

list: represents an ordered, mutable sequence of elements of any data type, e.g.

x = [1, 2, 3, "apple", "banana"]

y = [True, False, 42, 3.14, "Hello"]

tuple: represents an ordered, immutable sequence of elements of any data type, e.g.

x = (1, 2, 3, "apple", "banana")

y = (True, False, 42, 3.14, "Hello")


Mapping data type with example

The mapping data type in Python is the dict (dictionary) type, which represents an unordered, mutable collection of key-value pairs. An example of a dictionary in Python is:

x = {"name": "John Doe", "age": 30, "email": "johndoe@example.com"}

y = {1: "one", 2: "two", 3: "three"}

In the first example, "name", "age", and "email" are the keys, and "John Doe", 30, and "johndoe@example.com" are the corresponding values. In the second example, 1, 2, and 3 are the keys, and "one", "two", and "three" are the corresponding values.


Set data type with example

The set data type in Python is the set type, which represents an unordered, mutable collection of unique elements of any data type. An example of a set in Python is:

x = {1, 2, 3, "apple", "banana"}

y = {True, False, 42, 3.14, "Hello"}

Note that sets do not allow duplicates, so if you try to add an element that already exists in the set, it will not be added again. Also, sets are unordered, so the elements have no index and cannot be accessed by indexing.


Boolean data type with example

The Boolean data type in Python is represented by the bool type, which can have two values: True or False. Examples of Boolean values in Python are:

x = True

y = False

Boolean values are often used to represent the truth value of expressions and conditions in control flow statements like if and while.


Binary data type with example

Binary data in Python is represented using the bytes type or the bytearray type.

bytes is an immutable sequence of bytes, and bytearray is a mutable sequence of bytes.

An example of creating a bytes object is:

x = b'\x00\x01\x02\x03\x04'

An example of creating a bytearray object is:

x = bytearray(b'\x00\x01\x02\x03\x04')

Note the use of the b prefix in front of the string literal to indicate that it is a sequence of bytes, not a string.


User-defined data types with example

User-defined data types in Python are created using classes. A class is a blueprint for creating objects, and each object created from a class is an instance of that class.

Here is an example of a simple class in Python:

class Person:

    def __init__(self, name, age):

        self.name = name

        self.age = age


    def say_hello(self):

        print("Hello, my name is " + self.name)


p = Person("John Doe", 30)

p.say_hello() # prints "Hello, my name is John Doe"

In this example, the Person class has two attributes name and age, and one method say_hello. The __init__ method is a special method that is called when an object is created from the class, and is used to initialize the attributes of the object. The self parameter is a reference to the current object, and is used to access its attributes and methods.

More :- Python

Next Post Previous Post
No Comment
Add Comment
comment url