Python lab

Conditional and Looping Programs

                        
n = int(input("Enter last number : "))
i = 1
while(i<=n):
    print(i)
    i+=1
                        
                
                        
num = int(input("Enter a number : "))
if(num%2==0):
    print("Entered number is even.")
else:
    print("Entered number is odd.")

# Using a function 
def checkNum(a):
    if a%2==0:
        print("Number is even")
    else:
        print("Number is odd")
        
n = int(input("Enter a number : "))
checkNum(n)
                        
                    
                        
for i in range(2,50,2):
    print(i)
                        
                
                        
n = int(input("Enter a natural number : "))
i = 1
sum = 0
while(i<=n):
 sum+=i
 i=i+1
print("The sum is = ", str(sum))
                        
                
                        
n = int(input("Enter a natural number : "))
sum = 0
for i in range(1,n+1):
    sum+=i
print("The sum is = ", str(sum))
                        
                
                        
mat1 = [[1,2],[3,4]]
mat2 = [[2,3],[4,4]]
sum = [[0,0],[0,0]]
#sum = [[0 for _ in range(len(mat1[0]))] for _ in range(len(mat1))] we can also do this

for i in range(len(mat1)):
    for j in range(len(mat1[0])):
        sum[i][j] = mat1[i][j] + mat2[i][j]

print(sum)
                        
                
                        
print("Enter marks of 5 subjects:")
sum_marks=[]
for i in range(5):
    sum_marks.append(int(input()))
    
avg = sum(sum_marks) / len(sum_marks) # len function give length of the list

if (avg>91) and (avg<=100): # if 91 < avg <=100:
    print("Grade = A")
elif 81 < avg <= 91:
    print("Grade = B")
elif 71 < avg <= 81:
    print("Grade = C")
elif 61 < avg <= 71:
    print("Grade = D")
elif 51 < avg <= 61:
    print("Grade = E")
else:
    print("Grade = F")
                        
                    
                        
ch = input("Enter a character:")
if 'a' <= ch <= 'z':
    print("Entered character is lower case letter")
elif 'A' <= ch <= 'Z':
    print("Enter character is upper case letter")
else:
    print("Entered character is not an Alphabet")
                        
                
                        
# Program to check if a number is even or odd, or less than 100

# Ask the user to enter a number
number = int(input("Enter a number: "))

# Check if the number is greater than or equal to 100
if number >= 100:
    # Check if the number is even or odd
    if number % 2 == 0:
        print(f"The number {number} is even.")
    else:
        print(f"The number {number} is odd.")
else:
    # Print message if the number is less than 100
    print("The number is less than 100.")
                        
                    

// & ** Operator in Python

The // and ** operators are special operators in Python that serve different purposes.

Examples of using these operators:

                
# Floor division (//) example
result_floor_division = 10 // 3 # Result: 3

# Exponentiation (**) example
result_exponentiation = 2 ** 4 # Result: 16
                
            
                        
import math #so that we can use math.sqrt function

x1 = int(input("Enter x1 of first coordinate : "))
y1 = int(input("Enter y1 of first coodinate : "))
x2 = int(input("Enter x2 of second coordinate : "))
y2 = int(input("Enter y2 of second coordinate : "))

ans = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
print("The distance is = ", str(ans))
                        
                

String Programs

Create a string "hello world" and perform the following opeations:

  1. Print the complete string
  2. Print the character from third to fifth
  3. Print the first character of the string
  4. Print the string's 3rd character
  5. Print the string twenty times
  6. Create another string text and concatinate with previous one

                            
str1 = "hello world"

#printing complete string
print(str1[0:]) # Output: hello world

#printing the character from third to fith
print(str1[2:5]) # Output: llo

#printing the first charcter of the string
print(str1[0]) # Output: h

#printing the string's 3rd character
print(str1[3]) # Output: l

#printing the string twenty times
print(str1 * 20) # Output: hello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello world

#create another string test and concatinate with previous one
test = "hi"
str1 = str1 + test
print(str1) # Output: hello worldhi
                            
                    
                        
def swapFirstLast(str):
    first_char = str[0]
    last_char = str[-1]
    newStr = last_char + str[1:-1] + first_char
    return newStr

myStr = "menance"

print(swapFirstLast(myStr))
                        
                    
                    
