From f164fb6ff4f16ab3092f4675af201670ea40b2e3 Mon Sep 17 00:00:00 2001 From: Enyew Ashebir <154270403+Enyewashebr@users.noreply.github.com> Date: Mon, 15 Jul 2024 21:51:37 +0300 Subject: [PATCH] DONE --- README.md | 387 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 387 insertions(+) diff --git a/README.md b/README.md index 3e79495..ce9bd60 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=15417028&assignment_repo_type=AssignmentRepo) # SE-Assignment-6 Assignment: Introduction to Python Instructions: @@ -8,32 +10,417 @@ Answer the following questions based on your understanding of Python programming 1. Python Basics: - What is Python, and what are some of its key features that make it popular among developers? Provide examples of use cases where Python is particularly effective. + + Python is a high-level, interpreted, and general-purpose programming language. Some of its key features that make it popular among developers include: + +--- Easy to learn and read syntax, making it a great language for beginners. +--- Extensive standard library and vast ecosystem of third-party packages, providing a wide range of functionality. +--- Interpreted nature, allowing for quick feedback and rapid prototyping. +--- Versatility - Python can be used for web development, data analysis, machine learning, automation, and more. +Examples of use cases where Python is particularly effective include data science and analytics, scientific computing, web development, automation, and scripting. + 2. Installing Python: - Describe the steps to install Python on your operating system (Windows, macOS, or Linux). Include how to verify the installation and set up a virtual environment. + The steps to install Python on your operating system are as follows: + +Windows: + +Visit the official Python website (https://www.python.org/downloads/) and download the latest version of Python for Windows. +Run the installer and follow the on-screen instructions to complete the installation. +To verify the installation, open the Command Prompt and type python --version. You should see the installed version of Python. +To set up a virtual environment, use the venv module by running python -m venv my_env in the Command Prompt. This will create a new virtual environment named my_env. +macOS and Linux: + +Check if Python is already installed by opening the Terminal and typing python3 --version. If not, visit the Python website and download the latest version for your operating system. +Follow the on-screen instructions to install Python. +To verify the installation, open the Terminal and type python3 --version. +To set up a virtual environment, use the venv module by running python3 -m venv my_env in the Terminal. This will create a new virtual environment named my_env. +Python Syntax and Semantics: + +Here's a simple Python program that prints "Hello, World!" to the console: + +python +Copy +print("Hello, World!") +The key syntax elements used in this program are: + +print(): This is a built-in function in Python that outputs the specified message to the console. +"Hello, World!": This is a string literal, which is a sequence of characters enclosed in double quotes. + + + + 3. Python Syntax and Semantics: - Write a simple Python program that prints "Hello, World!" to the console. Explain the basic syntax elements used in the program. +print("Hello, World!") +The key syntax elements used in this program are: + +-- print(): This is a built-in Python function used to output or display data. +-- "Hello, World!": This is a string literal enclosed within double quotes. +When executed, this program will display the message "Hello, World!" in the console or terminal. + + + 4. Data Types and Variables: - List and describe the basic data types in Python. Write a short script that demonstrates how to create and use variables of different data types. + +Basic Data Types in Python: + +1. **Integer (int)**: Whole numbers, both positive and negative, without a decimal point. Examples: `42`, `-10`, `0`. +2. **Floating-point (float)**: Numbers with a decimal point. Examples: `3.14`, `-2.5`, `0.0`. +3. **Boolean (bool)**: Logical values that can be either `True` or `False`. +4. **String (str)**: Sequences of characters, enclosed in single quotes (`'`), double quotes (`"`), or triple quotes (`'''` or `"""`). Examples: `'Hello'`, `"World"`, `"""Python is fun!"""`. +5. **None**: A special data type representing the absence of a value. + +Here's a short script that demonstrates the creation and usage of variables with different data types: + +```python +# Integer +age = 25 +print("Age:", age) # Output: Age: 25 + +# Float +pi = 3.14159 +print("Pi:", pi) # Output: Pi: 3.14159 + +# Boolean +is_student = True +print("Is Student:", is_student) # Output: Is Student: True + +# String +name = "John Doe" +print("Name:", name) # Output: Name: John Doe + +# None +favorite_color = None +print("Favorite Color:", favorite_color) # Output: Favorite Color: None + +# Performing operations with different data types +sum_of_numbers = age + int(pi) +print("Sum of age and pi:", sum_of_numbers) # Output: Sum of age and pi: 28 +``` + +In this script, we create variables of different data types (integer, float, boolean, string, and None) and demonstrate how to access and use them. We also show how to perform operations between variables of different data types, such as adding an integer and a float. + +The key points to note are: + +- Variables in Python are declared without a specific data type, and the type is determined by the value assigned to the variable. +- Python automatically handles type conversions when performing operations between different data types (e.g., `int(pi)` converts the float `pi` to an integer). +- The `print()` function can be used to output the values of variables and perform string formatting. + +This script provides a basic understanding of how to work with the fundamental data types in Python. + + + 5. Control Structures: - Explain the use of conditional statements and loops in Python. Provide examples of an `if-else` statement and a `for` loop. + +1. **Conditional Statements (if-else):** +Conditional statements are used to execute different blocks of code based on the evaluation of a condition. The most common form is the `if-else` statement: + +```python +age = 18 +if age >= 18: + print("You are an adult.") +else: + print("You are a minor.") +``` + + +2. **Loops (for):** +Loops are used to repeatedly execute a block of code. The `for` loop is commonly used to iterate over a sequence (such as a list, tuple, or string): +~~~python +fruits = ["apple", "banana", "cherry"] +for fruit in fruits: + print("I like", fruit) + + + +for i in range(5): + print(i) + + + + + + 6. Functions in Python: - What are functions in Python, and why are they useful? Write a Python function that takes two arguments and returns their sum. Include an example of how to call this function. + + In Python, functions are reusable blocks of code that perform a specific task. They are useful for the following reasons: + +1. **Code Reuse**: Functions allow you to write code once and use it multiple times throughout your program, reducing code duplication and making your code more maintainable. +2. **Modularity**: Functions help you break down your program into smaller, more manageable pieces, making it easier to understand, debug, and test. +3. **Abstraction**: Functions hide the implementation details, allowing you to focus on what the function does rather than how it does it. +4. **Parameterization**: Functions can accept input parameters, making them more versatile and allowing you to write generic, reusable code. +5. **Code Organization**: Functions help you organize your code into logical, self-contained units, improving the overall structure and readability of your program. + +Here's an example of a Python function that takes two arguments and returns their sum: + +```python +def add_numbers(a, b): + """ + Adds two numbers and returns the result. + + Args: + a (int or float): The first number to add. + b (int or float): The second number to add. + + Returns: + int or float: The sum of the two numbers. + """ + result = a + b + return result +``` + +To call this function, you can use the following syntax: + +```python +result = add_numbers(5, 3) +print(result) # Output: 8 + +another_result = add_numbers(10.5, 2.7) +print(another_result) # Output: 13.2 +``` + +In this example, the `add_numbers()` function takes two arguments, `a` and `b`, which can be either integers or floats. The function calculates the sum of the two numbers and returns the result. + +The function also includes a docstring, which is a string enclosed in triple quotes ("") that provides a brief description of the function, its parameters, and its return value. This documentation can be accessed using the built-in `help()` function or by examining the function's `__doc__` attribute. + +When you call the `add_numbers()` function, you pass the two numbers you want to add as arguments, and the function returns the sum, which can be stored in a variable and used further in your program. + +Functions are a powerful tool in Python, as they allow you to write modular, reusable, and maintainable code. By encapsulating specific tasks or operations within functions, you can create code that is easier to understand, debug, and extend over time. + + + 7. Lists and Dictionaries: - Describe the differences between lists and dictionaries in Python. Write a script that creates a list of numbers and a dictionary with some key-value pairs, then demonstrates basic operations on both. + In Python, lists and dictionaries are two distinct data structures with different characteristics and use cases. + +**Lists**: +- Lists are ordered collections of elements, where each element has an index (a numerical position) within the list. +- Lists can contain elements of different data types (e.g., integers, floats, strings, other data structures). +- Lists are defined using square brackets `[]`, and elements are separated by commas. +- Lists support various operations, such as indexing, slicing, appending, removing, and sorting. + +**Dictionaries**: +- Dictionaries are unordered collections of key-value pairs, where each key is unique and maps to a corresponding value. +- Dictionaries are commonly used to store and retrieve data by associating a unique identifier (the key) with a value. +- Dictionaries are defined using curly braces `{}`, and each key-value pair is separated by a colon. +- Keys in a dictionary can be of various data types (e.g., strings, integers, tuples), but they must be unique. +- Dictionaries support operations like accessing, adding, modifying, and removing key-value pairs. + +Here's a script that demonstrates the usage of lists and dictionaries in Python: + +```python +# List of numbers +numbers = [5, 2, 9, 1, 7] + +# Accessing and manipulating the list +print("List of numbers:", numbers) +print("Second element:", numbers[1]) +numbers.append(3) +print("List after appending:", numbers) +numbers.sort() +print("Sorted list:", numbers) + +# Dictionary with key-value pairs +person = { + "name": "John Doe", + "age": 35, + "occupation": "Software Engineer" +} + +# Accessing and manipulating the dictionary +print("\nDictionary of person details:", person) +print("Person's name:", person["name"]) +person["age"] = 36 +print("Person's updated age:", person["age"]) +person["hobbies"] = ["reading", "hiking"] +print("Person's details:", person) +``` + +Output: +``` +List of numbers: [5, 2, 9, 1, 7] +Second element: 2 +List after appending: [5, 2, 9, 1, 7, 3] +Sorted list: [1, 2, 3, 5, 7, 9] + +Dictionary of person details: {'name': 'John Doe', 'age': 35, 'occupation': 'Software Engineer'} +Person's name: John Doe +Person's updated age: 36 +Person's details: {'name': 'John Doe', 'age': 36, 'occupation': 'Software Engineer', 'hobbies': ['reading', 'hiking']} +``` + +In this script, we first create a list of numbers and demonstrate common list operations, such as accessing an element, appending a new element, and sorting the list. + +Next, we create a dictionary that stores the details of a person, including their name, age, and occupation. We then access the values in the dictionary, update the age, and add a new key-value pair for the person's hobbies. + +Lists and dictionaries are both powerful data structures in Python, but they serve different purposes. Lists are best suited for ordered collections of data, while dictionaries are more appropriate for storing and retrieving data by unique keys. + 8. Exception Handling: - What is exception handling in Python? Provide an example of how to use `try`, `except`, and `finally` blocks to handle errors in a Python script. +Exception handling in Python is a mechanism that allows you to handle and manage unexpected or exceptional situations that may occur during the execution of a program. When an exception occurs, it can disrupt the normal flow of the program, and without proper handling, it can lead to program termination or unexpected behavior. + +The `try`, `except`, and `finally` blocks are the core components of exception handling in Python: + +1. **try**: The `try` block contains the code that might raise an exception. If an exception is raised within the `try` block, the program will immediately jump to the corresponding `except` block. + +2. **except**: The `except` block is used to catch and handle specific types of exceptions. You can have multiple `except` blocks to handle different types of exceptions. + +3. **finally**: The `finally` block is optional and is used to execute a set of statements regardless of whether an exception was raised or not. This is often used for cleanup tasks, such as closing files or releasing resources. + +Here's an example that demonstrates how to use `try`, `except`, and `finally` blocks to handle errors in a Python script: + +```python +def divide_numbers(a, b): + try: + result = a / b + print(f"The result of {a} / {b} is: {result}") + except ZeroDivisionError: + print("Error: Division by zero is not allowed.") + except TypeError: + print("Error: Both arguments must be numbers.") + finally: + print("The operation has completed.") + +# Example usage +divide_numbers(10, 2) # Output: The result of 10 / 2 is: 5.0, The operation has completed. +divide_numbers(10, 0) # Output: Error: Division by zero is not allowed., The operation has completed. +divide_numbers(10, "2") # Output: Error: Both arguments must be numbers., The operation has completed. +``` + +In this example, the `divide_numbers()` function takes two arguments, `a` and `b`, and performs a division operation. The `try` block contains the division logic, which might raise a `ZeroDivisionError` if `b` is zero or a `TypeError` if the arguments are not numbers. + +The `except` blocks are used to catch and handle these specific exceptions. If a `ZeroDivisionError` or `TypeError` is raised, the corresponding `except` block will execute, and an error message will be printed. + +The `finally` block is executed regardless of whether an exception was raised or not. In this case, it prints a message indicating that the operation has completed. + +By using exception handling, you can ensure that your program can gracefully handle unexpected situations and provide meaningful error messages to the user, rather than crashing or producing unexpected results. + +Exception handling is an important aspect of writing robust and reliable Python code, as it allows you to anticipate and manage potential problems that may arise during program execution. 9. Modules and Packages: - Explain the concepts of modules and packages in Python. How can you import and use a module in your script? Provide an example using the `math` module. +In Python, modules and packages are fundamental concepts that help organize and reuse code. + +**Modules**: +- A module is a single Python file that contains definitions (such as functions, classes, and variables) that can be used in other Python scripts. +- Modules allow you to break down your code into smaller, more manageable pieces, making it easier to maintain and organize. +- When you create a Python file, it automatically becomes a module that can be imported and used in other parts of your code. + +**Packages**: +- A package is a collection of related modules that are organized into a hierarchical structure. +- Packages provide a way to group and namespace related modules, making it easier to manage and distribute them. +- Packages are represented as directories containing one or more Python files (modules). +- The `__init__.py` file, which can be empty, is used to define a directory as a package. + +To import and use a module in your Python script, you can use the `import` statement. Here's an example using the built-in `math` module: + +```python +# Importing the entire math module +import math + +# Using functions from the math module +result = math.sqrt(16) +print(f"The square root of 16 is: {result}") + +# Accessing a constant from the math module +pi_value = math.pi +print(f"The value of pi is: {pi_value}") + +# Importing specific functions from the math module +from math import log, exp + +# Using the imported functions directly +log_result = log(10) +exp_result = exp(2) +print(f"The natural logarithm of 10 is: {log_result}") +print(f"e^2 is: {exp_result}") +``` + +Output: +``` +The square root of 16 is: 4.0 +The value of pi is: 3.141592653589793 +The natural logarithm of 10 is: 2.302585092994046 +e^2 is: 7.38905609893065 +``` + +In this example, we first import the entire `math` module using the `import math` statement. This allows us to access the module's functions and constants using the dot notation, such as `math.sqrt()` and `math.pi`. + +Next, we demonstrate importing specific functions from the `math` module using the `from math import log, exp` statement. This allows us to use the `log()` and `exp()` functions directly, without needing to prefix them with the module name. + +Modules provide a way to organize and reuse code, making it easier to manage and distribute your Python applications. By importing and using modules, you can leverage the functionality provided by the standard library or third-party libraries, saving you time and effort in implementing common tasks or algorithms. + +When working with larger projects, packages can further enhance code organization by grouping related modules together, providing a hierarchical structure and a way to create namespaced imports. + + 10. File I/O: - How do you read from and write to files in Python? Write a script that reads the content of a file and prints it to the console, and another script that writes a list of strings to a file. +In Python, you can read from and write to files using the built-in `open()` function. Here's how you can do it: + +**Reading from a File**: + +```python +# Open the file in read mode +with open('example.txt', 'r') as file: + # Read the contents of the file + content = file.read() + +# Print the file contents +print(content) +``` + +In this example, we use the `open()` function to open the file named `'example.txt'` in read mode (`'r'`). The `with` statement ensures that the file is properly closed after we're done with it, even if an exception occurs. + +The `file.read()` method reads the entire contents of the file and assigns it to the `content` variable. Finally, we print the file contents to the console. + +**Writing to a File**: + +```python +# Create a list of strings to write to the file +data = ['Hello, world!', 'This is line 2.', 'And this is line 3.'] + +# Open the file in write mode +with open('output.txt', 'w') as file: + # Write each string to the file, separated by a newline + for line in data: + file.write(line + '\n') +``` + +In this example, we first create a list of strings called `data`. Then, we use the `open()` function to open a file named `'output.txt'` in write mode (`'w'`). The `with` statement ensures that the file is properly closed after we're done writing to it. + +Inside the `with` block, we iterate over the `data` list and write each string to the file, followed by a newline character (`'\n'`) to separate the lines. + +After running this script, a new file named `'output.txt'` will be created (or overwritten if it already exists) with the following contents: + +``` +Hello, world! +This is line 2. +And this is line 3. +``` + +Here are a few key points about reading from and writing to files in Python: + +- The `open()` function takes two arguments: the file name and the mode. Common modes are `'r'` for reading, `'w'` for writing, and `'a'` for appending. +- The `with` statement is recommended to ensure proper file handling and automatic file closure, even if an exception occurs. +- When reading from a file, you can also use `file.readline()` to read one line at a time or `file.readlines()` to read all lines into a list. +- When writing to a file, you can use `file.write()` to write a string or `file.writelines()` to write a list of strings. + +File handling is an essential skill in Python, as it allows you to persist data, read configuration settings, and interact with various file-based resources. + + + + # Submission Guidelines: - Your answers should be well-structured, concise, and to the point.