From ee3825451348eeccd283eb02cb0f9b5edfd33f8d Mon Sep 17 00:00:00 2001 From: Tshifhiwa Maumela <138950862+maumelas1234@users.noreply.github.com> Date: Tue, 23 Jul 2024 13:19:18 +0200 Subject: [PATCH] Update README.md --- README.md | 198 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 178 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 3e79495..08d2f8f 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=15451672&assignment_repo_type=AssignmentRepo) # SE-Assignment-6 Assignment: Introduction to Python Instructions: @@ -5,35 +7,191 @@ Answer the following questions based on your understanding of Python programming Questions: -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. +1.Python Basics: -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. +Python is a high-level, interpreted programming language known for its simplicity and readability. Some key features include: -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. + Easy to Learn and Use: Python's syntax is straightforward and emphasizes readability. + Versatility: It supports various programming paradigms (procedural, object-oriented, functional). + Large Standard Library: Python comes with a vast collection of modules and libraries for different tasks. -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. +Examples of Use Cases: -5. Control Structures: - - Explain the use of conditional statements and loops in Python. Provide examples of an `if-else` statement and a `for` loop. + Web Development: Django and Flask frameworks for building web applications. + Data Science: Libraries like NumPy, Pandas, and Matplotlib for data analysis and visualization. + Automation: Writing scripts for automation tasks due to its simplicity and cross-platform compatibility. -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. +2.Installing Python: -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. +To install Python on your operating system (Windows, macOS, or Linux), follow these steps: -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. + Download Python: Visit the official Python website (https://www.python.org/) and download the installer for your operating system. -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. + Run the Installer: + Windows: Run the downloaded .exe file and follow the installation prompts. Ensure to check the box "Add Python to PATH" during installation. + macOS: Run the downloaded .pkg file and follow the prompts. + Linux: Python is often pre-installed on Linux. Use your package manager (e.g., apt-get for Ubuntu) to install Python. -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. + Verify Installation: Open a command prompt (or terminal) and type python --version or python3 --version to verify Python is installed correctly. + + Set up a Virtual Environment (Optional but recommended): + Install virtualenv if not already installed: pip install virtualenv. + Create a virtual environment: virtualenv myenv. + Activate the virtual environment: + Windows: myenv\Scripts\activate + macOS/Linux: source myenv/bin/activate + +3.Python Syntax and Semantics: + +Here's a simple Python program that prints "Hello, World!" to the console: + +python + +# hello_world.py +print("Hello, World!") + +Explanation: + + print() is a built-in Python function used to output text to the console. + "Hello, World!" is a string literal enclosed in double quotes. + +4.Data Types and Variables: + +Basic data types in Python include: + + int: Integer numbers (e.g., 5, -10). + float: Floating-point numbers (e.g., 3.14, 2.718). + str: Strings of characters (e.g., "hello", 'world'). + bool: Boolean values (True or False). + list: Ordered collection of items (e.g., [1, 2, 3]). + dict: Unordered collection of key-value pairs (e.g., {"name": "John", "age": 30}). + +Example script demonstrating variables: + +python + +# variables.py +# Creating variables +x = 5 # int +y = 3.14 # float +name = "Alice" # str +is_student = True # bool +numbers = [1, 2, 3] # list +person = {"name": "Bob", "age": 25} # dict + +# Accessing variables +print(x) +print(name) +print(numbers[0]) +print(person["age"]) + +5.Control Structures: + +Conditional Statements: + +python + +# conditional.py +# If-else statement +age = 18 +if age >= 18: + print("You are an adult.") +else: + print("You are a minor.") + +Loops: + +python + +# loops.py +# For loop +for i in range(5): + print(i) + +6.Functions in Python: + +Functions in Python are blocks of reusable code used to perform a specific task. They are defined using the def keyword. + +python + +# functions.py +# Function to add two numbers +def add_numbers(a, b): + return a + b + +# Calling the function +result = add_numbers(3, 5) +print("Result:", result) + +7.Lists and Dictionaries: + +Lists are ordered collections, while dictionaries are unordered collections with key-value pairs. + +python + +# lists_and_dicts.py +# List of numbers +numbers = [1, 2, 3, 4, 5] + +# Dictionary of person's information +person = {"name": "Alice", "age": 30, "city": "New York"} + +# Accessing elements +print(numbers[0]) # Output: 1 +print(person["name"]) # Output: Alice + +# Adding new elements +numbers.append(6) +person["email"] = "alice@example.com" + +8.Exception Handling: + +Exception handling in Python allows you to handle errors gracefully using try, except, and optionally finally. + +python + +# exception_handling.py +# Example with division by zero error +try: + result = 10 / 0 +except ZeroDivisionError: + print("Error: Division by zero!") +finally: + print("Execution completed.") + +9.Modules and Packages: + +Modules are Python files containing functions and variables, while packages are directories of modules. You can import and use modules in your scripts. + +python + +# math_module_example.py +# Using math module +import math + +# Calculating square root +num = 25 +sqrt = math.sqrt(num) +print("Square root of", num, "is", sqrt) + +10 File I/O: + +File I/O in Python involves reading from and writing to files. + +python + +# read_file.py +# Reading from a file +with open('example.txt', 'r') as file: + content = file.read() + print(content) + +# write_file.py +# Writing to a file +data = ["Apple", "Banana", "Cherry"] +with open('fruits.txt', 'w') as file: + for fruit in data: + file.write(fruit + '\n') # Submission Guidelines: - Your answers should be well-structured, concise, and to the point.