-
1. Introduction to Python
-
2. Python Basics
-
3. Working with Data Structures
-
4. Functions and Modules
-
5. Object-Oriented Programming (OOP)
-
6. File Handling
-
7. Error and Exception Handling
-
8. Python for Data Analysis
-
9. Advanced Topics in Python
-
10. Working with APIs
-
11. Python for Automation
-
12. Capstone Projects
- 13. Final Assessment and Quizzes
Solution: Hands-On Coding Challenges
1. Introduction to Python
Challenge: Print "Hello, Python!" to the console.
# Solution print("Hello, Python!")
Challenge: Check the Python version installed on your system using a script.
# Solution import sys print("Python version:", sys.version)
2. Python Basics
Challenge: Write a program that accepts a user's name and greets them with "Hello, [name]!"
# Solution name = input("Enter your name: ") print(f"Hello, {name}!")
Challenge: Write a program to calculate the sum of two numbers entered by the user.
# Solution num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) print("Sum:", num1 + num2)
3. Working with Data Structures
Lists
Challenge: Create a list of 5 numbers and print the sum of all the numbers in the list.
# Solution numbers = [1, 2, 3, 4, 5] print("Sum:", sum(numbers))
Tuples
Challenge: Write a program to create a tuple with elements from 1 to 5 and print the third element.
# Solution my_tuple = (1, 2, 3, 4, 5) print("Third element:", my_tuple[2])
Dictionaries
Challenge: Create a dictionary with keys as names and values as ages. Print all names (keys) in the dictionary.
# Solution people = {"Alice": 25, "Bob": 30, "Charlie": 35} print("Names:", list(people.keys()))
Sets
Challenge: Write a program to find the intersection of two sets.
# Solution set1 = {1, 2, 3, 4} set2 = {3, 4, 5, 6} print("Intersection:", set1 & set2)
4. Functions and Modules
Challenge: Write a function to calculate the factorial of a given number.
# Solution def factorial(n): if n == 0: return 1 return n * factorial(n - 1) print(factorial(5)) # Output: 120
Challenge: Import the math module and use it to calculate the square root of 64.
# Solution import math print("Square root of 64:", math.sqrt(64))
5. Object-Oriented Programming (OOP)
Classes and Objects
Challenge: Create a Car class with attributes make and model, and a method display_info() that prints them.
# Solution class Car: def __init__(self, make, model): self.make = make self.model = model def display_info(self): print(f"Make: {self.make}, Model: {self.model}") my_car = Car("Toyota", "Camry") my_car.display_info()
Inheritance
Challenge: Create a Dog class that inherits from an Animal class and overrides a method.
# Solution class Animal: def sound(self): print("Some generic sound") class Dog(Animal): def sound(self): print("Bark") my_dog = Dog() my_dog.sound()
6. File Handling
Challenge: Write a program to create a text file and write "Hello, File!" into it.
# Solution with open("example.txt", "w") as file: file.write("Hello, File!")
Challenge: Write a program to read and print the contents of the text file created above.
# Solution with open("example.txt", "r") as file: content = file.read() print(content)
7. Error and Exception Handling
Challenge: Write a program to handle a ZeroDivisionError exception.
# Solution try: result = 10 / 0 except ZeroDivisionError: print("Cannot divide by zero!")
8. Python for Data Analysis
NumPy
Challenge: Create a NumPy array of numbers from 1 to 5 and calculate the mean.
# Solution import numpy as np arr = np.array([1, 2, 3, 4, 5]) print("Mean:", np.mean(arr))
Pandas
Challenge: Create a Pandas DataFrame with columns Name and Age and print it.
# Solution import pandas as pd data = {"Name": ["Alice", "Bob"], "Age": [25, 30]} df = pd.DataFrame(data) print(df)
9. Advanced Topics in Python
Generators
Challenge: Create a generator to yield numbers from 1 to 3.
# Solution def my_generator(): for i in range(1, 4): yield i for num in my_generator(): print(num)
Regular Expressions
Challenge: Write a regex to find all email addresses in a string.
# Solution import re text = "Contact us at support@example.com or admin@domain.com." emails = re.findall(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b', text) print("Emails:", emails)
10. Working with APIs
Making API Requests
Challenge: Use the requests library to fetch data from an API (e.g., https://jsonplaceholder.typicode.com/todos/1).
# Solution import requests response = requests.get("https://jsonplaceholder.typicode.com/todos/1") print(response.json())
Parsing JSON Responses
Challenge: Parse the JSON response to print the title of the fetched data.
# Solution data = response.json() print("Title:", data["title"])
These challenges are designed to provide hands-on experience with Python while covering essential concepts.
Commenting is not enabled on this course.