python5thsem2.0 updated
python5thsem2.0 updated
Python ::)
Group A::)
1) What is slicing in Python?
Slicing in Python allows you to access a portion or subset of a
sequence (like a string, list, or tuple) using a range of indices.
It is done using the colon : operator within square brackets.
Syntax: sequence[start:stop:step]
• start: The starting index (inclusive, default is 0).
• stop: The ending index (exclusive).
• step: The interval between indices (default is 1).
For example ::
my_list = [0, 1, 2, 3, 4, 5]
print(my_list[1:5]) # Output: [1, 2, 3, 4]
2) How do you define a lambda function in Python?
A lambda function is an anonymous function defined using
the lambda keyword. It can have multiple arguments but only
a single expression.
Syntax:
python
Copy code
lambda arguments: expression
for example ::
square = lambda x: x**2
print(square(5)) # Output: 25
3) Two differences between List and Tuple:
A list is a data structure in Python that is a mutable, or
changeable, ordered sequence of elements.
List
Lists are used to store multiple items in a single variable.
Lists are one of 4 built-in data types in Python used to store
collections of data, the other 3 are Tuple, Set, and Dictionary,
all with different qualities and usage.
Vs :)
For example ::
my_list = [1, 2, 3] # List
my_tuple = (1, 2, 3) # Tuple
4) What are Python libraries? Name a few of them.
Python libraries are pre-written collections of functions and
modules that simplify complex tasks and enhance
functionality. They save time and effort by providing reusable
code for various applications.
Popular Python Libraries:
• NumPy: For numerical computing.
• Pandas: For data manipulation and analysis.
• Matplotlib: For data visualization.
• Scikit-learn: For machine learning.
• TensorFlow: For deep learning.
Group B::
9) How do you create a class in Python?
A class in Python is created using the class keyword. It acts as
a blueprint for creating objects with attributes (data) and
methods (functions).
Example:
class Person:
def __init__(self, name, age): # Constructor
self.name = name
self.age = age
def greet(self):
print(f"Hello, my name is {self.name} and I am {self.age}
years old.")
# Create an object
person1 = Person("Alice", 25)
person1.greet()
releted question --
Object of Python Class
In Python programming an Object is an instance of a Class. A
class is like a blueprint while an instance is a copy of the class
with actual values.
Class
A class is a user-defined blueprint or prototype from which
objects are created. Classes provide a means of bundling data
and functionality together.
Long question :
15) Python Code to Check if a Number is an Armstrong
Number
An Armstrong number (or narcissistic number) is a number
that is equal to the sum of its digits each raised to the power
of the number of digits.
Example Explanation (153):
• 153 has three digits: 13+53+33=1+125+27=1531^3 + 5^3
+ 3^3 = 1 + 125 + 27 = 15313+53+33=1+125+27=153, so
it is an Armstrong number.
Code:
16) Answers:
a. Explain how to create a dictionary in Python:
A dictionary is a collection of key-value pairs, where each key
is unique. Dictionaries are created using curly braces {} or the
dict() function.
Example:
# Creating a dictionary
my_dict = {"name": "Alice", "age": 25, "city": "New York"}
# Accessing a value
print(my_dict["name"]) # Output: Alice
Extra question:
INTERNAL QUESTIONS :
1. array in python ?
In Python, arrays can be handled using the list data type or
the array module. Assuming you're referring to lists, here
are three commonly used built-in methods for arrays (lists):
1. append()
• Purpose: Adds an item to the end of the list.
• Example:
python
Copy code
my_list = [1, 2, 3]
my_list.append(4)
print(my_list) # Output: [1, 2, 3, 4]
2. extend()
• Purpose: Adds all elements of an iterable (like another
list) to the end of the list.
• Example:
python
Copy code
my_list = [1, 2, 3]
my_list.extend([4, 5])
print(my_list) # Output: [1, 2, 3, 4, 5]
3. pop()
• Purpose: Removes and returns an element from the
list. By default, it removes the last element, but you
can specify the index.
• Example
my_list = [1, 2, 3, 4]
removed_item = my_list.pop()
print(my_list) # Output: [1, 2, 3]
print(removed_item) # Output: 4
Updating a Value
You can update a value by assigning a new value to
an existing key.
Example:
my_dict = {"name": "Alice", "age": 25, "city": "New
York"}
my_dict["age"] = 26 # Change the value of the "age"
key
print(my_dict)
# Output: {'name': 'Alice', 'age': 26, 'city': 'New York'}
Code :
# Create a dictionary
my_dict = {
"a": 1,
"b": 2,
"c": 3
}
print(multi_value_dict)
output:
class Child(Parent):
def greet(self):
super().greet() # Call the parent class method
print("Hello from Child!")
obj = Child()
obj.greet()
output :
Hello from Parent!
Hello from Child!
Program ::
write a program to display the fibbonachi sequence up to n
th term ? write output also?
def fibonacci(n):
# Initialize the first two terms of the Fibonacci sequence
a, b = 0, 1
# Loop through the sequence until the n-th term
for _ in range(n):
print(a, end=" ")
a, b = b, a + b # Update values for the next term
# Get input from the user for the number of terms
n = int(input("Enter the number of terms in the Fibonacci sequence:
"))
fibonacci(n)
output :
0 1 1 2 3 5 8 13 21 34
if choice == '1':
print(f"{num1} + {num2} = {add(num1, num2)}")
elif choice == '2':
print(f"{num1} - {num2} = {subtract(num1, num2)}")
elif choice == '3':
print(f"{num1} * {num2} = {multiply(num1, num2)}")
elif choice == '4':
print(f"{num1} / {num2} = {divide(num1, num2)}")
else:
print("Invalid Input! Please select a valid option.")
return vowels