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
289 changes: 288 additions & 1 deletion 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=15372370&assignment_repo_type=AssignmentRepo)
# SE-Assignment-6
Assignment: Introduction to Python
Instructions:
Expand All @@ -6,34 +8,319 @@ 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.
- What is Python, and what are some of the 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, which makes it accessible to both beginners and experienced developers.
Key Features of Python:
1. Simple and Readable Syntax: Python’s syntax is designed to be easy to read and write, which reduces the cost of program maintenance and allows developers to write clean and
readable code.
2. Interpreted Language: Python code is executed line by line, which simplifies debugging and development.
3. Dynamic Typing: Variables in Python do not need an explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable.
4. Extensive Standard Library: Python has a rich standard library that supports many common programming tasks such as file I/O, system calls, and web development.
Use Cases Where Python is Particularly Effective:
1. Web Development: Frameworks like Django and Flask make it easy to build robust and scalable web applications.
2. Data Science and Machine Learning: Libraries like Pandas, NumPy, SciPy, sci-kit-learn, and TensorFlow are widely used for data analysis, manipulation, and machine learning.
3. Scripting and Automation: Python is often used for writing scripts to automate repetitive tasks.
4. Game Development: Libraries like Pygame allow for the creation of simple 2D games.

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 on Windows OS
1. Download the Installer:
Visit the official Python website.
Download the latest Python installer for Windows.
2. Run the Installer:
Run the downloaded installer.
Check the box that says "Add Python to PATH."
Choose "Customize installation" for advanced options or proceed with "Install Now."
3. Customize Installation (Optional):
On the customization screen, you can select additional features and installation locations.
Click "Next" and then "Install."
4. Verify Installation:
Open Command Prompt.
Type python --version and press Enter.
You should see the installed Python version.

