Here, You can read basic concepts of machine learning and enhance your level manifolds.
Datatypes in Python

Data Types

Python is a dynamically typed programming language, which means that the data type of a variable is determined at runtime based on the value assigned to it. Here are some of the commonly used data types in Python:

  1. Integer: Integers are whole numbers without a fractional component. In Python, they are represented using the int keyword. Examples:

    x = 5
    y = -10
    z = 0

  2. Float: Floats are numbers that have a decimal component. In Python, they are represented using the float keyword. Examples:

    a = 45.14
    b = -4.5
    c = 0.0

  3. Boolean: Boolean values are used to represent the truth or falsehood of an expression. In Python, they are represented using the bool keyword. Examples:

    p = True
    q = False

  4. String: Strings are sequences of characters enclosed in single or double quotes. In Python, they are represented using the str keyword. Examples:

    name = 'Yuvi'
    msg = "Hello, World!"

  5. List: Lists are ordered collections of values enclosed in square brackets, separated by commas. In Python, they are represented using the list keyword. Examples:

    fruits = ['Banana', 'Apple', 'Cherry']
    numbers = [1, 2, 3, 4, 5]

  6. Tuple: Tuples are similar to lists, but they are immutable (cannot be changed once created) and are enclosed in parentheses instead of square brackets. In Python, they are represented using the tuple keyword. Examples:

    person = ('Ram', 28, 'Male')
    coordinates = (5.14, -3.5)

  7. Set: Sets are unordered collections of unique values enclosed in curly braces, separated by commas. In Python, they are represented using the set keyword. Examples:

    letters = {'a', 'b', 'c'}
    numbers = {1, 2, 3, 4, 5}

  8. Dictionary: Dictionaries are unordered collections of key-value pairs enclosed in curly braces, separated by commas. In Python, they are represented using the dict keyword. Examples:

    person = {'name': 'Ravi', 'age': 35, 'gender': 'Male'}
    grades = {'Suman': 90, 'Kamal': 45, 'Ramesh': 95}

  9. Arrays: An array is a collection of values of the same data type, stored in contiguous memory locations. In Python, arrays can be created using the array module. Here's an example:

    import array
    # Create an array of integers
    my_array = array.array('i', [1, 2, 3, 4, 5])
    # Access elements of the array
    print(my_array[0]) # prints 1
    print(my_array[2]) # prints 3

  10. Namedtuple: A namedtuple is a subclass of a tuple that allows fields to be accessed by name instead of index. In Python, namedtuples can be created using the collections module. Here's an example:

    import collections
    # Define a namedtuple class
    Person = collections.namedtuple('Person', ['name', 'age', 'gender'])
    # Create a Person object
    person = Person('Ravi', 25, 'Male')
    # Access fields by name
    print(person.name) # prints 'Ravi'
    print(person.age) # prints 25
    print(person.gender) # prints 'Male'

  11. Defaultdict: A defaultdict is a subclass of a dictionary that returns a default value when an unknown key is accessed. In Python, defaultdicts can be created using the collections module.

Advertisement

Advertisement