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
128 changes: 118 additions & 10 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=15409851&assignment_repo_type=AssignmentRepo)
# SE-Assignment-6
Assignment: Introduction to Python
Instructions:
Expand All @@ -6,34 +8,140 @@ 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 its key features that make it popular among developers? Provide examples of use cases where Python is particularly effective. Answer:Python is a high-level, interpreted programming language known for its simplicity and readability. Its syntax is clear and understandable, making it easier to write and maintain code. Python's extensive standard library provides modules and functions for various tasks, reducing the need for developers to write code from scratch. It is dynamically typed, handling memory management automatically, simplifying programming. Python supports multiple programming paradigms, making it adaptable to various use cases. It is popular for web development, data science, machine learning, scripting, and automation. Python's large community of developers contributes libraries, frameworks, and provides support through forums and tutorials. This community-driven aspect ensures ongoing development and support for Python, making it a popular choice for various applications in software development, scientific computing, and data analysis.References:Python Official Website: python.org, Python Documentation: docs.python.org. code:# Hello World program in Python
print("Hello, World!")


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.
- 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.Answer: To install Python on Windows, macOS, and Linux, follow these steps:
1. Download Python from the official website.
2. Double-click the downloaded.exe file and add Python to the PATH during installation.
3. Verify installation by running Command Prompt (cmd.exe) and typing python --version or python -V to check the installed Python version.
4. Set up a virtual environment using pip.
5. Install virtualenv using pip.
6. Create a new virtual environment using myenv.
7. Activate the virtual environment by running bash.
8. Verify installation and virtual environment by checking Python version and pip version.
9. Check virtual environment activation by running bash.
10. This setup allows for managing dependencies and packages separately for different projects, ensuring a clean and isolated development environment.REfernece: https://docs.python.org/3/using/index.html . code : python pip install virtualenv, python virtualenv myenv, python myenv\Scripts\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.
- Write a simple Python program that prints "Hello, World!" to the console. Explain the basic syntax elements used in the program.Answer: This Python program prints "Hello, World!" to the console using the print() function. The program starts with the # symbol and ends with the string "Hello, World!" The print() function outputs the string to the console. Strings can be enclosed in single or double quotes. To run the program, save it in a file and open a terminal or command prompt. Reference:https://docs.python.org/3/ . code : print("Hello, World!")


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.
- 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. Answer: Python uses several basic data types, including integers (int), floats (float), strings (str), Booleans (bool), lists (list), tuples (tuples), and dictionaries (dicts). A short script demonstrates how to create and use variables of different data types. Variables are declared using the assignment operator (=), and their values are displayed in the console. Elements of lists, tuples, and dictionaries are accessed using indexing ([]) or keys (['key'] for dictionaries). The script also demonstrates how to print the values of variables and access elements in these types. Reference: https://docs.python.org/3/library/stdtypes.html . Code: # Define variables of different data types
my_integer = 42
my_float = 3.14
my_string = "Hello, Python!"
my_boolean = True
my_list = [1, 2, 3, 4, 5]
my_tuple = ('apple', 'banana', 'cherry')
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}

# Print the variables
print("Integer:", my_integer)
print("Float:", my_float)
print("String:", my_string)
print("Boolean:", my_boolean)
print("List:", my_list)
print("Tuple:", my_tuple)
print("Dictionary:", my_dict)

# Accessing elements in list, tuple, and dictionary
print("First element of list:", my_list[0])
print("Second element of tuple:", my_tuple[1])
print("Age in dictionary:", my_dict['age'])


5. Control Structures:
- Explain the use of conditional statements and loops in Python. Provide examples of an `if-else` statement and a `for` loop.
- # Define variables of different data types
my_integer = 42
my_float = 3.14
my_string = "Hello, Python!"
my_boolean = True
my_list = [1, 2, 3, 4, 5]
my_tuple = ('apple', 'banana', 'cherry')
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}

# Print the variables
print("Integer:", my_integer)
print("Float:", my_float)
print("String:", my_string)
print("Boolean:", my_boolean)
print("List:", my_list)
print("Tuple:", my_tuple)
print("Dictionary:", my_dict)

