"Python Study Notes: Modules, Dates, Dictionaries, Problem Solving"

🐍 Python Study Notes Week 3 (Day 3–6) 📘 Day 3: Python Modules and Libraries 🔹 What is a Module? A module is a file containing Python code (functions, classes, variables) that can be reused in other programs. 🔹 What is a Library? A library is a collection of modules that provide specific functionality, like math operations or random number generation. 📐 math Module Used for mathematical operations. import math print(math.sqrt(1000)) # Square root of 1000 print(math.pi) # Value of π (pi) You can rename the module: import math as m print(m.pow(2, 6)) # 2 raised to the power of 6 🎲 random Module Used to generate random values. import random print(random.random()) # Random float between 0 and 1 print(random.randint(100, 150)) # Random integer between 100 and 150 colors = ["red", "blue", "black", "yellow", "green"] print(random.choice(colors)) # Randomly picks one item random.shuffle(colors) # Shuffles the list print(colors) 📘 Day 4: Working with Dates and Time 🔹 datetime Module Used to work with dates and times. import datetime from datetime import datetime, date, time, timedelta current_datetime = datetime.now() print(current_datetime) # Current date and time print(current_datetime.year) # Year print(current_datetime.month) # Month print(current_datetime.day) # Day 📌 Other Components date() – Represents a calendar date. time() – Represents time (hour, minute, second). datetime() – Combines date and time. timedelta() – Represents a duration (difference between two dates/times). 📘 Day 5: Problem Solving with Dictionaries 🔹 What is a Dictionary? A dictionary is a collection of key-value pairs. It helps store and retrieve data efficiently. 🧩 Problem: Find the Most Repetitive Element L1 = [1, 2, 2, 3, 2, 3, 4, 5] counts = {} for item in L1:     if item in counts:         counts[item] += 1     else:         counts[item] = 1     print(counts) temp = 0 most_repetitive_elements = None for k, v in counts.items():     print(f"item : {k}, value : {v}, temp = {temp}")     if v > temp:         temp = v         most_repetitive_elements = k print("most repetitiveelement is : ",most_repetitive_elements) 📘 Day 6: Problem Solving Without Dictionary 🧩 Same Problem, Different Approach l1 = [1, 2, 2, 3, 2, 3, 4, 5] most_repetitive_elements = None max_count = 0 for item in l1:     count = l1.count(item)     print(f"Item : {item}, count : {count}")     if count > max_count:         max_count = count         most_repetitive_elements = item print(f"The most repetitive element is : {most_repetitive_elements} and count is : {max_count}") #python #DataEngineer #AzureDataEngineer #DataAnalytics

To view or add a comment, sign in

Explore content categories