diff --git a/README.md b/README.md index 3e79495..cc6ca1c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +[![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-22041afd0340ce965d47ae6ef1cefeee28c7c493a6346c4f15d667ab976d596c.svg)](https://classroom.github.com/a/WfNmjXUk) +[![Open in Visual Studio Code](https://classroom.github.com/assets/open-in-vscode-2e0aaae1b6195c2367325f4f02e2d04e9abb55f0b24a779b69b11b9e10269abc.svg)](https://classroom.github.com/online_ide?assignment_repo_id=15380803&assignment_repo_type=AssignmentRepo) # SE-Assignment-6 Assignment: Introduction to Python Instructions: @@ -42,3 +44,638 @@ Answer the following questions based on your understanding of Python programming - Submit your completed assignment by [due date]. + + Python is an open-source, object-oriented, high-level, general-purpose programming language that has gained immense popularity for several reasons1. Here are some key features that contribute to its widespread adoption: +1. Intuitive Syntax: Python’s syntax resembles natural English, making it easy to learn and read. For beginners, this human-friendly approach simplifies writing and debugging code. +2. Extensive Standard Library: Python comes with a rich standard library that covers a wide range of functionalities. Developers can leverage built-in modules for tasks like file I/O, regular expressions, and networking. +3. Additional Libraries and Modules: Python offers an extensive ecosystem of third-party libraries and packages. For example, NumPy (for numerical computing), pandas (for data manipulation), and Matplotlib (for data visualization) enhance Python’s capabilities. +4. Community Support: Python has a large and active community. This support network contributes to continuous development, bug fixes, and the creation of new tools and libraries. +5. General Purpose: Python can be used for various applications, from web development to scientific computing. Let’s explore some specific use cases: +o Data Analysis: Python excels in data science, data engineering, and analytics. Libraries like pandas, NumPy, and SciPy facilitate data manipulation, exploration, and statistical analysis. +o Machine Learning and AI: Python is the go-to language for machine learning and artificial intelligence. Frameworks like TensorFlow, PyTorch, and scikit-learn empower developers to build and train models efficiently. +o Web Development: Django and Flask are popular Python frameworks for web development. They simplify building robust, scalable web applications. +o Scripting and Automation: Python’s concise syntax makes it ideal for writing scripts and automating repetitive tasks. +o Scientific Computing: Researchers and scientists use Python for simulations, numerical modeling, and solving complex mathematical problems. +o Internet of Things (IoT): Python is used to develop IoT applications due to its lightweight nature and compatibility with microcontrollers. +o Legacy App Modernization: Python helps modernize legacy systems by integrating new features or migrating to more efficient platforms. + + +Here's a detailed guide on how to install Python on Windows, macOS, and Linux, including verification and virtual environment setup. +Windows +Step 1: Download Python Installer +1. Go to the official Python website. +2. Click on the download link for the latest version of Python for Windows. +Step 2: Run the Installer +1. Run the downloaded installer. +2. Check the box that says "Add Python to PATH". +3. Select "Install Now". +Step 3: Verify Installation +1. Open Command Prompt (type cmd in the search bar and press Enter). +2. Type python --version and press Enter. You should see the Python version number. +3. Type pip --version to check if pip, the Python package installer, is installed. +Step 4: Set Up a Virtual Environment +1. Navigate to the desired project directory using Command Prompt. +2. Type python -m venv myenv to create a virtual environment named myenv. +3. Activate the virtual environment by typing myenv\Scripts\activate. + + +Here's a simple Python program that prints "Hello, World!" to the console: + + +print("Hello, World!") +``` + +### Explanation of Basic Syntax Elements + +1. **`print` Function**: + - `print` is a built-in Python function that outputs data to the console. + - The function takes one or more arguments (data to be printed), which can be strings, numbers, or other types of data. + +2. **Strings**: + - `"Hello, World!"` is a string, a sequence of characters enclosed in double quotes (`"`). Strings can also be enclosed in single quotes (`'`). + +3. **Parentheses `()`**: + - The parentheses `()` after `print` indicate that `print` is a function call. + - The data to be printed is passed as an argument inside the parentheses. + +4. **Double Quotes `"`**: + - Double quotes are used to denote the beginning and end of a string literal. Python also allows the use of single quotes (`'`) for the same purpose. + +### Additional Notes + +- **Comments**: You can add comments to your Python code using the `#` symbol. Anything written after `#` on the same line is ignored by the Python interpreter. + ```python + # This is a comment + print("Hello, World!") # This prints Hello, World! to the console + ``` + +- **Indentation**: Python uses indentation (spaces or tabs) to define the structure of the code. While indentation is not needed in this simple example, it is crucial for defining blocks of code such as loops and functions. + + + +This program demonstrates the basic structure and syntax of Python code, providing a foundation for more complex programs. + + +### Basic Data Types in Python + +1. **Integers (`int`)**: Whole numbers, positive or negative, without a decimal point. + ```python + age = 25 + ``` + +2. **Floating-Point Numbers (`float`)**: Numbers that contain a decimal point. + ```python + price = 19.99 + ``` + +3. **Strings (`str`)**: Sequences of characters enclosed in single, double, or triple quotes. + ```python + greeting = "Hello, World!" + ``` + +4. **Booleans (`bool`)**: Represents one of two values: `True` or `False`. + ```python + is_student = True + ``` + +5. **Lists (`list`)**: Ordered, mutable collections of items (can be of different types). + ```python + numbers = [1, 2, 3, 4, 5] + ``` + +6. **Tuples (`tuple`)**: Ordered, immutable collections of items. + ```python + coordinates = (10.0, 20.0) + ``` + +7. **Dictionaries (`dict`)**: Unordered, mutable collections of key-value pairs. + ```python + person = {"name": "Alice", "age": 30} + ``` + +8. **Sets (`set`)**: Unordered collections of unique items. + ```python + unique_numbers = {1, 2, 3, 4, 5} + ``` + +### Script Demonstrating Variables of Different Data Types + +```python +# Integer +age = 25 +print("Age:", age, "Type:", type(age)) + +# Float +price = 19.99 +print("Price:", price, "Type:", type(price)) + +# String +greeting = "Hello, World!" +print("Greeting:", greeting, "Type:", type(greeting)) + +# Boolean +is_student = True +print("Is student:", is_student, "Type:", type(is_student)) + +# List +numbers = [1, 2, 3, 4, 5] +print("Numbers:", numbers, "Type:", type(numbers)) + +# Tuple +coordinates = (10.0, 20.0) +print("Coordinates:", coordinates, "Type:", type(coordinates)) + +# Dictionary +person = {"name": "Alice", "age": 30} +print("Person:", person, "Type:", type(person)) + +# Set +unique_numbers = {1, 2, 3, 4, 5} +print("Unique numbers:", unique_numbers, "Type:", type(unique_numbers)) +``` + +### Explanation of the Script + +- **Integer Variable**: `age` is assigned an integer value and printed along with its type. +- **Float Variable**: `price` is assigned a floating-point value and printed along with its type. +- **String Variable**: `greeting` is assigned a string value and printed along with its type. +- **Boolean Variable**: `is_student` is assigned a boolean value and printed along with its type. +- **List Variable**: `numbers` is assigned a list of integers and printed along with its type. +- **Tuple Variable**: `coordinates` is assigned a tuple of floats and printed along with its type. +- **Dictionary Variable**: `person` is assigned a dictionary with string keys and values of different types and printed along with its type. +- **Set Variable**: `unique_numbers` is assigned a set of integers and printed along with its type. + +This script showcases the creation and use of variables of various data types in Python, demonstrating how to define and print variables along with their types. + + + + + + + + + +### Conditional Statements + +Conditional statements allow you to execute specific blocks of code based on certain conditions. In Python, the primary conditional statements are `if`, `elif`, and `else`. + +#### Example of an if-else Statement + +```python +age = 20 + +if age >= 18: + print("You are an adult.") +else: + print("You are a minor.") +``` + +### Explanation: +- **`if` Statement**: Checks if the condition `age >= 18` is `True`. If it is, the code block under the `if` statement is executed. +- **`else` Statement**: If the `if` condition is `False`, the code block under the `else` statement is executed. + +You can also use the `elif` (else if) statement to check multiple conditions: + +```python +age = 20 + +if age < 18: + print("You are a minor.") +elif age < 65: + print("You are an adult.") +else: + print("You are a senior.") +``` + +### Explanation: +- **`elif` Statement**: Checks another condition if the previous `if` condition is `False`. +- **`else` Statement**: Executes if none of the previous conditions are `True`. + +### Loops + +Loops allow you to execute a block of code multiple times. Python supports `for` and `while` loops. + +#### Example of a for Loop + +```python +fruits = ["apple", "banana", "cherry"] + +for fruit in fruits: + print(fruit) +``` + +### Explanation: +- **`for` Loop**: Iterates over each item in the `fruits` list. +- **`fruit` Variable**: Takes the value of each item in the list on each iteration. +- **`print(fruit)`**: Prints the current item. + +You can also use the `range()` function with a `for` loop to iterate over a sequence of numbers: + +```python +for i in range(5): + print(i) +``` + +### Explanation: +- **`range(5)`**: Generates a sequence of numbers from 0 to 4. +- **`for i in range(5)`**: Iterates over each number in the sequence. +- **`print(i)`**: Prints the current number. + +#### Example of a while Loop + +```python +count = 0 + +while count < 5: + print(count) + count += 1 +``` + +### Explanation: +- **`while` Loop**: Continues to execute as long as the condition `count < 5` is `True`. +- **`count += 1`**: Increments the value of `count` by 1 in each iteration to eventually break the loop when `count` is no longer less than 5. + +These examples demonstrate the use of conditional statements and loops in Python, allowing for decision-making and repetitive tasks in your programs. + + + + + + + + + + + +### Functions in Python + +Functions are reusable blocks of code that perform a specific task. They are useful because they help organize code, make it more readable, and avoid repetition. Functions allow for easier maintenance and debugging and enable you to reuse code across different parts of a program or in different programs. + +### Defining a Function + +In Python, functions are defined using the `def` keyword, followed by the function name, parentheses (which may include parameters), and a colon. The function body is indented. + +### Example Function: Sum of Two Arguments + +Here’s a simple Python function that takes two arguments and returns their sum: + +```python +def add_numbers(a, b): + """Returns the sum of a and b.""" + return a + b +``` + +### Explanation: +- **`def`**: Keyword used to define a function. +- **`add_numbers`**: The name of the function. +- **`(a, b)`**: The parameters of the function. These are the inputs to the function. +- **`"""Returns the sum of a and b."""`**: A docstring that describes what the function does. +- **`return a + b`**: The function body that calculates the sum of `a` and `b` and returns the result. + +### Calling the Function + +You call a function by using its name followed by parentheses, including any necessary arguments. + +#### Example of Calling the Function + +```python +# Call the add_numbers function with arguments 5 and 3 +result = add_numbers(5, 3) + +# Print the result +print("The sum is:", result) +``` + +### Explanation: +- **`add_numbers(5, 3)`**: Calls the `add_numbers` function with arguments `5` and `3`. +- **`result`**: Stores the returned value from the function. +- **`print("The sum is:", result)`**: Prints the result, which is the sum of `5` and `3`. + +### Full Example + +Here is the complete code for defining the function and calling it: + +```python +def add_numbers(a, b): + """Returns the sum of a and b.""" + return a + b + +# Call the add_numbers function +result = add_numbers(5, 3) + +# Print the result +print("The sum is:", result) +``` + +### Output + +``` +The sum is: 8 +``` +7) Differences Between Lists and Dictionaries in Python + +**Lists**: +- Ordered collections of items. +- Items are accessed by their index (position) in the list. +- Lists are mutable, meaning you can change their contents after creation. +- Lists can contain duplicate elements. +- Syntax: Items are enclosed in square brackets `[]`. + +**Dictionaries**: +- Unordered collections of key-value pairs. +- Items are accessed by their keys, which must be unique. +- Dictionaries are mutable. +- Keys in dictionaries must be of an immutable type (e.g., strings, numbers, tuples). +- Syntax: Key-value pairs are enclosed in curly braces `{}` with a colon `:` separating keys and values. + +### Script Demonstrating Basic Operations on Lists and Dictionaries +```python +# Create a list of numbers +numbers = [1, 2, 3, 4, 5] +# Create a dictionary with some key-value pairs +person = { + "name": "Alice", + "age": 30, + "city": "New York" +} +# Basic operations on the list +print("Original list:", numbers) + +# Append an item to the list +numbers.append(6) +print("After appending 6:", numbers) + +# Remove an item from the list +numbers.remove(3) +print("After removing 3:", numbers) + +# Access an item by index +second_item = numbers[1] +print("Second item:", second_item) + +# Basic operations on the dictionary +print("Original dictionary:", person) + +# Add a new key-value pair to the dictionary +person["email"] = "alice@example.com" +print("After adding email:", person) + +# Update an existing key-value pair +person["age"] = 31 +print("After updating age:", person) + +# Access a value by key +name = person["name"] +print("Name:", name) + +# Remove a key-value pair +del person["city"] +print("After removing city:", person) +List Operations +1. **Creating a List**: `numbers = [1, 2, 3, 4, 5]` creates a list with five numbers. +2. **Printing the Original List**: `print("Original list:", numbers)` prints the original list. +3. **Appending an Item**: `numbers.append(6)` adds the number `6` to the end of the list. +4. **Removing an Item**: `numbers.remove(3)` removes the first occurrence of the number `3` from the list. +5. **Accessing an Item by Index**: `second_item = numbers[1]` retrieves the second item (index `1`) from the list. + +#### Dictionary Operations + +1. **Creating a Dictionary**: `person = {"name": "Alice", "age": 30, "city": "New York"}` creates a dictionary with three key-value pairs. +2. **Printing the Original Dictionary**: `print("Original dictionary:", person)` prints the original dictionary. +3. **Adding a New Key-Value Pair**: `person["email"] = "alice@example.com"` adds a new key `email` with the value `alice@example.com`. +4. **Updating an Existing Key-Value Pair**: `person["age"] = 31` updates the value associated with the key `age` to `31`. +5. **Accessing a Value by Key**: `name = person["name"]` retrieves the value associated with the key `name`. +6. **Removing a Key-Value Pair**: `del person["city"]` removes the key `city` and its associated value from the dictionary. + +### Output +Original list: [1, 2, 3, 4, 5] +After appending 6: [1, 2, 3, 4, 5, 6] +After removing 3: [1, 2, 4, 5, 6] +Second item: 2 +Original dictionary: {'name': 'Alice', 'age': 30, 'city': 'New York'} +After adding email: {'name': 'Alice', 'age': 30, 'city': 'New York', 'email': 'alice@example.com'} +After updating age: {'name': 'Alice', 'age': 31, 'city': 'New York', 'email': 'alice@example.com'} +Name: Alice +After removing city: {'name': 'Alice', 'age': 31, 'email': 'alice@example.com'} +8) +### Exception Handling in Python + +Exception handling in Python is a mechanism to handle runtime errors gracefully without crashing the program. It allows you to respond to exceptional conditions (such as file not found, division by zero, etc.) in a controlled way. + +### Keywords Used in Exception Handling + +- **`try`**: The block of code to be monitored for exceptions. +- **`except`**: The block of code that executes if an exception occurs. +- **`finally`**: The block of code that executes no matter what, whether an exception occurs or not. + +### Example of Using `try`, `except`, and `finally` + +Here's an example that demonstrates how to use these blocks to handle errors: + +```python +def divide_numbers(a, b): + try: + # Code that might raise an exception + result = a / b + except ZeroDivisionError: + # Code that executes if a ZeroDivisionError occurs + print("Error: Cannot divide by zero.") + result = None + except TypeError: + # Code that executes if a TypeError occurs + print("Error: Invalid input type. Please provide numbers.") + result = None + else: + # Code that executes if no exception occurs + print("Division successful.") + finally: + # Code that always executes + print("Execution complete.") + return result + +# Examples of calling the function +print("Result:", divide_numbers(10, 2)) # Valid division +print("Result:", divide_numbers(10, 0)) # Division by zero +print("Result:", divide_numbers(10, 'a')) # Invalid input type +``` + +### Explanation: + +1. **`try` Block**: + - Contains code that might raise an exception. Here, `result = a / b` could potentially cause a `ZeroDivisionError` or `TypeError`. + +2. **`except` Blocks**: + - Handle specific exceptions that might occur in the `try` block. + - **`ZeroDivisionError`**: Catches the error when attempting to divide by zero and prints an error message. + - **`TypeError`**: Catches the error when the inputs are not of the correct type (e.g., one or both inputs are not numbers) and prints an error message. + +3. **`else` Block**: + - Executes if no exceptions occur in the `try` block. + - Prints a success message. + +4. **`finally` Block**: + - Executes no matter what, even if an exception is raised or not. + - Prints a message indicating that the execution is complete. + +### Output: + +``` +Division successful. +Execution complete. +Result: 5.0 +Error: Cannot divide by zero. +Execution complete. +Result: None +Error: Invalid input type. Please provide numbers. +Execution complete. +Result: None +``` + +### Explanation of the Output: + +1. **First Call**: `divide_numbers(10, 2)` + - Valid division, prints "Division successful." and "Execution complete.", returns `5.0`. + +2. **Second Call**: `divide_numbers(10, 0)` + - Division by zero, prints "Error: Cannot divide by zero." and "Execution complete.", returns `None`. + +3. **Third Call**: `divide_numbers(10, 'a')` + - Invalid input type, prints "Error: Invalid input type. Please provide numbers." and "Execution complete.", returns `None`. + +9) +### Modules and Packages in Python +#### Modules +A module in Python is a file containing Python definitions and statements. It can define functions, classes, and variables, and can include runnable code. Modules help organize and reuse code across different parts of a program or across different programs. +- **Creating a Module**: Any Python file can be considered a module. For example, a file named `mymodule.py` is a module that can be imported into other Python scripts. +- **Importing a Module**: You can import a module using the `import` statement. +#### Packages +A package is a way of organizing related modules into a directory hierarchy. A package is a directory that contains a special `__init__.py` file (which can be empty), signaling to Python that this directory should be treated as a package. +- **Creating a Package**: A package is a directory that contains multiple module files and an `__init__.py` file. +- **Importing a Package**: You can import modules from a package using the `import` statement and dot notation. +### Importing and Using a Module +You can import a module using the `import` statement, and then use the functions, classes, and variables defined in that module. +#### Example Using the `math` Module +The `math` module is a built-in Python module that provides mathematical functions. +```python +import math +# Use the math module to calculate the square root of a number +number = 16 +sqrt_result = math.sqrt(number) +print(f"The square root of {number} is {sqrt_result}") +# Use the math module to calculate the sine of a number (in radians) +angle = math.pi / 2 +sine_result = math.sin(angle) +print(f"The sine of {angle} radians is {sine_result}") +# Use the math module to get the value of pi +pi_value = math.pi +print(f"The value of pi is {pi_value}") +### Explanation: +1. **`import math`**: Imports the `math` module, making its functions and constants available in the script. +2. **`math.sqrt(number)`**: Uses the `sqrt` function from the `math` module to calculate the square root of `number`. +3. **`math.sin(angle)`**: Uses the `sin` function from the `math` module to calculate the sine of `angle` (in radians). +4. **`math.pi`**: Accesses the constant `pi` from the `math` module. +### Output: +The square root of 16 is 4.0 +The sine of 1.5707963267948966 radians is 1.0 +The value of pi is 3.141592653589793 +### Advanced Import Techniques +- **Importing Specific Functions**: You can import specific functions from a module using the `from` keyword. + ```python + from math import sqrt, sin, pi + print(sqrt(16)) + print(sin(pi / 2)) + print(pi) + ``` +- **Renaming Modules**: You can rename a module while importing it using the `as` keyword. + ```python + import math as m + print(m.sqrt(16)) + print(m.sin(m.pi / 2)) + print(m.pi) + ``` +### Using Custom Modules and Packages +#### Custom Module Example +1. Create a file named `mymodule.py` with the following content: + ```python + def greet(name): + return f"Hello, {name}!" +2. Use the custom module in another script: + ```python + import mymodule + print(mymodule.greet("Alice")) +#### Custom Package Example +1. Create a directory structure for the package: + mypackage/ + __init__.py + module1.py + module2.py +2. Add content to `module1.py`: + ```python + def func1(): + return "Function 1 from module 1" +3. Add content to `module2.py`: + ```python + def func2(): + return "Function 2 from module 2" +4. Use the custom package in another script: + ```python + from mypackage import module1, module2 + print(module1.func1()) + print(module2.func2()) + + +10) +### Reading from and Writing to Files in Python +Python provides built-in functions for file handling, making it easy to read from and write to files. The primary functions used for file operations are `open()`, `read()`, `write()`, and `close()`. You can also use the `with` statement to handle files, which ensures proper resource management and automatically closes the file after the block of code is executed. +### Reading from a File +To read the contents of a file, you can use the `open()` function in read mode (`'r'`). +#### Script to Read the Content of a File +```python +# Reading the content of a file and printing it to the console +def read_file(file_path): + try: + with open(file_path, 'r') as file: + content = file.read() + print(content) + except FileNotFoundError: + print(f"The file at {file_path} was not found.") + except IOError: + print(f"An error occurred while reading the file at {file_path}.") +# Example usage +read_file('example.txt') +``` +### Writing to a File +To write to a file, you can use the `open()` function in write mode (`'w'`). If the file does not exist, it will be created. If it exists, its contents will be overwritten. For appending to a file, use append mode (`'a'`). +### Script to Write a List of Strings to a File +```python +# Writing a list of strings to a file +def write_to_file(file_path, lines): + try: + with open(file_path, 'w') as file: + for line in lines: + file.write(line + '\n') + except IOError: + print(f"An error occurred while writing to the file at {file_path}.") +# Example usage +lines_to_write = [ + "This is the first line.", + "This is the second line.", + "This is the third line." +] +write_to_file('output.txt', lines_to_write) +### Explanation +1. **Reading from a File**: + - **`open(file_path, 'r')`**: Opens the file in read mode. + - **`with` statement**: Ensures that the file is properly closed after the block of code is executed. + - **`file.read()`**: Reads the entire content of the file. + - **Exception Handling**: Catches `FileNotFoundError` if the file does not exist and `IOError` for any other I/O errors. +2. **Writing to a File**: + - **`open(file_path, 'w')`**: Opens the file in write mode. + - **`with` statement**: Ensures that the file is properly closed after the block of code is executed. + - **`file.write(line + '\n')`**: Writes each line to the file, adding a newline character at the end. + - **Exception Handling**: Catches `IOError` for any I/O errors during writing. +### Example Files +- **Reading**: Ensure you have a file named `example.txt` with some content in the same directory as your script. +- **Writing**: The script will create or overwrite a file named `output.txt` in the same directory as your script with the provided list of strings. + +