# Accessing elements in list, tuple, and dictionary
print("First element of list:", my_list[0])
print("Second element of tuple:", my_tuple[1])
print("Age in dictionary:", my_dict['age'])
.

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. Answer: Functions in Python are organized blocks of code that perform specific tasks, simplifying code readability, understanding, and maintenance. They promote code reusability and enable abstraction, allowing complex operations to be encapsulated into a single entity. Functions are useful for modularity, code reusability, abstraction, and organization. For example, the function'sum_two_numbers' can be defined using the def keyword and returns the sum of a and b. To call the function, provide the required arguments and print the result. Reference: https://docs.python.org/3/tutorial/controlflow.html#defining-functions . code: def fib(n): # write Fibonacci series up to n
"""Print a Fibonacci series up to n."""
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a+b
print()

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.
- 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. Answer: In Python, lists and dictionaries are fundamental data structures with distinct characteristics. Lists are ordered collections of items, indexed by integers starting from 0, and can contain elements of different data types. They are mutable and can be changed after creation. On the other hand, dictionaries are unordered collections of key-value pairs, mutable and unique, with values of any data type. They are defined using curly braces { }, separated by a colon :. Lists are used when the order of elements matters and when frequent addition or removal is necessary, while dictionaries are used when accessing elements by a specific key is more relevant. An example script demonstrates basic operations on both lists and dictionaries. Reference:https://docs.python.org/3/tutorial/datastructures.html . code : fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
fruits.count('apple')
2
fruits.count('tangerine')
0
fruits.index('banana')
3
fruits.index('banana', 4) # Find next banana starting at position 4
6
fruits.reverse()
fruits
['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange']
fruits.append('grape')
fruits
['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange', 'grape']
fruits.sort()
fruits
['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange', 'pear']
fruits.pop()
'pear'

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.
- 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. Answer: Python's exception handling system allows for graceful handling of errors during program execution without causing the program to crash. It consists of try, except, and optionally finally blocks. The try block contains the code where an error might occur, and if an exception is raised within it, Python looks for an except block that matches the type of exception raised. If an exception occurs in the try block, execution immediately jumps to the except block that handles that particular type of exception. The optional finally block is always executed, regardless of whether an exception occurred or not. This is typically used for cleanup operations, such as closing files or releasing resources. Reference : https://docs.python.org/3/tutorial/errors.html. code : Result of 10 / 2 = 5.0
Execution of division operation completed.

Error: Division by zero!
Execution of division operation completed.

Error: unsupported operand type(s) for /: 'int' and 'str'
Execution of division operation 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.
- 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. Answer: Python modules and packages are tools for organizing and reusing code. Modules are files containing Python definitions and statements, allowing for better readability, maintainability, and reusability. They can be imported into a Python script to access functions, classes, and variables. Packages are collections of Python modules under a single directory, providing a hierarchical structure to the module namespace. To import and use a module, use the import statement followed by the module name. For example, the math module provides mathematical functions, such as square root and factorial. Reference : https://docs.python.org/3/tutorial/modules.html. code : Square root of 16 is: 4.0
Factorial of 5 is: 120


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.
- 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. Answer: Python allows for easy reading and writing to files using built-in functions and methods. To read from a file, use the open() function to open the file in read mode, read the content using methods like read(), readline(), or readlines(), and close the file using the close() method to free up system resources. To write to a file, open the file in write mode, write data using write(), or write a sequence of strings using writelines(). Close the file using the close() method after writing to ensure all data is properly saved. The with open() statement opens the file in read mode, reads the content using read(), and prints the content to the console. The with open() statement opens or creates the file in write mode, ensures proper closing, and writes each line to the file. Reference: https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files . code : # Example of reading from a file and printing its content

# Open the file in read mode
file_path = 'sample.txt'
with open(file_path, 'r') as file:
# Read the entire content of the file
file_content = file.read()

# Print the content
print("Content of the file:")
print(file_content)


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