From d952e685f4f7c6f09c0be71720c0e5544da372ae Mon Sep 17 00:00:00 2001 From: Mantombi81 <163122012+Mantombi81@users.noreply.github.com> Date: Mon, 8 Jul 2024 20:29:18 +0200 Subject: [PATCH] Update README.md --- README.md | 180 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 176 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3e79495..7423141 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=15385943&assignment_repo_type=AssignmentRepo) # SE-Assignment-6 Assignment: Introduction to Python Instructions: @@ -7,33 +9,203 @@ 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 programming language known for its simplicity and readability. It was created by Guido van Rossum and first released in 1991. Python's design philosophy emphasizes + code readability and simplicity, which makes it a popular choice for beginners and experienced developers alike. + * Features: + -Easy to Learn and Use + -Expressive Language + -Extensive Standard Library + -Versatility + -Community and Support + -Platform Independence + * Examples of use cases where Python is particularly effective: + -Web Development: Python frameworks like Django and Flask are widely used for building web applications due to their simplicity and scalability + -Data Analysis and Visualization: Python's libraries such as Pandas, NumPy, and Matplotlib are extensively used for data manipulation, analysis, and visualization + -Machine Learning and AI: Python's simplicity and rich ecosystem of libraries (e.g., TensorFlow, PyTorch, Scikit-learn) make it a preferred choice for developing machine learning models and AI + applications + -Scripting and Automation: Python's ease of use and cross-platform compatibility make it ideal for scripting tasks and automating repetitive tasks + -Education: Python's readability and simplicity make it an excellent choice for teaching programming to beginners + 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. +*Installing Python +Windows: +Download Python Installer +Visit the official Python website: python.org/downloads +Download the latest version of Python for Windows (e.g., Python 3.10.1) +Run the Installer: +Double-click the downloaded installer (.exe file) +Check the box "Add Python x.x to PATH" during installation +Click "Install Now" (or customize the installation if needed) +Verify Installation +Open Command Prompt (cmd) or PowerShell +Type python --version or python3 --version +You should see the installed Python version number 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. - +* #This is a comment in Python + print("Hello, World!") + -In Python, comments start with the # character + -The print() function in Python is used to print output to the console + 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. - + * Integer (int):represents whole numbers + - age = 25 + * Floating-point number (float):represents decimal numbers + - height = 1.75 + * String (str):represents sequences of characters, enclosed in single (') + or double (") quotes + - name = "Oratile" + message = 'Happy, Birthday.' + * Boolean (bool):represents truth values True and False + - is_firstName = True + * List:ordered collection of items + - numbers = [1, 2, 3, 4] + * Tuple:similar to a list but immutable (cannot be changed after creation) + - coordinates = (15.5, 25.5) + * Dictionary (dict):collection of key-value pairs + - person = {'name': 'Oratile', 'age': 15, 'country': 'South Africa'} + 5. Control Structures: - Explain the use of conditional statements and loops in Python. Provide examples of an `if-else` statement and a `for` loop. +*In Python, conditional statements and loops are fundamental constructs + used for decision-making and repetitive tasks, respectively +*Examples: +-Conditional Statements (if-else): + Conditional statements allow you to execute certain pieces of code based + on whether a condition evaluates to True or False. +-Loops (for loop): + Loops are used to iterate over a sequence (such as a list, tuple, or + string) or execute a block of code repeatedly. 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. +- 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 blocks of organized, reusable code that perform + a specific task. They allow you to break down a program into smaller, + manageable pieces, which can be called (invoked) whenever needed. + Functions can take inputs (arguments), perform computations, and + optionally return outputs +*Benefits of using functions in Python: + Modularity: Functions help in modularizing code, making it easier to + understand, reuse, and maintain. + Code Reusability: Once defined, functions can be called multiple times + from different parts of the program, reducing redundancy. + Abstraction: Functions abstract away implementation details, allowing + users to focus on what the function does rather than how it does it + Testing and Debugging: Functions make it easier to test and debug code, + as you can isolate specific parts of the program. +*Example of a Python function: + def sum_two_numbers(a, b): + """ + Function to calculate the sum of two numbers. + Parameters: + a (int or float): First number. + b (int or float): Second number. + Returns: + int or float: Sum of a and b. + """ + return a + b 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 is indexed + by a number starting from 0. +- # Example of a list of numbers + numbers = [1, 2, 3, 4, 5] +*Dictionaries:dictionaries are unordered collections of key-value pairs +- # Example of a dictionary with key-value pairs +person = { + 'name': 'Oratile', + 'age': 15, + 'country': 'South Africa' +} 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 to handle runtime errors + (exceptions) gracefully, preventing the program from crashing + unexpectedly. It allows you to catch exceptions that occur during + program execution and take appropriate actions +- Components of Exception Handling: + try block:the try block is used to enclose the code that might raise an + exception. +except block:the except block is used to handle specific exceptions that + are raised in the try block.You can have multiple except + blocks to handle different types of exceptions. +finally block:the finally block is optional and is used to execute + cleanup code, whether an exception occurred or not. + It's typically used for releasing external resources (like + closing a file or network connection). +Example of Exception Handling: +-def divide_numbers(a, b): + try: + result = a / b + except ZeroDivisionError: + print("Error: Division by zero!") + else: + print(f"{a} divided by {b} is equal to {result}") + finally: + print("Executing finally block") +# Example usage +divide_numbers(10, 2) # Normal case +divide_numbers(10, 0) # Exception case 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. +*Modules:a module in Python is a file containing Python definitions + functions, classes, variables) and statements. It serves as a + way to organize Python code logically into reusable units. +*Packages:a package in Python is a way to structure modules + hierarchically. It consists of multiple modules grouped + together in a directory. The directory must contain a special + file named __init__.py, which can be empty or contain + initialization code for the package. + -Importing and Using a Module in Python: + Example using the math module: + The math module in Python provides access to mathematical functions and + constants. Here's how you can import and use the math module in your + script: +# Importing the math module +import math + +# Using functions from the math module +print("Value of pi:", math.pi) # Accessing the constant pi +print("Square root of 16:", math.sqrt(16)) # Calculating the square root + +# Using variables from the math module +radius = 5 +area_of_circle = math.pi * (radius ** 2) +print("Area of a circle with radius 5:", area_of_circle) + +# Using math module functions within your own function +def calculate_hypotenuse(a, b): + return math.sqrt(a**2 + b**2) + +side1 = 3 +side2 = 4 +hypotenuse = calculate_hypotenuse(side1, side2) +print(f"Hypotenuse of a right triangle with sides {side1} and {side2}:", hypotenuse) -10. File I/O: +11. 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. +*Python provides built-in functions and methods for reading from and + writing to files. These operations are fundamental for interacting with + external data and storing outputs +-Reading from a File steps: +Open the File: Use the open() function to open a file in read mode ('r'). +Read Content: Use methods like read(), readline(), or readlines() to read + the content of the file. +Close the File: Always close the file after reading to free up system + resources. + -Writing to a File steps: +Open the File: Use the open() function with 'w' (write) or 'a' (append) + mode. +Write Content: Use methods like write() to write data to the file. +Close the File: Always close the file after writing to ensure all data is + written and to free up system resources. # Submission Guidelines: - Your answers should be well-structured, concise, and to the point.