Skip to content
This repository was archived by the owner on Apr 10, 2026. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 198 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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=15445746&assignment_repo_type=AssignmentRepo)
# SE-Assignment-6
Assignment: Introduction to Python
Instructions:
Expand Down Expand Up @@ -41,4 +43,200 @@ Answer the following questions based on your understanding of Python programming
- Cite any references or sources you use in your answers.
- Submit your completed assignment by [due date].

ANSWERS
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, simplicity, and versatility. Its key features include:
1. Readable and Simple Syntax: Python's syntax is designed to be easy to understand and write, which makes it accessible for beginners and allows for faster development.
2. Dynamic Typing: Python does not require explicit declaration of variable types, which makes it flexible and reduces boilerplate code.
3. Interpreted Language: Python code is executed line by line, which makes debugging easier.
4. Extensive Standard Library: Python comes with a vast library of modules and packages, which facilitates various tasks without needing additional installations.
5. Cross-Platform Compatibility: Python runs on various operating systems, including Windows, macOS, and Linux.
6. Support for Multiple Paradigms: Python supports object-oriented, procedural, and functional programming styles.
Use Cases:
• Web Development: Frameworks like Django and Flask.
• Data Science: Libraries like Pandas, NumPy, and Matplotlib for data analysis and visualization.
• Machine Learning: Libraries like TensorFlow, Keras, and scikit-learn.
• Automation: Scripting tasks and automating workflows with libraries like os and subprocess.
• Scientific Computing: Simulations and calculations with SciPy.
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.
Windows:
1. Download Python Installer: Visit the official Python website and download the installer for Windows.
2. Run the Installer: Double-click the installer and ensure the checkbox "Add Python to PATH" is checked. Click "Install Now."
3. Verify Installation: Open Command Prompt and type python --version or python -V to check the installed version.
4. Set Up Virtual Environment:
o Install virtualenv (if not included): pip install virtualenv
o Create a virtual environment: python -m venv myenv
o Activate the virtual environment: source myenv/Scripts/activate
macOS:
1. Install Homebrew: If not installed, follow instructions at Homebrew.
2. Install Python: Run brew install python.
3. Verify Installation: Open Terminal and type python3 --version or python3 -V.
4. Set Up Virtual Environment:
o Install virtualenv (if not included): pip3 install virtualenv
o Create a virtual environment: python3 -m venv myenv
o Activate the virtual environment: source myenv/bin/activate
Linux:
1. Install Python: Use the package manager. For Debian-based distributions (like Ubuntu), run sudo apt-get install python3.
2. Verify Installation: Open Terminal and type python3 --version or python3 -V.
3. Set Up Virtual Environment:
o Install virtualenv (if not included): sudo apt-get install python3-venv
o Create a virtual environment: python3 -m venv myenv
o Activate the virtual environment: source myenv/bin/activate

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 the specified text to the console.
• "Hello, World!": A string literal enclosed in double quotes.
Syntax Elements:
• Python uses indentation (usually 4 spaces) to define code blocks.
• Statements end with a newline, not a semicolon.
• Function names are followed by parentheses, which can include arguments.

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:
1. Integers (int): Whole numbers.
2. Floating-Point Numbers (float): Numbers with decimal points.
3. Strings (str): Textual data.
4. Booleans (bool): True or False values.
Script Example:
# Variables of different data types
integer_var = 10
float_var = 20.5
string_var = "Hello, Python!"
boolean_var = True

# Print variable values and their types
print("Integer:", integer_var, "Type:", type(integer_var))
print("Float:", float_var, "Type:", type(float_var))
print("String:", string_var, "Type:", type(string_var))
print("Boolean:", boolean_var, "Type:", type(boolean_var))
Explanation:
• type(): Function to get the type of a variable.
• Variables are assigned using the = operator.
________________________________________
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:
• Used to execute code based on conditions.
• if, elif, and else are used to handle different cases.
Example:
x = 10

if x > 0:
print("Positive number")
elif x == 0:
print("Zero")
else:
print("Negative number")
Loops:
• Used to repeat code execution.
Example (for loop):
for i in range(5):
print(i)
Explanation:
• range(5): Generates numbers from 0 to 4.
• The for loop iterates over this range and prints each number.
________________________________________
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 are reusable blocks of code that perform a specific task. They help in organizing code, improving readability, and avoiding code duplication.
Example Function:
def add_numbers(a, b):
return a + b

# Calling the function
result = add_numbers(5, 3)
print("Sum:", result)
Explanation:
• def: Keyword to define a function.
• return: Keyword to send the result back to the caller.



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:
• Ordered collections of items.
• Items are accessed by index.
Dictionaries:
• Unordered collections of key-value pairs.
• Items are accessed by keys.
Script Example:
# List of numbers
numbers = [1, 2, 3, 4, 5]
print("List:", numbers)
print("First item:", numbers[0])

# Dictionary with key-value pairs
person = {"name": "Alice", "age": 30, "city": "New York"}
print("Dictionary:", person)
print("Name:", person["name"])

# Adding an item to the list
numbers.append(6)
print("Updated list:", numbers)

# Adding an item to the dictionary
person["occupation"] = "Engineer"
print("Updated dictionary:", person)
Explanation:
• Lists use indices for access and manipulation.
• Dictionaries use keys for access and manipulation.
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: Mechanism to manage runtime errors and ensure the program can handle unexpected situations gracefully.
Example:
try:
# Code that may cause an exception
num = int(input("Enter a number: "))
result = 10 / num
print("Result:", result)
except ZeroDivisionError:
# Code to handle division by zero
print("Error: Cannot divide by zero.")
except ValueError:
# Code to handle invalid input
print("Error: Invalid input. Please enter a number.")
finally:
# Code that will run regardless of an exception
print("Execution completed.")
Explanation:
• try: Block where exceptions may occur.
• except: Blocks to handle specific exceptions.
• finally: Block that executes regardless of whether an exception occurred.
________________________________________
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: Files containing Python code (functions, classes, variables) that can be imported into other scripts.
Packages: Collections of modules organized in directories with an __init__.py file.
Example with math Module:
import math

# Using functions from the math module
print("Square root of 16:", math.sqrt(16))
print("Value of pi:", math.pi)
Explanation:
• import: Keyword to include a module in your script.
• math.sqrt(): Function to calculate the square root.
• math.pi: Constant for the value of pi.

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:
# Reading from a file
with open('example.txt', 'r') as file:
content = file.read()
print("File content:\n", content)
Writing to a File:
# Writing to a file
lines = ["Hello, World!\n", "This is a test.\n", "Python file I/O."]

with open('output.txt', 'w') as file:
file.writelines(lines)