diff --git a/submission b/submission new file mode 100644 index 0000000..e8981d4 --- /dev/null +++ b/submission @@ -0,0 +1,167 @@ +Assignment: Introduction to Python +Instructions: +Answer the following questions based on your understanding of Python programming. Provide detailed explanations and examples where appropriate. + + 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. + +Python is a high-level, interpreted programming language known for its readability and simplicity. Its syntax emphasizes readability and allows developers to express concepts in fewer lines of code compared to languages like C++ or Java. Python supports multiple programming paradigms, including procedural, object-oriented, and functional programming. + +Key Features: + +1. Readability: Python's syntax is designed to be readable and straightforward. +2. Ease of Learning: Python is easy to learn for beginners due to its simplicity. +3. Versatility: It can be used for web development, data analysis, artificial intelligence, scientific computing, and more. +4. Rich Ecosystem: Extensive libraries and frameworks, such as NumPy, pandas, Django, and Flask. +5. Cross-Platform: Python runs on various operating systems, including Windows, macOS, and Linux. +Use Cases: + +Web Development: Using frameworks like Django or Flask. +Data Analysis: With libraries like pandas and NumPy. +Artificial Intelligence: Through libraries like TensorFlow or PyTorch. +Automation: For scripting and automating repetitive tasks. +Scientific Computing: Using libraries like SciPy and Jupyter Notebooks. + +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. + + Steps for Installation: + +Download Python: + +Go to the official https://www.python.org/downloads/ +Download the installer suitable for your operating system (Windows, macOS, or Linux). +Install Python: + +Windows: Run the installer and check the box to add Python to your PATH. Follow the installation wizard. +macOS: Open the downloaded .pkg file and follow the instructions. +Linux: Use your package manager, e.g., sudo apt-get install python3 for Ubuntu. +Verify Installation: + +Open a terminal or command prompt and type python --version or python3 --version to check the installed version.s. +et Up Virtual Environment: + +Install venv if it’s not installed: pip install virtualenv. +Create a virtual environment: python -m venv myenv. +Activate the virtual environment: +Windows: myenv\Scripts\activate +macOS/Linux: source myenv/bin/activate +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!") + Explanation: + +print(): A built-in function that outputs text to the console. +"Hello, World!": A string literal that is passed as an argument to print(). + +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: + +Integer: Whole numbers, e.g., 5. +Float: Decimal numbers, e.g., 3.14. +String: Sequence of characters, e.g., "Hello". +Boolean: Represents True or False + Integer + +age = 25 + + Float +height = 5.9 + + String +name = "Alice" + + Boolean +is_student = True + +print("Age:", age) +print("Height:", height) +print("Name:", name) +print("Is student:", is_student) + +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 statement +age = 18 + +if age >= 18: + print("You are an adult.") +else: + print("You are a minor.") + +for loop +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. + + def add_numbers(a, b): + return a + b + +result = add_numbers(5, 7) +print("The sum is:", result) + + +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 and Dictionaries +Lists: + +Ordered collection of items. +Example: numbers = [1, 2, 3, 4, 5] + +Dictionaries: + +Unordered collection of key-value pairs. +Example: person = {"name": "Alice", "age": 30} + +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 allows you to manage errors and unexpected situations gracefully. + +try: + x = 10 / 0 +except ZeroDivisionError: + + + print("Cannot divide by zero.") +finally: + print("Execution completed.") + +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. + Concepts: + +Modules: Python files (.py) containing functions, classes, or variables. +Packages: Collections of modules in directories. + +import math + +print("Square root of 16:", math.sqrt(16)) + + +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. + + Reading from a File: + with open('example.txt', 'r') as file: + content = file.read() + print(content) + +Writing to a File: +with open('example.txt', 'w') as file: + lines = ["Hello", "World", "Python is awesome!"] + file.write("\n".join(lines)) + + + +