Setting Up a Virtual Environment
1. Ensure venv is Installed:
Python 3.3+ includes the venv module by default. For older versions or missing installations, you can install it using:
python3 -m pip install --user virtualenv
2. Create a Virtual Environment:
Navigate to your project directory.
Run the following command:
python3 -m venv myenv
3. Activate the Virtual Environment:
Run the following command:
.\myenv\Scripts\activate
4. Verify Activation:
The command prompt will change to indicate that the virtual environment is active, typically showing (myenv) before the prompt.
Deactivate the Virtual Environment:
5. To deactivate, simply run:
deactivate

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 of Basic Syntax Elements:
1. Function Call:
print(): This is a built-in function in Python that outputs the specified message to the console. Functions are invoked by their name followed by parentheses.
2. String:
"Hello, World!": This is a string, which is a sequence of characters enclosed within double quotes (") or single quotes ('). In this case, the string "Hello, World!" is the
argument passed to the print() function.

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. Integers (int):
Whole numbers without a fractional component.
Example: 42, -3
2. Floating-Point Numbers (float):
Numbers that contain a decimal point.
Example: 3.14, -0.001
3. Strings (str):
Ordered sequences of characters enclosed in single (') or double (") quotes.
Example: "Hello, World!", 'Python'
4. Booleans (bool):
Represents one of two values: True or False.
Example: True, False
5. Lists (list):
Ordered collections of items (can be of different types) enclosed in square brackets.
Example: [1, 2, 3], ['apple', 'banana', 'cherry']
6. Tuples (tuple):
Ordered, immutable collections of items enclosed in parentheses.
Example: (1, 2, 3), ('apple', 'banana', 'cherry')
7. Dictionaries (dict):
Unordered collections of key-value pairs enclosed in curly braces.
Example: {'name': 'John', 'age': 30}
8. Sets (set):
Unordered collections of unique items enclosed in curly braces.
Example: {1, 2, 3}, {'apple', 'banana', 'cherry'}
# Integer
age = 25
print("Age:", age)
print("Type:", type(age))

# Float
height = 5.9
print("\nHeight:", height)
print("Type:", type(height))

# String
name = "Alice"
print("\nName:", name)
print("Type:", type(name))

# Boolean
is_student = True
print("\nIs student:", is_student)
print("Type:", type(is_student))

# List
fruits = ["apple", "banana", "cherry"]
print("\nFruits:", fruits)
print("Type:", type(fruits))

# Tuple
coordinates = (10.0, 20.0)
print("\nCoordinates:", coordinates)
print("Type:", type(coordinates))

# Dictionary
person = {"name": "John", "age": 30}
print("\nPerson:", person)
print("Type:", type(person))

# Set
unique_numbers = {1, 2, 3, 4, 5}
print("\nUnique Numbers:", unique_numbers)
print("Type:", type(unique_numbers))

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 in Python are used to execute certain blocks of code based on specific conditions. The primary conditional statements are if, elif, and else.
if-else Statement Example:
age = 20
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")

for loop Statement Example:
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 blocks of reusable code that perform a specific task. They allow you to encapsulate code logic and make your programs more modular, readable, and
maintainable.

Functions in Python
Functions in Python are blocks of reusable code that perform a specific task. They allow you to encapsulate code logic and make your programs more modular, readable, and maintainable. Functions are defined using the def keyword, followed by the function name, parentheses (which may include parameters), and a colon. The code block within the function is indented.

Benefits of Using Functions:
1. Modularity - Functions break the code into smaller, manageable, and logical sections.
2. Reusability - Functions Allow you to reuse code without rewriting it.
3. Readability - Improves the readability and organization of the code.
4. Maintainability - Makes the code easier to update and maintain.
def add_numbers(a, b):
"""
This function takes two arguments and returns their sum.
"""
return a + b
Calling the function
# Example of calling the add_numbers function
result = add_numbers(5, 3)
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.
1. Lists are ordered collections of items. They can hold any type of object, including integers, strings, other lists, or even mixed types while dictionaries are unordered collections of items. Each item in a dictionary is stored as a key-value pair.
2. Elements in a list are accessed by their position (index). Indices start from 0 while dictionaries are Elements in a dictionary are accessed by their keys. Keys can be of any immutable type (typically strings or numbers).
3. Mutability: Lists are mutable, meaning you can change, add, or remove elements after the list is created while dictionaries are are mutable, so you can change the value associated with a key or add/remove key-value pairs.

# Creating a list of numbers
numbers = [1, 2, 3, 4, 5]

# Creating a dictionary with key-value pairs
person = {
"name": "Alice",
"age": 30,
"city": "New York",
"job": "Engineer"
}

# Accessing elements in a list
print("First element in numbers list:", numbers[0]) # Output: 1

# Accessing elements in a dictionary
print("Name of the person:", person["name"]) # Output: Alice

# Modifying elements in a list
numbers[0] = 10
print("Updated numbers list:", numbers) # Output: [10, 2, 3, 4, 5]

# Modifying elements in a dictionary
person["age"] = 31
print("Updated age in person dictionary:", person["age"]) # Output: 31

# Adding elements to a list
numbers.append(6)
print("List after appending a number:", numbers) # Output: [10, 2, 3, 4, 5, 6]

# Adding elements to a dictionary
person["email"] = "alice@example.com"
print("Updated person dictionary with email:", person)
# Output: {'name': 'Alice', 'age': 31, 'city': 'New York', 'job': 'Engineer', 'email': 'alice@example.com'}

# Removing elements from a list
numbers.remove(3)
print("List after removing number 3:", numbers) # Output: [10, 2, 4, 5, 6]

# Removing elements from a dictionary
del person["job"]
print("Updated person dictionary after removing job:", person)
# Output: {'name': 'Alice', 'age': 31, 'city': 'New York', 'email': 'alice@example.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 handle runtime errors gracefully, preventing the program from crashing and providing a way to manage errors and unexpected conditions. Python uses try, except, else, and finally blocks to catch and handle exceptions.
def divide_numbers(a, b):
try:
result = a / b
except ZeroDivisionError as e:
print("Error: Division by zero is not allowed.")
print(f"Exception message: {e}")
except TypeError as e:
print("Error: Invalid input type. Please provide numbers.")
print(f"Exception message: {e}")
else:
print("Division successful!")
print(f"Result: {result}")
finally:
print("Execution of the divide_numbers function is complete.")

# Example calls to the function
print("Example 1:")
divide_numbers(10, 2) # No exception, should print the result

print("\nExample 2:")
divide_numbers(10, 0) # ZeroDivisionError, should print the error message

print("\nExample 3:")
divide_numbers(10, "a") # TypeError, should print the error message

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:

Definition: A module in Python is a file containing Python definitions (functions, classes, variables) and statements. It allows you to organize your Python code logically and reuse it across multiple files.
Purpose: Modules help in modular programming, making code more readable, maintainable, and reusable.
Example: If you have a file named my_module.py containing functions and variables, you can use it in another Python script by importing it (import my_module).
Packages:

Definition: A package is a collection of related Python modules bundled together. It provides a hierarchical structure to organize and manage Python code. Packages help avoid naming conflicts and provide a means of namespace management.
Purpose: Packages help in organizing large codebases into manageable units and facilitate distribution and reuse of Python code.
Example: The numpy package, for instance, contains many modules (numpy.array, numpy.random, etc.) that provide different functionalities related to numerical computing.
Importing and Using a Module (Example with math module):
# Importing the math module
import math

# Using functions from the math module
print("Value of pi:", math.pi) # Output: Value of pi: 3.141592653589793
print("Square root of 16:", math.sqrt(16)) # Output: Square root of 16: 4.0
print("Cosine of 0:", math.cos(0)) # Output: Cosine of 0: 1.0

# You can also import specific functions or constants from math
from math import pi, sqrt

print("Another way to get pi value:", pi) # Output: Another way to get pi value: 3.141592653589793
print("Square root of 25 using import:", sqrt(25)) # Output: Square root of 25 using import: 5.0

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.
In Python, you can use the built-in open() function to read from and write to files. The open() function takes two main arguments: the file name and the mode in which to open the file. The common modes are:
'r' for reading (default)
'w' for writing (creates a new file or truncates an existing file)
'a' for appending (adds to the end of the file)
'b' for binary mode

Script to Read from a File
# Read from a file and print its content to the console

def read_file(filename):
try:
with open(filename, 'r') as file:
content = file.read()
print(content)
except FileNotFoundError:
print(f"Error: The file '{filename}' was not found.")
except IOError as e:
print(f"Error: An I/O error occurred. {e}")

# Example usage
read_file('example.txt')

Script to Write a List of Strings to a File
# Write a list of strings to a file

def write_to_file(filename, lines):
try:
with open(filename, 'w') as file:
for line in lines:
file.write(line + '\n')
except IOError as e:
print(f"Error: An I/O error occurred. {e}")

# Example usage
lines_to_write = [
"First line of text.",
"Second line of text.",
"Third line of text."
]

write_to_file('output.txt', lines_to_write)


# Submission Guidelines:
- Your answers should be well-structured, concise, and to the point.
Expand Down