Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Py_midend_ans (1)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

23PCS3CC06

CORE COURSE: ADVANCED PYTHON AND MONGODB

Answer all the questions:

SECTION-A (K1-Knowledge/Remembering)

7×1-7

1. What is python?

Python is a general-purpose programming language that is used to create software,


websites, and analyze data. It is known for being easy to learn, readable, and efficient.
Python is a good choice for beginners and experienced developers alike.

2. Define lists,

A list is a data structure in Python that is a mutable, or changeable, ordered sequence


of elements. Each element or value that is inside of a list is called an item.Lists are
mutable, meaning that their contents can be changed after the list has been created.
They can hold a various of data types, including integers, floats, strings, and even other
lists.

example for Python list:

a = [1, 'apple', 3.14, [5, 6]]


print(a)

Output
[1, 'apple', 3.14, [5, 6]]

3. Identify the data type with example.

In Python, the main data types include:

1. **Integer**: Whole numbers.


- Example: `x = 10`

2. **Float**: Numbers with decimal points.


- Example: `y = 3.14`
3. **String**: A sequence of characters.
- Example: `name = "Alice"`

4. **Boolean**: Represents `True` or `False`.


- Example: `is_active = True`

5. **List**: An ordered collection of items.


- Example: `fruits = ["apple", "banana", "cherry"]`

6. **Tuple**: An ordered, immutable collection of items.


- Example: `coordinates = (10, 20)`

7. **Dictionary**: A collection of key-value pairs.


- Example: `person = {"name": "Alice", "age": 30}`

8. **Set**: A collection of unique items.


- Example: `unique_numbers = {1,2,3,3}` # Only 1, 2, 3 are stored.

4. Relate a program to add two numbers using input function.

Code:

x = input("Type a number: ")


y = input("Type another number: ")
sum = int(x) + int(y)
print("The sum is: ", sum)

Output
Type a number :2
Type another number :5
The sum is : 7

5. Define NumPy.

NumPy, or Numerical Python, is an open-source Python library that provides


support for multi-dimensional arrays and matrices, as well as a variety of mathematical
functions

Arrays: NumPy arrays have a fixed size and all elements must be of the same data type.

6. Outline the syntax for range function.


The range() function returns a sequence of numbers, starting from 0 by default,
and increments by 1 (by default), and stops before a specified number.
Syntax
range(start, stop, step)

7. Describe arrays.

An array is a collection of items stored at contiguous memory locations. The idea


is to store multiple items of the same type together. This makes it easier to calculate the
position of each element by simply adding an offset to a base value, i.e., the memory
location of the first element of the array (generally denoted by the name of the array).

Eg:

Index: 0. 1. 2. 3
my Arr = [ 20, 35, 55, 65]

SECTION-B (K2-Comprehension/Understanding)

Answer all the questions:

5x3-15

8. Interpret a python program given integer number is odd or even using not operator.

# Function to check if a number is even or odd

def check_even_odd(number):
if not number % 2:
return f"{number} is even."
else:
return f"{number} is odd."

# Example usage
num = int(input("Enter an integer: "))
result = check_even_odd(num)
print(result)

9. Infer a python program to print the years 1000 to 2000 and mention it is a leap year or
not using while loop.

Code:
year = 1000

while year <= 2000:


if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")
year += 1

Output:

1000 is a leap year.


1001 is not a leap year.
1002 is not a leap year.
1003 is not a leap year.
1004 is a leap year.
1005 is not a leap year.
...
1996 is a leap year.
1997 is not a leap year.
1998 is not a leap year.
1999 is not a leap year.
2000 is a leap year.

10. Summarize a python program to print 1 to 10 numbers using for loop.

# Program to print numbers from 1 to 10

for i in range(1, 11):


print(i)

Output:

1
2
3
4
5
6
7
8
9
10
11. Interpret a python program to find given operator is arithmetic or not using if.

# Function to check if the operator is arithmetic


def is_arithmetic_operator(operator):
# Define a list of arithmetic operators
arithmetic_operators = ['+', '-', '*', '/', '%']

if operator in arithmetic_operators:
return True
else:
return False

# Input from the user


user_input = input("Enter an operator:")
if is_arithmetic_operator(user_input):
print(f"{user_input} is an arithmetic operator.")
else:
print(f"{user_input} is not an arithmetic operator.")

12. Infer a python program to find the biggest of three numbers using nested-if.

# Function to find the biggest of three numbers


def find_biggest(a, b, c):
if a >= b:
if a >= c:
return a # a is the biggest
else:
return c # c is the biggest
else:
if b >= c:
return b # b is the biggest
else:
return c # c is the biggest

# Input from the user


num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))

# Finding and displaying the biggest number


biggest = find_biggest(num1, num2, num3)
print(f"The biggest number is: {biggest}")
Output :
Enter the first number: 10
Enter the second number: 20
Enter the third number: 15
The biggest number is: 20.0
SECTION-C (K3-Application/Applying)

3x6-18

Answer ALL the questions by choosing either/or:

13.a) Sketch a python program to determine the student marks grade using nested-if

