Comprehensive Python Concepts

Bhimraj Yadav
7 min readJul 8, 2023

--

Python Exercise Project: Comprehensive Python Concepts

In this exercise project, we will cover various important Python concepts. Each section will focus on a specific concept, providing explanations, code examples, potential errors, and hints to help you understand and apply the concepts effectively. Let’s dive in!

Section 1: Basic Data Types

Concepts Covered:

  • Basic data types: int, float, str, bool, list

Instructions:

  1. Create variables of each basic data type: int, float, str, bool, list.
  2. Perform operations and conversions using these data types.

Example Code:

# Basic Data Types
my_int = 5
my_float = 3.14
my_str = "Hello, World!"
my_bool = True
my_list = [1, 2, 3, 4, 5]
# Operations and Conversions
result = my_int / my_float
int_conversion = int(my_float)
float_conversion = float("2.5")
str_conversion = str(42)
rounded_value = round(3.14159, 2)

Section 2: Operators and Static vs Dynamic Typing

Concepts Covered:

  • Operators: // (integer divide), ** (exponentiation)
  • Static typing vs dynamic typing

Instructions:

  1. Perform arithmetic operations using the integer divide (//) and exponentiation (*) operators.
  2. Observe the difference in behavior between static typing and dynamic typing when performing division (/).

Example Code:

# Operators and Static vs Dynamic Typing
integer_division = 5 // 2
exponentiation = 2 ** 3

Section 3: Data Type Conversion

Concepts Covered:

  • Data type conversion using functions: float(), int(), str(), round()

Instructions:

  1. Convert values between different data types using the provided conversion functions.
  2. Perform rounding using the round() function.

Example Code:

# Data Type Conversion
float_conversion = float(2)
str_conversion = str(42)
rounded_value = round(3.14159, 2)

Section 4: List Operations

Concepts Covered:

  • List methods: len(), insert(), append(), pop()
  • Slicing: [i:j:step]
  • List repetition: [0]*4, [2,4]*3
  • Sorting: sorted(my_list) vs my_list.sort()

Instructions:

  1. Perform operations on lists using the provided methods.
  2. Understand slicing and perform slicing operations on lists.
  3. Explore list repetition using the * operator.
  4. Compare the behavior of sorted(my_list) and my_list.sort().

Example Code:

# List Operations
my_list = [1, 2, 3, 4, 5]

list_length = len(my_list)
my_list.insert(0, 0)
my_list.append(6)
popped_value = my_list.pop(0)
sliced_list = my_list[1:4]
repeated_list = [0] * 4
extended_list = [2, 4] * 3
sorted_list = sorted(my_list) # Returns a new sorted list
my_list.sort() # Sorts the list in-place

Section 5: Tuple and Dictionary Operations

Concepts Covered:

  • Tuple creation and usage
  • Dictionary methods: get(), dict[key]

Instructions:

  1. Create tuples with different values and demonstrate tuple operations.
  2. Use dictionary methods to access values by key and handle missing keys.

Example Code:

# Tuple Operations
my_tuple = (1, 2, 3)

x, y, z = my_tuple
single_element_tuple = (3,)
x = 1, 2, 3

# Dictionary Operations
my_dict = {"key1": "value1", "key2": "value2"}
value = my_dict.get("key1")
default_value = my_dict.get("key3", "N/A")
direct_access = my_dict["key1"]

Section 6: String Operations

Concepts Covered:

  • String creation and formatting
  • String methods: split(), join()
  • Triple quote strings and escape characters

Instructions:

  1. Create strings using single quotes, double quotes, and triple quotes.
  2. Perform string splitting and joining operations.
  3. Format strings using f-strings and the format() method.
  4. Understand the usage of escape characters.

Example Code:

# String Operations
my_str = "Hello"
my_str_single_quote = 'World'
my_str_multi_line = """This is a multi-line
string."""


words = my_str.split(" ")
joined_string = "-".join(words)
formatted_string = f"Hello, {name}!"
formatted_string_alternative = "Hello, {}!".format(name)
escaped_string = "This string contains a \\\\"quote\\\\"."

Section 7: Variable Bindings

Concepts Covered:

  • Variable binding and scoping rules

Instructions:

  1. Demonstrate the behavior of variable bindings and scoping rules.

Example Code:

# Variable Bindings
a = 5
b = a
a = 6
print(b) # Output: 5

a = [1, 2, 3]
b = a
a = [4, 5, 6]
print(b) # Output: [1, 2, 3]

Section 8: Control Flow Statements

Concepts Covered:

  • Indentation and semicolon usage
  • Control flow statements: if-elif-else, while loop, for loop

Instructions:

  1. Utilize proper indentation and avoid unnecessary semicolons.
  2. Implement if-elif-else, while loop, and for loop based on the given instructions.

Example Code:

# Control Flow Statements
x = 10

if x > 10:
print("x is greater than 10")
elif x < 10:
print("x is less than 10")
else:
print("x is equal to 10")
counter = 0
while counter < 5:
print(counter)
counter += 1
for i in range(5):
print(i)

Section 9: Import Statements

Concepts Covered:

  • Importing modules and functions

Instructions:

  1. Import the necessary modules and functions as specified in the instructions.
  2. Use the imported modules and functions in the provided code snippets.

Example Code:

# Import Statements
from math import sqrt
import numpy as np
import math

result1 = sqrt(16)
result2 = np.array([1, 2, 3])
result3 = math.pi

Section 10: Functions

Concepts Covered:

  • Function definition and usage
  • Positional vs named parameters
  • Default parameters
  • Variable scoping rules
  • Return statement and multiple variable return
  • Avoiding global keyword, *args, and **kwargs
  • Docstring and None return value
  • Pass by object reference

Instructions:

  1. Implement the provided functions based on the given instructions.
  2. Pay attention to the parameter types and scoping rules.
  3. Handle return values and ensure the functions behave as expected.

Example Code:

# Functions

# Function with positional and named parameters
def greet(name, greeting="Hello"):
return f"{greeting}, {name}!"

# Function with variable scoping and object reference
def reassign(my_list):
my_list.append(2)
my_list = [0, 1]
my_list = [1, 2, 3]
new_list = reassign(my_list)
print(my_list) # Output: [1, 2, 3, 2]

def foo(a, b):
a[0] = 99
b = [7, 8]
a = [1, 2, 3]
b = [1, 2, 3]
foo(a, b)
print(a, b) # Output: [99, 2, 3] [1, 2, 3]

# Function with docstring and None return value
def add_numbers(a, b):
"""Adds two numbers and returns the sum."""
return a + b
def do_nothing():
"""Does nothing and returns None."""
pass
# Function with shallow copy and deep copy
import copy
grades = [[1, 2], [3, 4]]
grades2 = copy.deepcopy(grades)
grades2[0] = [5, 6]
grades2[1][1] = 5
print(grades) # Output: [[1, 2], [3, 4]]
print(grades2) # Output: [[5, 6], [3, 5]]
# Function with type hinting
def square(n: int) -> int:
return n ** 2

Section 11: f-strings, Formatting, and Iterators

Concepts Covered:

  • f-strings and formatting code
  • Iterators and next() function

Instructions:

  1. Use f-strings and formatting code to create formatted strings.
  2. Demonstrate the usage of iterators and the next() function.

Example Code:

# f-strings and Formatting
name = "Alice"
age = 25
formatted_string = f"My name is {name} and I am {age:.2f} years old."

# Iterators
it = iter([2, 3, 4, 5])
next_element = next(it)

Section 12: Testing with Doctest and Unit Testing

Concepts Covered:

  • Testing using doctest and unit testing frameworks

Instructions:

  1. Utilize doctest and unit testing frameworks to test the provided functions.
  2. Implement test cases based on the given instructions.

Example Code:

# Please visit doctest documentation page from the link in description
import doctest

def get_digits(number):
"""
Returns a list of individual digits in the number.
>>> get_digits(12345)
[1, 2, 3, 4, 5]
"""
return [int(digit) for digit in str(number)]

# Run doctest
doctest.testmod()
# Please visit unittest documentation page from the link in description

# Unit Testing with unittest
import unittest
class TestSomeName(unittest.TestCase):
def testSomething(self):
self.assertEqual(2 + 2, 4)
self.assertNotEqual(3 * 3, 7)

unittest.main()

# Run unit tests in notebook
unittest.main(argv=[""], exit=False)

Section 13: Student Grade Tracker

Concepts Covered:

  • Class definition and instantiation
  • Attributes and methods
  • Data types: dict, str

Instructions:

  1. Create a class named Student.
  2. Define the __init__ method that initializes the student's name and an empty dictionary to store grades.
  3. Implement the add_grade method that takes two parameters: subject (str) and grade (int). The method should add the grade for the given subject to the student's grades dictionary.
  4. Implement the get_average_grade method. It should calculate and return the average grade for the student based on the grades stored in the dictionary.

Example Code:

class Student:
def __init__(self, name):
self.name = name
self.grades = {}

def add_grade(self, subject, grade):
self.grades[subject] = grade

def get_average_grade(self):
if len(self.grades) == 0:
return 0
total_grades = sum(self.grades.values())
average_grade = total_grades / len(self.grades)
return average_grade

Section 14: Course Class

Concepts Covered:

  • Class definition and instantiation
  • Composition (class within a class)
  • Attributes and methods
  • Data types: list

Instructions:

  1. Create a class named Course.
  2. Define the __init__ method that initializes the course's name and an empty list to store students.
  3. Implement the add_student method that takes a Student object as a parameter and adds it to the course's list of students.
  4. Implement the get_student_average_grades method that calculates and returns a dictionary containing each student's name and average grade.

Example Code:

class Course:
def __init__(self, name):
self.name = name
self.students = []

def add_student(self, student):
self.students.append(student)

def get_student_average_grades(self):
average_grades = {}
for student in self.students:
average_grade = student.get_average_grade()
average_grades[student.name] = average_grade
return average_grades

Section 15: Testing the Code

Concepts Covered:

  • Creating objects
  • Method calls
  • Iterating over dictionaries

Instructions:

  1. Create instances of the Student class for different students and assign grades using the add_grade method.
  2. Create an instance of the Course class.
  3. Add the student instances to the course using the add_student method.
  4. Call the get_student_average_grades method on the course instance to obtain the average grades for each student.
  5. Display the students' names and their average grades.

Example Code:

# Creating students and assigning grades
student1 = Student("Alice")
student1.add_grade("Math", 90)
student1.add_grade("Science", 85)

student2 = Student("Bob")
student2.add_grade("Math", 75)
student2.add_grade("Science", 80)

# Creating a course and adding students
course = Course("Mathematics")
course.add_student(student1)
course.add_student(student2)

# Getting average grades and displaying them
average_grades = course.get_student_average_grades()
for student, average_grade in average_grades.items():
print(f"Average grade for {student}: {average_grade:.2f}")

Error Handling and Potential Issues:

  1. Empty grades dictionary: When calculating the average grade, handle the case where the student has no grades yet. Return 0 to avoid division by zero errors.
  2. Missing attributes or methods: Ensure that you have defined all the necessary attributes and methods in the Student and Course classes as specified in the instructions. Check for typos and missing indentations.
  3. Invalid input: Implement error handling for potential errors such as invalid grades or incorrect subject names. Use appropriate control flow statements (if-elif-else) to handle these cases gracefully and provide informative error messages to the user.
  4. Missing students in the course: When calculating the average grades for each student in the course, handle the case where the course has no students yet. Return an empty dictionary or an appropriate message to indicate this situation.

Conclusion:

Congratulations on completing the Comprehensive Python Concepts exercise project! You have covered a wide range of Python concepts, including basic data types, operators, data type conversion, list operations, tuple and dictionary operations, string operations, variable bindings, and implementing classes and methods for student grade tracking. This exercise project provides a solid foundation for understanding and applying these concepts in real-world Python programming. Keep practicing and exploring more Python concepts to enhance your skills further!

References:

Here are some useful references related to this article:

Use these resources to deepen your understanding of Python and improve your programming skills.

--

--

Bhimraj Yadav

Computer Engineer | Python, ML/DL and NextJs Developer