def find_middle_element(s):
    length = len(s)
    if length % 2 == 1:  # If the length of the string is odd
        middle_index = length // 2
        middle_element = s[middle_index]
        print("Middle element is:", middle_element)
    else:
        print("No middle element")
        return  # Return control flow when there is no middle element

string1 = "PASSED"
string2 = "REDPYTHON"

find_middle_element(string1)
find_middle_element(string2)
                    
                

List Programs

Create a list containing the following items: 556, "Mothi", 84, 96, 84, 75, 84 and name it student, and perform the following operations:

  1. Access 0th element
  2. Access 0th to 1st element
  3. Access 2nd to end of list elements
  4. Access starting to ending elements
  5. Access last index value
  6. Access elements in reverse order

Note: all the string slicing operation can be applied on list also.

                        
mylist = [556, "Mothi", 84, 96, 84, 75, 84]

#Access 0th element
print(mylist[0])

#Access 0th to first element
print(mylist[0:2])

#Access 2nd to end of the list
print(mylist[1:])

#Accessing starting to ending element
print(mylist[:])

#Access last index value
print(mylist[-1])

#Access element in reverse order
print(mylist[::-1])
                        
                    
                            
# Define a list of numbers
numbers = [10, 20, 30, 40, 50]

# Method 1: Using a for loop (no built-in sum() function)
total_sum_iterative = 0
for num in numbers:
    total_sum_iterative += num

# Method 2: Using the sum() function
total_sum_builtin = sum(numbers)

# Print the sums calculated by both methods
print("Sum of all elements using iterative approach:", total_sum_iterative)
print("Sum of all elements using built-in sum() function:", total_sum_builtin)
                            
                        
                        
myList = [22, 54, 2, 54, 33, 44]
smallest = largest = myList[0]

for num in myList:
    if num < smallest:
        smallest = num
    if num > largest:
        largest = num

print("Largest is =", largest)
print("Smallest is =", smallest)
                        
                
                        
# Define a list with duplicates
numbers = [10, 20, 30, 20, 40, 50, 10]

# Create an empty list to store unique elements
unique_numbers = []

# Loop through the original list
for num in numbers:
    # Check if the element is not already in the unique list
    if num not in unique_numbers:
        # Add the element to the unique list
        unique_numbers.append(num)

# Print the list without duplicates
print("List without duplicates:", unique_numbers)
                        
                    
                        
# Define a list
numbers = [10, 20, 30, 40, 50]

# Initialize an empty list to store the reversed elements
reversed_numbers = []

# Calculate the index of the last element in the original list
last_index = len(numbers) - 1

# Loop through the original list and add elements to the reversed list in reverse order
while last_index >= 0:
    reversed_numbers.append(numbers[last_index])
    last_index -= 1

# Print the reversed list
print("Original List:", numbers)
print("Reversed List:", reversed_numbers)
                        
                    
                        
# Define a list of integers
numbers = [9, 5, 2, 7, 1, 3]

# Implementing a simple bubble sort algorithm
def bubble_sort(arr):
    n = len(arr)
    # Traverse through all elements in the list
    for i in range(n):
        # Last i elements are already in place, so we don't need to check them again
        for j in range(0, n-i-1):
            # Swap if the element found is greater than the next element
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]

# Call the bubble_sort function with the list of numbers
bubble_sort(numbers)

# Print the sorted list
print("Sorted List:", numbers)
                        
                    

Tuple Programs

                        
# Define a tuple
my_tuple = (10, 20, 30, 40, 50)

# Function to calculate the length of the tuple
def tuple_length(t):
    count = 0
    for element in t:  # Using 'element' instead of '_'
        count += 1
    return count

# Call the function and print the length of the tuple
length = tuple_length(my_tuple)
print("Length of the tuple:", length)
                        
                    
                        
# Define two tuples
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)

# Function to concatenate two tuples
def concatenate_tuples(t1, t2):
    result = []
    for element in t1:
        result.append(element)
    for element in t2:
        result.append(element)
    return tuple(result)

# Call the function and print the concatenated tuple
concatenated_tuple = concatenate_tuples(tuple1, tuple2)
print("Concatenated Tuple:", concatenated_tuple)
                        
                    
                        
# Define a tuple
my_tuple = (10, 20, 30, 40, 50)