Code:

# Function to determine the grade


def determine_grade(marks):

if marks >= 0 and marks <= 100: # Check if marks are within a valid range
if marks >= 90:
grade = 'A'
else:
if marks >= 80:
grade = 'B'
else:
if marks >= 70:
grade = 'C'
else:
if marks >= 60:
grade = 'D'
else:
grade = 'F'
return grade
else:
return "Invalid marks. Please enter marks between 0 and 100."

# Input from user


try:
student_marks = float(input("Enter the student's marks: "))
grade = determine_grade(student_marks)
print(f"The student's grade is: {grade}")
except ValueError:
print("Please enter a valid number.")
Output:
Enter the student's marks: 85
The student's grade is: B

Explanation:
1. Function Definition: The determine_grade function checks the marks and assigns
a grade based on nested if statements.
2. Input Validation: It ensures the marks are within a valid range (0-100).
3. Grade Calculation: The program uses nested if statements to determine the grade.
4. User Input: It prompts the user to enter the student's marks and handles any
invalid input.
(OR)

b) Sketch a python program to find if the given integer number is single, double, triple,
four- digit number or not using nested-if.

# Function to determine the digit category


def determine_digit_category(number):
if number >= 0: # Check for non-negative numbers
if number < 10:
category = "Single-digit number"
else:
if number < 100:
category = "Double-digit number"
else:
if number < 1000:
category = "Triple-digit number"
else:
if number < 10000:
category = "Four-digit number"
else:
category = "Not a single, double, triple, or four-digit number"
else:
category = "Not a single, double, triple, or four-digit number"

return category

# Input from user


try:
user_input = int(input("Enter an integer: "))
result = determine_digit_category(user_input)
print(result)
except ValueError:
print("Please enter a valid integer.")
Output:
Enter an integer: 5
Single-digit number
Enter an integer: 25
Double-digit number
Enter an integer: 123
Triple-digit number
Enter an integer: 5678
Four-digit number
Enter an integer: -5
Not a single, double, triple, or four-digit number
Enter an integer: abc
Please enter a valid integer.
Explanation:
Function Definition: The determine_digit_category function checks the number and
categorizes it based on nested if statements.
Non-negative Check: It first checks if the number is non-negative.
Digit Count: It uses nested if statements to classify the number as single, double, triple,
or four-digit.
User Input: The program prompts the user for input and handles non-integer values
gracefully.

14.a) Relate a python program for matrix addition.

# Function to add two matrice

def add_matrices(matrix1, matrix2):


# Check if the dimensions match

if len(matrix1) != len(matrix2) or len(matrix1[0]) != len(matrix2[0]):


return "Matrices must have the same dimensions for addition."
result = [[0 for _ in range(len(matrix1[0]))] for _ in range(len(matrix1))]

# Add the matrices


for i in range(len(matrix1)):
for j in range(len(matrix1[0])):
result[i][j] = matrix1[i][j] + matrix2[i][j]

return result

# Function to input a matrix


def input_matrix(rows, cols):
matrix = []
print(f"Enter the elements for a {rows}x{cols} matrix:")
for i in range(rows):
row = list(map(int, input(f"Row {i + 1}: ").split()))
while len(row) != cols:
print(f"Please enter exactly {cols} numbers.")
row = list(map(int, input(f"Row {i + 1}: ").split()))
matrix.append(row)
return matrix

# Main program
try:
# Input dimensions for the matrices
rows = int(input("Enter number of rows: "))
cols = int(input("Enter number of columns: "))

# Input the matrices


print("Matrix 1:")
matrix1 = input_matrix(rows, cols)

print("Matrix 2:")
matrix2 = input_matrix(rows, cols)

# Perform matrix addition


result = add_matrices(matrix1, matrix2)

# Print the result


print("Resultant Matrix:")
for row in result:
print(row)
except ValueError:
print("Please enter valid integer values.")
Enter number of rows: 2
Enter number of columns: 2
Matrix 1:
Row 1: 1 2
Row 2: 3 4
Matrix 2:
Row 1: 5 6
Row 2: 7 8
Resultant Matrix:
[6, 8]
[10, 12]

b)Demonstrate Three dimensional array with example

Creating a Three-Dimensional Array:

In Python, a three-dimensional array can be represented using nested lists. Here’s an


example of a 3D array representing a cube with dimensions 2x3x4:
Create a 3D array (2 layers, 3 rows, 4 columns)

three_d_array = [
[ # Layer 1
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
],
[ # Layer 2
[13, 14, 15, 16],
[17, 18, 19, 20],
[21, 22, 23, 24]
]
]

# Accessing elements
print("Element at (0, 1, 2):", three_d_array[0][1][2])

Output: 7

print("Element at (1, 2, 3):",


three_d_array[1][2][3])

Output: 24

Manipulating the Three-Dimensional Array:

You can modify elements in the 3D array like this:

Modifying an element

three_d_array[0][0][0] = 100
# Change the element at (0, 0, 0)
print("Modified Element at (0, 0, 0):", three_d_array[0][0][0])
Output: 100

