Data Types & Variables


Data types define the kind of data a variable can hold, and variables are names assigned to store data in memory. Python is dynamically typed, meaning you don’t explicitly declare types, and types are inferred at runtime.

The following are the most commonly used data types in Python:

  1. Integer: An integer is a built-in data type used to represent whole number (positive, negative, or zero) without a fractional or decimal component.
  2. Float: A float is a number that has a decimal point or is represented in exponential notation.
  3. String: A string is a sequence of characters enclosed in single ('), double ("), or triple (''' ''') quotes. (e.g. 'Hello', "123", '''Python''')
  4. List: A list is an ordered, mutable (changeable) collection of elements. It can contain different data types like integers, floats, strings, or even other lists.
  5. Boolean: A Boolean is a data type that represents True or False values. It is commonly used in logical operations, conditions, and comparisons.
  6. Tuple: A tuple is an ordered, immutable (unchangeable) collection of elements. It is similar to a list but cannot be modified after creation.
  7. Dictionary: A dictionary is an unordered, mutable collection of key-value pairs. It allows fast lookups and modifications using keys.
  8. Set: A set is an unordered, mutable, and unique collection of elements. It does not allow duplicate values and is optimized for fast lookups.
  9. Variable:A variable is a named storage that holds a value. Python automatically determines the data type based on the assigned value.
    
    
        #Integer 
        x = 30 #positive integer
        y = -25 #negative integer
        z = 0   # Zero
        print(type(x))  #### Checking Type of an Integer
        print(int(3.7))    ### Converting other types to integer (float to int(3))

        #Float 
        x = 3.14   # Positive float
        y = -0.75  # Negative float
        z = 0.0    # Zero as float
        print(float(3))         #Converts float to float(3.0) 
        print(float("30.1"))    #Converts string to float(30.1) 

        # String
        greeting = "Hi there"

        # List
        fruits = ["apple", "banana", "orange"]   # List of strings
        list_1 = [1, 2, 3, 4, 5]                   # List of integers
        mixed_list = [1, "Hello", 3.14, True]    # List with different data types 

        # Tuple
        tuple_1 = (1, 2, 3, 4, 5)          # Tuple of integers
        mixed_tuple = (1, "Hello", 3.14)    # Tuple with mixed data types

        # Dictionary
        person = {"name": "John", "age": 30}

        # Set
        unique_nums = {1, 2, 3, 3}  # Duplicates ignored, result: {1, 2, 3}

        # Boolean
        print(10 > 5)    # True

        #Variable
        name = "Alice"       # String
        age = 25            # Integer
        height = 5.7        # Float
        is_student = True   # Boolean