# Function to find the index of an element in the tuple
def find_index(t, element):
    for i in range(len(t)):
        if t[i] == element:
            return i
    return -1  # Return -1 if the element is not found

# Call the function and print the index of the element
element = 30
index = find_index(my_tuple, element)
if index != -1:
    print(f"The index of {element} is {index}")
else:
    print(f"{element} is not in the tuple")
                        
                    
                        
# Define a tuple
my_tuple = (10, 20, 30, 40, 50, 30, 20, 30)

# Function to count the occurrences of an element in the tuple
def count_occurrences(t, element):
    count = 0
    for item in t:
        if item == element:
            count += 1
    return count

# Call the function and print the count of the element
element = 30
count = count_occurrences(my_tuple, element)
print(f"The element {element} occurs {count} times in the tuple.")
                        
                    
                        
# Define two tuples
tuple1 = (1, 2, 3, 4, 5)
tuple2 = (5, 6, 7, 8, 9)

# Function to check if two tuples have any elements in common
def have_common_elements(t1, t2):
    for element in t1:
        if element in t2:
            return True
    return False

# Call the function and print the result
result = have_common_elements(tuple1, tuple2)
if result:
    print("The tuples have common elements.")
else:
    print("The tuples do not have any common elements.")
                        
                    

Set Programs

Set operations

  • Union (| or union()): Combines elements from two sets, including elements that are present in either or both sets, without duplicates.
  • Intersection (& or intersection()): Finds common elements between two sets, i.e., elements that are present in both sets.
  • Difference (- or difference()): Finds elements that are present in the first set but not in the second set.
    Formula → set1 - (set1 ⋂ set2)
  • Symmetric Difference (^ or symmetric_difference()): Finds elements that are present in either of the sets but not in both sets (excludes common elements).
    Formula → (set1 ⋃ set2) - (set1 ⋂ set2)
                            
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}

# Union
print(set1 | set2)  # Output: {1, 2, 3, 4, 5, 6}
print(set1.union(set2))  # Output: {1, 2, 3, 4, 5, 6}

# Intersection
print(set1 & set2)  # Output: {3, 4}
print(set1.intersection(set2))  # Output: {3, 4}

# Difference
print(set1 - set2)  # Output: {1, 2}
print(set1.difference(set2))  # Output: {1, 2}

# Symmetric Difference
print(set1 ^ set2)  # Output: {1, 2, 5, 6}
print(set1.symmetric_difference(set2))  # Output: {1, 2, 5, 6}
                            
                    
                        
# Define two sets
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}

# Function to check if two sets have any elements in common
def check_common_elements(set_a, set_b):
    # Use the intersection operation to find common elements
    common_elements = set_a.intersection(set_b)
    # Alternatively, we can use the '&' operator for intersection: common_elements = set_a & set_b
    if common_elements:
        return True
    else:
        return False

# Call the function and print the result
if check_common_elements(set1, set2):
    print("The sets have common elements.")
else:
    print("The sets do not have any common elements.")
                        
                    

Dictionary Programs

                        
# Define a dictionary
my_dict = {
    'name': 'Alice',
    'age': 25,
    'city': 'New York'
}

# Function to iterate over the dictionary and print key-value pairs
def print_key_value_pairs(d):
    for key in d:
        print(f"Key: {key}, Value: {d[key]}")

# Call the function with the dictionary
print_key_value_pairs(my_dict)
                        
                    
                        
# Define two dictionaries
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'d': 4, 'e': 5, 'f': 6}

# Function to merge two dictionaries
def merge_dicts(d1, d2):
    merged_dict = {}
    
    # Add key-value pairs from the first dictionary
    for key in d1:
        merged_dict[key] = d1[key]
    
    # Add key-value pairs from the second dictionary
    for key in d2:
        merged_dict[key] = d2[key]
    
    return merged_dict

# Call the function and print the merged dictionary
merged_dict = merge_dicts(dict1, dict2)
print("Merged Dictionary:", merged_dict)
                        
                    
                        
# Define a dictionary
my_dict = {'a': 10, 'b': 5, 'c': 20, 'd': 15}

