diff --git a/README.md b/README.md index 3e79495..cd31f41 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=15365031&assignment_repo_type=AssignmentRepo) # SE-Assignment-6 Assignment: Introduction to Python Instructions: @@ -7,33 +9,328 @@ 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 versatile, high-level programming language known for its simplicity and readability. Key features include: + +1. Easy to learn syntax, suitable for beginners +2. Dynamic typing for rapid prototyping +3. Extensive standard library and ecosystem of libraries +4. Cross-platform compatibility + +Python excels in use cases like: + +1. Data science and machine learning +2. Web development with frameworks like Django +3. Automation and scripting +4. Scientific and numerical computing +5. Rapid prototyping + 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. +1. Download the latest Python installer from the official website (python.org). +2. Run the installer and select "Add Python to PATH" during installation. +3. Open the Command Prompt and type python --version to verify the installation. + + 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. +- 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 basic syntax elements used in the program. + +1. print(): This is a built-in Python function that outputs the specified message to the console or terminal. The message is enclosed within parentheses. +2. "Hello, World!": This is a string literal, which is a sequence of characters enclosed within double quotes. In Python, strings can be enclosed within single quotes ('), double quotes ("), or triple quotes (''' or """). + 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. +1. Numeric Types: +Integers (int): Whole numbers, e.g., 42, -10. +Floating-point Numbers (float): Numbers with decimal points, e.g., 3.14, -2.5. +Complex Numbers (complex): Numbers with real and imaginary parts, e.g., 2 + 3j. +2. Boolean Type (bool): Represents a logical value, either True or False. +3. String Type (str): Represents a sequence of characters, e.g., "Hello, World!", 'Python'. +4. None Type (NoneType): Represents the absence of a value. +5. Sequence Types: +Lists (list): Ordered collections of values, e.g., [1, 2, 3], ['apple', 'banana', 'cherry']. +Tuples (tuple): Ordered, immutable collections of values, e.g., (1, 2, 3), ('red', 'green', 'blue'). +Ranges (range): Sequences of numbers, e.g., range(5), range(1, 11, 2). +6. Mapping Type: +Dictionaries (dict): Unordered collections of key-value pairs, e.g., {'name': 'Alice', 'age': 30}. +7. Set Types: +Sets (set): Unordered collections of unique values, e.g., {1, 2, 3}, {'apple', 'banana', 'cherry'}. +Frozen Sets (frozenset): Immutable sets. + +Here's a short script that demonstrates how to create and use variables of different data types in Python: + +# Numeric Types +integer_var = 42 +float_var = 3.14 +complex_var = 2 + 3j + +# Boolean Type +bool_var = True + +# String Type +string_var = "Hello, World!" + +# None Type +none_var = None + +# Sequence Types +list_var = [1, 2, 3, 4, 5] +tuple_var = (1, 2, 3, 4, 5) +range_var = range(1, 11, 2) + +# Mapping Type +dict_var = {'name': 'Alice', 'age': 30} + +# Set Types +set_var = {1, 2, 3, 4, 5} +frozenset_var = frozenset({1, 2, 3, 4, 5}) + +# Printing the variables +print("Integer:", integer_var) +print("Float:", float_var) +print("Complex:", complex_var) +print("Boolean:", bool_var) +print("String:", string_var) +print("None:", none_var) +print("List:", list_var) +print("Tuple:", tuple_var) +print("Range:", list(range_var)) +print("Dictionary:", dict_var) +print("Set:", set_var) +print("Frozen Set:", frozenset_var) + + + 5. Control Structures: - Explain the use of conditional statements and loops in Python. Provide examples of an `if-else` statement and a `for` loop. +Conditional Statements (if-else): +Conditional statements in Python use the if, elif (else if), and else keywords to execute different blocks of code based on a given condition. The general structure is as follows: + +if condition1: + # code block to execute if condition1 is True +elif condition2: + # code block to execute if condition1 is False and condition2 is True +else: + # code block to execute if all previous conditions are False + +Here's an example of an if-else statement in Python: + +age = 18 +if age >= 18: + print("You are eligible to vote.") +else: + print("You are not eligible to vote.") + +Loops (for loop): +Loops in Python allow you to execute a block of code repeatedly based on a given condition. The for loop is used to iterate over a sequence (such as a list, tuple, or string) and execute the code block for each element in the sequence. The general structure of a for loop is as follows: + +for item in sequence: + # code block to execute for each item in the sequence + + fruits = ['apple', 'banana', 'cherry'] +for fruit in fruits: + print(fruit) + + + 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. +Functions in Python are reusable blocks of code that perform a specific task. They are useful for the following reasons: + +1. Code Reuse: Functions allow you to encapsulate a piece of code and reuse it throughout your program, reducing duplication and making your code more maintainable. +2. Modularity: Functions help you break down your program into smaller, more manageable parts, making it easier to understand, test, and debug. +3. Abstraction: Functions hide the implementation details, allowing you to focus on the high-level functionality and interface. +4. Parameterization: Functions can accept parameters (inputs) and return values, making them more versatile and adaptable. +Here's an example of a Python function that takes two arguments and returns their sum: + +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 input numbers. + """ + result = a + b + return result + +To call this function, you can use the following syntax: + +# Calling the function +sum_of_numbers = add_numbers(5, 3) +print(sum_of_numbers) # Output: 8 + + + 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. + Lists: + +Lists are ordered collections of items, where each item has an index based on its position in the list. +Lists can contain elements of different data types, such as integers, strings, and even other lists or dictionaries. +Lists are mutable, meaning you can add, remove, or modify elements in the list. +Lists are accessed and manipulated using index-based operations. + +Dictionaries: + +Dictionaries are unordered collections of key-value pairs, where each key is unique and is used to access the corresponding value. +Dictionaries can also contain elements of different data types, but the keys must be immutable data types (e.g., strings, numbers, or tuples). +Dictionaries are mutable, allowing you to add, remove, or modify key-value pairs. +Dictionaries are accessed and manipulated using the keys. + +Here's a Python script that demonstrates the creation and basic operations on a list and a dictionary: +# Creating a list of numbers +numbers_list = [1, 2, 3, 4, 5] + +# Creating a dictionary with key-value pairs +person_dict = { + "name": "John Doe", + "age": 30, + "occupation": "Software Engineer" +} + +# Accessing elements in the list and dictionary +print("List element at index 2:", numbers_list[2]) # Output: List element at index 2: 3 +print("Dictionary value for key 'name':", person_dict["name"]) # Output: Dictionary value for key 'name': John Doe + +# Modifying elements in the list and dictionary +numbers_list[1] = 10 +person_dict["age"] = 31 + +# Adding new elements to the list and dictionary +numbers_list.append(6) +person_dict["email"] = "john.doe@example.com" + +# Removing elements from the list and dictionary +del numbers_list[0] +del person_dict["occupation"] + +# Iterating over the list and dictionary +print("Elements in the list:") +for num in numbers_list: + print(num) + +print("\nKey-value pairs in the dictionary:") +for key, value in person_dict.items(): + print(f"{key}: {value}") + +in this script, we first create a list of numbers and a dictionary with some key-value pairs. Then, we demonstrate the following operations: + +Accessing elements in the list and dictionary. +Modifying existing elements in the list and dictionary. +Adding new elements to the list and dictionary. +Removing elements from the list and dictionary. +Iterating over the list and dictionary. + +List element at index 2: 3 +Dictionary value for key 'name': John Doe +Elements in the list: +10 +3 +4 +5 +6 + +Key-value pairs in the dictionary: +name: Ayeni Victor +age: 24 +email: abbeyayeni2012@gmail.com + + + + 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 allows you to catch and handle unexpected or erroneous conditions using try, except, and finally blocks. + +The try block contains the code that might raise an exception, the except block specifies the type of exception to catch and the code to execute if that exception is raised, and the finally block contains code that will always execute. + +Example: +try: + result = 10 / 0 +except ZeroDivisionError: + print("Error: Division by zero") +finally: + print("This block will always execute.") + + In this example, the try block attempts to divide 10 by 0, which raises a ZeroDivisionError. The except block catches this exception and prints an error message. The finally block executes regardless of whether an exception was raised or not. + +Proper exception handling makes your code more robust and provides a better user experience by handling errors gracefully. + + + + + 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. + +single Python files that contain definitions of functions, classes, and variables. Modules allow you to group related code together and make it available for use in other parts of your application. + +Packages are collections of modules organized into directories. Packages provide a way to structure your code hierarchically and manage namespaces, making it easier to work with large codebases. + +To use a module in your Python script, you can import it using the import statement. Here's an example using the built-in math module: + +import math + +# Using functions from the math module +print(math.pi) # Output: 3.141592653589793 +print(math.sqrt(16)) # Output: 4.0 + +# Performing mathematical operations +result = math.pow(2, 3) # Equivalent to 2 ** 3 +print(result) # Output: 8.0 + +In this example, we first import the math module. This makes all the functions, constants, and other entities defined in the math module available to us in our script. + +We can then use the dot notation (module_name.entity_name) to access the various elements provided by the math module, such as math.pi and math.sqrt(). + +Alternatively, you can use the from statement to import specific entities from a module: + +from math import pi, sqrt + +print(pi) # Output: 3.141592653589793 +print(sqrt(16)) # Output: 4.0 + +In this case, we use from math import pi, sqrt to import the pi constant and the sqrt() function directly, allowing us to use them without the math. prefix. + +Modules and packages are essential for organizing and reusing code in Python. They help maintain code clarity, promote modularity, and enable the development of larger and more complex applications. + +When working with larger projects, it's common to create your own modules and packages to encapsulate and distribute your code. You can then import and use these custom modules and packages in your scripts, just like you would with built-in or third-party modules. + + + + + 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. + + + with open('file.txt', 'r') as file: + content = file.read() +print(content) + +data = ['Line 1', 'Line 2', 'Line 3'] +with open('output.txt', 'w') as file: + file.writelines(line + '\n' for line in data) + + + # Submission Guidelines: - Your answers should be well-structured, concise, and to the point.