0% found this document useful (0 votes)
4 views7 pages

Python Programming Basics Guide

Uploaded by

Fenris Loston
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Python Programming Basics Guide

Uploaded by

Fenris Loston
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

# Module 1

# 1. Design a calculator and all arithmetic operations

def calculator(a, b):

return {

'add': a + b,

'subtract': a - b,

'multiply': a * b,

'divide': a / b if b != 0 else 'Undefined'

# 2. Operators

# Arithmetic: +, -, *, /, %, **, //

# Comparison: ==, !=, >, <, >=, <=

# Logical: and, or, not

# Assignment: =, +=, -=, etc.

# Bitwise: &, |, ^, ~, <<, >>

# 3. Looping Statements

# for, while, break, continue, pass

# 4. Different types of function arguments

def func_example(a, b=10, *args, **kwargs):

return a, b, args, kwargs

# 5. Python program to find sum of square roots


import math

def sum_of_square_roots(n):

return sum([Link](i) for i in range(1, n+1))

# 6. Palindrome program

def is_palindrome(s):

return s == s[::-1]

# Module 2

# List Methods

list_methods = dir(list)

# Tuple Methods

tuple_methods = dir(tuple)

# Dictionary Methods

dict_methods = dir(dict)

# Set Methods

set_methods = dir(set)

# Module 3

import re

# 1. Program to find email pattern

def find_emails(text):

return [Link](r'\b[\w.-]+@[\w.-]+\.\w+\b', text)


# 2. Program to remove all white spaces

def remove_whitespace(s):

return ''.join([Link]())

# 3. Library and Branch class

class Library:

total_books = 0

def __init__(self):

pass

class Branch(Library):

def __init__(self, branch_books):

super().__init__()

self.branch_books = branch_books

Library.total_books += branch_books

# branch1 = Branch(100)

# branch2 = Branch(150)

# print(f"Total Books: {Library.total_books}, Branch 1 Books: {branch1.branch_books}, Branch 2


Books: {branch2.branch_books}")

# 4. Five modules of regular expressions

# match, search, findall, split, sub


# 5. Program to check email pattern

def is_valid_email(email):

return bool([Link](r'^[\w.-]+@[\w.-]+\.\w+$', email))

# 6. Check if "Python" is at the beginning

def starts_with_python(s):

return [Link]("Python")

# 7. Password validation

def is_valid_password(pwd):

return (len(pwd) > 9 and [Link](r'[A-Za-z]', pwd)

and [Link](r'\d', pwd) and [Link](r'[^\w]', pwd))

# 8. Starts with "The" and ends with "Spain"

def check_the_spain(s):

return [Link]("The") and [Link]("Spain")

# Module 4

import numpy as np

import pandas as pd

# 1. Numpy aggregate operations

arr = [Link]([1, 2, 3, 4])

agg_ops = {

'sum': [Link](arr),
'mean': [Link](arr),

'max': [Link](arr),

'min': [Link](arr)

# 2. Inner join in pandas

df1 = [Link]({'ID': [1, 2], 'Name': ['Alice', 'Bob']})

df2 = [Link]({'ID': [1, 2], 'Score': [90, 85]})

inner_join_df = [Link](df1, df2, on='ID')

# 3. Sort pandas dataframe

df = [Link]({

'Name': ['Alice', 'Bob', 'Charlie'],

'Score': [85, 90, 85],

'Age': [25, 23, 24]

})

sorted_df = df.sort_values(by=['Score', 'Age'])

# 4. Handling missing data

data = [Link]({

'A': [1, [Link], 3],

'B': [4, 5, [Link]]

})

data_filled = [Link](0)

data_dropped = [Link]()
# 5. .loc vs .iloc

# loc uses labels, iloc uses index positions

example_df = [Link]({

'name': ['Tom', 'Jerry'],

'score': [90, 95]

})

loc_result = example_df.loc[0]

iloc_result = example_df.iloc[0]

# 6. DataFrame methods

dataframe_methods = dir([Link])

# Module 5

import [Link] as plt

import seaborn as sns

# 1. Line graph

x = [1, 2, 3]

y = [2, 4, 6]

[Link](x, y)

[Link]("Line Graph")

[Link]()

# 2. Bar chart for frequency distribution

categories = ['A', 'B', 'C']


counts = [5, 3, 6]

[Link](categories, counts)

[Link]("Bar Chart")

[Link]()

# 3. Stacked Bar Chart

A = [3, 2, 5]

B = [4, 1, 2]

labels = ['X', 'Y', 'Z']

[Link](labels, A, label='A')

[Link](labels, B, bottom=A, label='B')

[Link]()

[Link]("Stacked Bar Chart")

[Link]()

# 4. Heat Map

corr_matrix = [Link]({

'A': [1, 2, 3],

'B': [4, 5, 6],

'C': [7, 8, 9]

}).corr()

[Link](corr_matrix, annot=True)

[Link]("Heatmap")

[Link]()

# 5 & 6. Line and Bar Chart (already covered)

You might also like