# Function to find keys corresponding to max and min values in a dictionary
def find_max_min_keys(d):
    max_key = None
    max_val = float('-inf')  # Initialize max_val to negative infinity
    min_key = None
    min_val = float('inf')   # Initialize min_val to positive infinity

    # Iterate over the dictionary to find max and min values
    for key, value in d.items():
        if value > max_val:
            max_val = value
            max_key = key
        if value < min_val:
            min_val = value
            min_key = key
    
    return max_key, min_key

# Call the function and print the keys
max_key, min_key = find_max_min_keys(my_dict)
print("Key corresponding to max value:", max_key)
print("Key corresponding to min value:", min_key)
                        
                    
                        
# Define a dictionary
my_dict = {'a': 10, 'b': 20, 'c': 30}

# Function to check if a key exists in a dictionary
def check_key_exists(d, key):
    if key in d:
        return True
    else:
        return False

# Call the function with a key to check
key_to_check = 'b'
if check_key_exists(my_dict, key_to_check):
    print(f"The key '{key_to_check}' exists in the dictionary.")
else:
    print(f"The key '{key_to_check}' does not exist in the dictionary.")
                        
                    
                            
# Define a dictionary
my_dict = {'a': 10, 'b': 5, 'c': 20, 'd': 15}

# Function to sort a dictionary by values in ascending order
def sort_dict_by_values(d):
    # Convert the dictionary items to a list of tuples
    items = list(d.items())

    # Bubble sort to sort the list of tuples by values
    n = len(items)
    for i in range(n):
        for j in range(0, n-i-1):
            if items[j][1] > items[j+1][1]:
                items[j], items[j+1] = items[j+1], items[j]

    # Convert the sorted list of tuples back to a dictionary
    sorted_dict = dict(items)
    return sorted_dict

# Call the function to sort the dictionary
sorted_dict = sort_dict_by_values(my_dict)

# Print the sorted dictionary
print("Sorted Dictionary (Ascending Order):", sorted_dict)
                            
                        

When you use my_dict.items(), it returns a view object that provides a way to access the key-value pairs of the dictionary as tuples. These tuples are then added as items to a list, creating a list of tuples where each tuple represents a key-value pair from the dictionary.
For example, if my_dict = {'a': 10, 'b': 5, 'c': 20, 'd': 15}, then list(my_dict.items()) would result in:

                        
[('a', 10), ('b', 5), ('c', 20), ('d', 15)]
                        
                    

Function Programs

                        
# Leap year condition:
# 1. If the year is divisible by 4,
# 2. But not divisible by 100,
# 3. Unless the year is also divisible by 400.

def is_leap_year(year):
    if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
        return True
    else:
        return False

year = int(input("Enter a year: "))
if is_leap_year(year):
    print(f"{year} is a leap year.")
else:
    print(f"{year} is not a leap year.")
                        
                
                    
def calc(a, b, op):
    if op == 1:
        print("Addition is =", a + b)
    elif op == 2:
        print("Subtraction is =", a - b)
    elif op == 3:
        print("Multiplication is =", a * b)
    elif op == 4:
        if b == 0:
            print("Division cannot be performed")
            return
        print("Division is =", a / b)
    else:
        print("Enter a correct operation")

# Main program loop
while True:
    # Get user input for numbers and operation choice
    a = int(input("Enter the first number: "))
    b = int(input("Enter the second number: "))
    op = int(input("\nEnter '1' for addition\n'2' for subtraction\n'3' for multiplication\n'4' for division\n'0' to exit: "))

    # Perform calculation based on user input
    if op == 0:
        print("Exiting...")
        break  # Exit the loop
    else:
        calc(a, b, op)
                    
                
                        
# Function to perform addition
def add(x, y):
    return x + y

# Function to perform subtraction
def subtract(x, y):
    return x - y

# Function to perform multiplication
def multiply(x, y):
    return x * y

# Function to perform division
def divide(x, y):
    if y == 0:
        return "Error! Division by zero."
    else:
        return x / y

print("Simple Calculator")
print("Enter two numbers:")
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

print("Available operations:")
print("1. Addition (+)")
print("2. Subtraction (-)")
print("3. Multiplication (*)")
print("4. Division (/)")

operator = input("Enter operator (e.g., +, -, *, /): ")

if operator == '+':
    print("Result:", add(num1, num2))
elif operator == '-':
    print("Result:", subtract(num1, num2))