Iterating Over the Three-Dimensional Array


You can also iterate through the array using nested loops:
Print all elements in the 3D array

for layer in range(len(three_d_array)):


print(f"Layer {layer + 1}:")
for row in range(len(three_d_array[layer])):
for col in range(len(three_d_array[layer][row])):
print(three_d_array[layer][row][col], end=' ')
print() # New line after each row
print() # New line after each layer

Output

Element at (0, 1, 2): 7


Element at (1, 2, 3): 24
Modified Element at (0, 0, 0): 100
Layer 1:
100 2 3 4
5678
9 10 11 12

Layer 2:
13 14 15 16
17 18 19 20
21 22 23 24

This example demonstrates how to work with three-dimensional arrays in Python,


including creation, accessing, modifying, and iterating through the elements.
15. a) Discover the tuples operations with examples.
(OR)

15. b) Discover the dictionary operations with examples.

Refer dictionary pdf

SECTION-D

16. Categorize any 10 List functions with examples. (K4-LEVEL)


https://www.geeksforgeeks.org/list-methods-python/

Refer list pdf also

17. Illustrate any 10String functions with examples.

https://trainings.internshala.com/blog/python-string-functions/
18. Devise any 10 Python program using NumPy with necessary examples. (K5-LEVEL)

https://www.geeksforgeeks.org/python-numpy/
Chatgpt la podu chlo adhula neriya ans varudhu endha ques ku
SECTION-D

Answer TWO questions by Choosing one at K5 Level and one at Ko Level

2x10-20

(K6 Level Question is Compulsory)

16. Reframe Create, Read, Update and Delete Operations using MongoDB with examples.
(KS-LEVEL)

https://www.geeksforgeeks.org/mongodb-crud-operations/

17. Evaluate the Four types of NOSQL Databases with examples. (K5-LEVEL)

https://www.geeksforgeeks.org/types-of-nosql-databases/

18. Design any five Aggregate Functions using Advanced MongoDB with examples
(K6-LEVEL)

https://www.mongodb.com/docs/manual/aggregation/
Answer all the questions:

SECTION-A (KI-Knowledge/Remembering)

7×1-7

1. Define Pandas.

2 Describe 3D Toolkit.

3D Toolkit is a software library that provides tools for creating, manipulating, and
rendering 3D graphics and models.

3. Identify the Histograms.

Histograms are graphical representations of the distribution of data, showing


frequencies of data points within specified intervals.

4. Outline the Databases.

Databases are structured collections of data that are stored, managed, and accessed
electronically for efficient retrieval and manipulation.
5. Identify Data Types.

Data types define the kind of values a variable can hold, such as integers, floats, strings,
and booleans.

6. Tell MapReduce.
MapReduce is a programming model for processing and generating large datasets in
parallel across distributed clusters.
7. Recite Replication.

Replication is the process of duplicating data across multiple servers or locations to


ensure reliability, availability, and fault tolerance.

SECTION-B (K2-Comprehension/Understanding)

Answer all the questions:5x3-15

8. Classify the introduction to Pandas Data Structures.


Pandas data structures include Series (1D) and DataFrame (2D), providing efficient
handling and manipulation of labeled data.
https://www.geeksforgeeks.org/data-structures-in-pandas/

9. Summarize the Hierarchical indexing and Levelling.


Hierarchical Indexing, also known as MultiIndexing, is a powerful feature in Pandas that
allows you to have multiple levels of indexing on an axis (row or column). This capability
is particularly useful when dealing with high-dimensional data.
https://www.geeksforgeeks.org/how-to-use-hierarchical-indexes-with-pandas/

10. Extract the Types of Indexes with examples.


https://www.ibm.com/docs/en/ias?topic=indexes-types

11. Relate Pipeline Operations with examples.


https://www.geeksforgeeks.org/pipelines-python-and-scikit-learn/

12. Classify the term The Aggregation Framework.


https://www.geeksforgeeks.org/aggregation-in-mongodb-using-python/

SECTION-C (K3-Application/Applying)

Answer ALL the questions by choosing either/or:


13. a) Discover the Functionalities of Indexes.

https://www.geeksforgeeks.org/python-list-index/

(OR)

b) Relate Line Charts, Bar Charts and Pie Charts using Matplotlib with examples.

Python notes pdf la 32 page no la paru chlo

14. a) Discover the find and $where Queries with examples.


https://www.geeksforgeeks.org/sql-select-query/
Crt ans thryala edhuku

14 b) Sketch some of the advantages of MongoDB.


https://www.geeksforgeeks.org/mongodb-advantages-disadvantages/
(OR)

15. a) Relate Normalization versus Denormalization.


https://www.geeksforgeeks.org/difference-between-normalization-and-denormalization/

(OR)

15.

b) Complete the Configuring a Replica Set Configuration with examples.


https://www.mongodb.com/docs/manual/reference/replica-configuration/#:~:text=You%20
can%20access%20the%20configuration,reconfig()%20for%20more%20information.

https://hevodata.com/learn/mongodb-replica-set-config/

Endha 2du link la paru chlo

You might also like