Python Editor

DataFrames using Pandas



# Import the pandas library import pandas as pd # Create a sample DataFrame data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'], 'Age': [25, 30, 35, 28], 'City': ['New York', 'Los Angeles', 'Chicago', 'Houston']} df = pd.DataFrame(data) # Display the DataFrame print("Original DataFrame:") print(df) # Accessing specific columns print("\nAccessing 'Name' column:") print(df['Name']) # Accessing specific rows print("\nAccessing the first row:") print(df.iloc[0]) # Filtering data based on conditions print("\nPeople above the age of 30:") print(df[df['Age'] > 30]) # Adding a new column df['Salary'] = [50000, 60000, 75000, 48000] print("\nDataFrame with the 'Salary' column:") print(df) # Deleting a column df = df.drop(columns=['City']) print("\nDataFrame after deleting the 'City' column:") print(df)

Program Explanation -

Python | Podcasts | Episode01-Is Python Interpreted Language?

Python | Applications | Quiz App


class Question: def __init__(self, prompt, answer): self.prompt = prompt self.answer = answer # Define a list of questions questions = [ Question("What is the capital of France?\n(a) Paris\n(b) Rome\n(c)
Madrid\n\n", "a"), Question("Which programming language is known for its readability?\n(a) Java\n(b)
Python\n(c) C++\n\n", "b"), Question("What symbol is used to indicate the start of a comment in Python?\n(a)
//\n(b) /*\n(c) #\n\n", "c") ] # Function to run the quiz def run_quiz(questions): score = 0 for question in questions: answer = input(question.prompt) if answer.lower() == question.answer.lower(): score += 1 print("You got " + str(score) + "/" + str(len(questions)) + " correct!") # Run the quiz run_quiz(questions)



Python | Lesson3 | Functions - Addition


def add(x, y):
   "This function adds two numbers"
   return x + y

num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

print("sum of num1,num2 =", add(num1,num2))


# Todo :  Create Subtraction, Multiplication and division functions,
# and print the results using the functions similar to add function 


Python | Lesson4 | Date Module


from datetime import datetime, timedelta

# current date and time
now = datetime.now()
print("Current date and time:", now)

# date object for a specific date
date1 = datetime(2022, 12, 25)
print("Date object for 25th December, 2022:", date1)

# date object for a specific date and time
date2 = datetime(2022, 12, 25, 14, 30, 45)
print("Date object for 25th December, 2022 2:30:45 PM:", date2)

# difference between two dates
delta = date2 - date1
print("Time delta:", delta)

# adding or subtracting time from a date
new_date = date1 + timedelta(days=5)
print("New date after adding 5 days:", new_date)


Python | Lesson4 | Math Module

 

import math

# square root of a number
x = 16
print("The square root of", x, "is", math.sqrt(x))


# max of two numbers
c = 4
d = 8
print("The max of", c, "and", d, "is", max(c, d))

# floor of a decimal number
e = 3.14159
print("The floor of", e, "is", math.floor(e))

# round off a decimal number to nearest integer
f = 2.71828
print("The round off of", f, "is", round(f))

# sine of an angle
y = math.pi/4
print("The sine of", y, "is", math.sin(y))

# cosine of an angle
z = math.pi/3
print("The cosine of", z, "is", math.cos(z))

# natural logarithm of a number
a = 2.71828
print("The natural logarithm of", a, "is", math.log(a))

# base 10 logarithm of a number
b = 100
print("The base 10 logarithm of", b, "is", math.log10(b))



Python | Lesson3 | Arrays

 


# Create an array of integers
integers = [1, 2, 3, 4, 5]

# Print the array
print(integers)  
# Access an element of the array
print(integers[2]) # 3

# Modify an element of the array
integers[2] = 6
print(integers)

# Append an element to the array
integers.append(7)
print(integers)
# Remove an element from the array
integers.remove(4)
print(integers)  

# Get the length of the array
print(len(integers))

Python | Lesson3 | Lambdas


# A  lambda function is a small anonymous function which
# can take any number of arguments, but can only have only one expression.

# Define a lambda function that takes in two numbers and returns their sum
sum = lambda x, y: x + y

# Test the function
print(sum(3,4)) # 7

# Using lambda function with filter
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers) # [2, 4, 6, 8, 10]

# Using lambda function with map
squared_numbers = list(map(lambda x: x**2, even_numbers))
print(squared_numbers) # [4, 16, 36, 64, 100] 


Python | Lesson3 | Functions

 

# Palindrome - the string is the same when reversed
# Example:  121, reverse 121 which is a palindrome number
# Example: MADAM, revere - MADAM, which is a palindrome string.

Algorithm:
Start | | | Define function | | | Convert number to string | | | Check if string is same when reversed | | | Return True if yes | | | End
def check_palindrome(input):
    # Convert the inputber to a string
    input_str = str(input)
    # Check if the string is the same when reversed
    return input_str == input_str[::-1]

# Test the function
print(check_palindrome(121)) # True
print(check_palindrome("MADAM")) # True

print(check_palindrome(123)) # False
print(check_palindrome("Teacher")) # False