elif operator == '*':
    print("Result:", multiply(num1, num2))
elif operator == '/':
    print("Result:", divide(num1, num2))
else:
    print("Invalid operator! Please enter a valid operator.")
                        
                
                            
import math
a = int(input("Enter a number : "))

def checkIsPrime(num):
    if num < 2:
        return False
    else:
        for i in range(2, int(math.sqrt(num))):
            if(num % i == 0):
                return False
    return True

if checkIsPrime(a):
    print("The number is prime")
else:
    print("The number is not prime")
                            
                    
                        
import math

def checkIsPrime(num):
    if num < 2:
        return False
    else:
        for i in range(2, int(math.sqrt(num))): #ultimate optimization
            if(num % i == 0):
                return False
    return True

def printPrime(x, y):
    for i in range(x+1, y-1):
        if(checkIsPrime(i)):
            print(i)
    
lowerRange = int(input("Enter lower range : "))
upperRange = int(input("Enter upper range : "))

printPrime(lowerRange, upperRange)
                        
                    
                        
muNumbers = [22, 33, 44, 55, 4, 3]

def meanAndMedian(arr):
    # Calculate the mean manually
    sum_values = 0
    for num in arr:
        sum_values += num
    mean_value = sum_values / len(arr)

    # Sort the array manually
    arr.sort()

    # Calculate the median manually
    n = len(arr)
    if n % 2 == 0:  # For even number of elements
        median_value = (arr[(n // 2) - 1] + arr[n // 2]) / 2
    else:  # For odd number of elements
        median_value = arr[n // 2]

    return mean_value, median_value

mean, median = meanAndMedian(muNumbers)

print("Mean =", mean, "and Median =", median)
                        
                    
                            
num = int(input("Enter a number: "))
square = lambda x: x * x
print(f"The square of {num} is {square(num)}")
                            
                        
                        
def calculate_string_length(input_string):
    length = 0
    for char in input_string:
        length += 1
    return length

# Test the function
input_string = "Hello, world!"
length = calculate_string_length(input_string)
print(f"The length of the string '{input_string}' is {length}.")
                        
                    
                        
def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)

# Test the function
num = int(input("Enter a number: "))
result = factorial(num)
print(f"The factorial of {num} is {result}.")
                        
                    

Pattern Programs

WAP to print the following pattern
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

                            
    # These both loop can print the pattern
    
    for i in range(5):
        for j in range(i+1):
            print(i+1, end=' ')
        print()
        
    for i in range(1,6):
        for j in range(0,i):
                print(i, end=' ')
        print()
                            
                    

WAP to print the following pattern

                    
            *
         *  *
      *  *  *
   *  *  *  *
*  *  *  *  *
                    
                
                                
    n = 5
    for i in range(n):
        for j in range(n-i-1):
            print(" ", end='')
        for k in range(i+1):
            print("*", end='')
        print()
                                
                        

Module Programs

Program to create basic calculator using module

                    
def addition(x, y):
    return x + y

def subtract(x, y):
    return x - y

def multiply(x, y):
    return x * y

def divide(x, y):
    if y == 0:
        return "Error! Division by zero."
    return x / y
                    
                
                    
import calculatorModule

def getValues():
    num1 = int(input("Enter first number: "))
    num2 = int(input("Enter second number: "))
    return num1, num2

while True:
    operator = int(input("** MENU ** \n1. For addition \n2. For subtraction \n3. For multiplication \n4. For division \n5. Exit: "))

    if operator in [1, 2, 3, 4]:
        num1, num2 = getValues()

        if operator == 1:
            print(f"The addition is = {calculatorModule.addition(num1, num2)}")
        elif operator == 2:
            print(f"The subtraction is = {calculatorModule.subtract(num1, num2)}")
        elif operator == 3:
            print(f"The multiplication is = {calculatorModule.multiply(num1, num2)}")
        elif operator == 4:
            print(f"The division is = {calculatorModule.divide(num1, num2)}")
    elif operator == 5:
        break
    else:
        print("Wrong selection")
                    
                

Explanation of the in Keyword

  • In Python, the in keyword is used to check if a value exists within a sequence (such as a list, tuple, string, etc.). It returns True if the value is found in the sequence and False otherwise.
  • In the context of the provided code, the in keyword is used to check if the user has entered a valid operator choice:
                                
    if operator in [1, 2, 3, 4]:
                                
                            
    This line checks whether the value of operator is one of the elements in the list [1, 2, 3, 4]. If operator is 1, 2, 3, or 4, the condition evaluates to True, and the code inside the if block is executed. If the value of operator is not in the list, the condition evaluates to False, and the else block is executed, prompting the user that the selection is wrong.

File Handling Programs

                        
f1 = open("new.txt", "w")
f1.write(input("Enter a line : "))
f1.close()
                        
                    
                        
f = open("myfile.txt", "w")

line1 = input("Enter the first line: ")
line2 = input("Enter the second line: ")
line3 = input("Enter the third line: ")

lines = [line1 + "\n", line2 + "\n", line3 + "\n"]
f.writelines(lines)

f.close()
                        
                    
                        
# Open intern.txt and read its content
f1 = open("intern.txt", "r")
intern_content = f1.read()
f1.close()

# Open myfile.txt and read its content
f2 = open("myfile.txt", "r")
myfile_content = f2.read()
f2.close()

# Merge the contents into merge.txt
merge_file = open("merge.txt", "w")
merge_file.write(intern_content)
merge_file.write("\n")  # Add a new line between contents
merge_file.write(myfile_content)
merge_file.close()

print("Contents from intern.txt and myfile.txt have been merged into merge.txt")
                        
                    
                        
# Open merge.txt and read its content
merge_file = open("merge.txt", "r")
merge_content = merge_file.read()
merge_file.close()

# Initialize counters
uppercase_count = 0
lowercase_count = 0
digit_count = 0

# Iterate through each character in the content
for char in merge_content:
    # Check for uppercase letters
    if 'A' <= char <= 'Z':
        uppercase_count += 1
    # Check for lowercase letters
    elif 'a' <= char <= 'z':
        lowercase_count += 1
    # Check for digits
    elif '0' <= char <= '9':
        digit_count += 1

# Print the counts
print("Total Uppercase Letters:", uppercase_count)
print("Total Lowercase Letters:", lowercase_count)
print("Total Digits:", digit_count)
                        
                    

Numpy Programs

                        
import numpy as np

# Define a list of numbers
numbers = [1, 2, 3, 4, 5, 6]

# Create a NumPy array from the list of numbers
array = np.array(numbers)

print("NumPy array:", array)
                        
                    
                        
import numpy as np

# Create an array of 10 random integers between 0 and 100
random_integers = np.random.randint(0, 100, size=10)

print("Array of 10 random integers:", random_integers)
                        
                    
                        
import numpy as np

array = np.arange(10, 21)

print("NumPy array:", array)
                        
                    
                        
import numpy as np

# Create a one-dimensional NumPy array with 9 elements
one_d_array = np.arange(1, 10)

# Reshape the array into a 3x3 matrix
matrix = one_d_array.reshape(3, 3)

print("One-dimensional array:", one_d_array)
print("3x3 matrix:\n", matrix)
                        
                    
                        
import numpy as np

# Create a NumPy array with elements from 1 to 10
array = np.arange(1, 11)

# Replace all even numbers with 0
for i in array:
    if array[i]%2==0:
        array[i] = 0

print("Modified array:", array)
                        
                    

Perform the following slicing:

Input:

                    
 1   2   3   4
 5   6   7   8
 9  10  11  12
13  14  15  16
                    
                

Output:

                    
 6   7
10  11
14  15
                    
                
                            
import numpy as np
arr1 = np.arrange(1, 17).reshape(4, 4)
arr2 = arr1[1:,1:3]
                            
                        
                        
import numpy as np

# Difference 1: Type
# --------------------
# Python list
py_list = [1, 2, 3, 4, 5]
print(f"Type of py_list: {type(py_list)}")  # The type of py_list is 

# NumPy array
np_array = np.array([1, 2, 3, 4, 5])
print(f"Type of np_array: {type(np_array)}")  # The type of np_array is 

# Difference 2: Element-wise Operations
# -------------------------------------
# Element-wise addition

py_list_add=[]
# Python list - element-wise addition requires a loop
for i in range(len(py_list)):
    py_list_add.append(py_list[i] + 1)
print(f"Python list after addition: {py_list_add}")  # Python list after addition: [2, 3, 4, 5, 6]

# NumPy array - element-wise addition is vectorized
np_array_add = np_array + 1
print(f"NumPy array after addition: {np_array_add}")  # NumPy array after addition: [2 3 4 5 6]

# Element-wise multiplication

# Python list - element-wise multiplication requires a loop
py_list_mul = []
for i in range(len(py_list)):
    py_list_mul.append(2*py_list[i])
print(f"Python list after multiplication: {py_list_mul}")  # Python list after multiplication: [2, 4, 6, 8, 10]

# NumPy array - element-wise multiplication is vectorized
np_array_mul = np_array * 2
print(f"NumPy array after multiplication: {np_array_mul}")  # NumPy array after multiplication: [ 2  4  6  8 10]

# Difference 3: Slicing
# ---------------------
# Python list - slicing returns a new list
py_list_slice = py_list[1:4]
print(f"Python list slice (1:4): {py_list_slice}")  # Python list slice (1:4): [2, 3, 4]

# NumPy array - slicing returns a view of the original array (no data copy)
np_array_slice = np_array[1:4]
print(f"NumPy array slice (1:4): {np_array_slice}")  # NumPy array slice (1:4): [2 3 4]

# Difference 4: Data Types
# ------------------------
# Python list - can store elements of different data types
py_list_mixed = [1, "two", 3.0]
print(f"Python list with mixed types: {py_list_mixed}")  # Python list with mixed types: [1, 'two', 3.0]

# NumPy array - all elements must be of the same data type
np_array_mixed = np.array([1, 2, 3.0])
print(f"NumPy array with mixed types: {np_array_mixed}")  # NumPy array with mixed types: [1. 2. 3.]
                        
                    

Pandas Program

                        
import pandas as pd

# Create a pandas Series
data = [10, 20, 30, 40, 50]
series = pd.Series(data)

# Convert the pandas Series to a Python list
list_from_series = series.tolist()

# Print the Python list
print("Python List from Pandas Series:")
print(list_from_series)
                        
                    
                        
import pandas as pd

# Create two pandas Series using lists without labels
data1 = [10, 20, 30]
data2 = [5, 15, 25]

series1 = pd.Series(data1)
series2 = pd.Series(data2)

# Addition
addition = series1 + series2
print("Addition:")
print(addition)

# Subtraction
subtraction = series1 - series2
print("\nSubtraction:")
print(subtraction)

# Multiplication
multiplication = series1 * series2
print("\nMultiplication:")
print(multiplication)

# Division
division = series1 / series2
print("\nDivision:")
print(division)
                        
                    
                        
import numpy as np
import pandas as pd

# Create a NumPy array
numpy_array = np.array([10, 20, 30, 40, 50])

# Convert the NumPy array to a pandas Series
series_from_numpy = pd.Series(numpy_array)

# Print the pandas Series
print("Pandas Series from NumPy array:")
print(series_from_numpy)
                        
                    
                        
import pandas as pd

# Create a dictionary with index labels
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Emily'],
        'Age': [25, 30, 35, 40, 45],
        'City': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Dallas']}
index_labels = ['A', 'B', 'C', 'D', 'E']

# Create a pandas DataFrame with index labels
df = pd.DataFrame(data, index=index_labels)

# Display the pandas DataFrame
print("Pandas DataFrame with Index Labels:")
print(df)
                        
                    
                        
import pandas as pd

# Load the CSV file into a pandas DataFrame
df = pd.read_csv('dirtydata.csv')

# Remove rows with empty cells
df.dropna(inplace=True)

# Remove rows with data in wrong format in the 'Age' column
df = df[pd.to_numeric(df['Age'], errors='coerce').notnull()]

# Remove duplicate rows
df.drop_duplicates(inplace=True)
                        
                    
                        
import pandas as pd

# Load the CSV file into a pandas DataFrame
df = pd.read_csv('dirtydata.csv')

# Display the original data
print("Original Data:")
print(df)

# Remove duplicate rows based on all columns
df.drop_duplicates(inplace=True)

# Display the cleaned data
print("\nCleaned Data (After Removing Duplicates):")
print(df)

# Save the cleaned DataFrame back to the same CSV file
df.to_csv('dirtydata.csv', index=False)
print("\nCleaned data (without duplicates) saved back to 'dirtydata.csv'")