Neha Answers
Neha Answers
Answers:
1.Ans)
Python programming language is a general-purpose, interpreted, and high-level language
that mainly offers code readability. It is largely used by professional programmers and
developers across a variety of fields, including Web Development and Machine Learning.
Like all other programming languages that you must have heard or read about, or you might
have used for various reasons,
Benefits of Python :
1) Easy-to-learn and Easy-to-use
2) Improves Productivity
3) Free and open-source:
4) Portable
5) Easy-to-integrate with Other Programming Languages
6) Massive Libraries
2.Ans)
An interpreted language is a type of programming language in which the source code is
executed line by line by an interpreter during runtime, rather than being translated into
machine code beforehand. In contrast to compiled languages, where a compiler translates
the entire source code into machine code before execution, interpreted languages allow for
a more dynamic and flexible approach to code execution. In an interpreted language, the
interpreter reads the source code, interprets each line, and executes the corresponding
instructions. This process happens on-the-fly, making it possible to see the results of code
immediately without the need for a separate compilation step. This approach provides
advantages in terms of ease of development, debugging, and platform independence.
3.Ans)
Lists and Tuples in Python are two classes of Python Data Structures. The list structure is
dynamic, and readily changed whereas the tuple structure is static and cannot be
changed. This means that the tuple is generally faster than the list. Lists are denoted by
square brackets and tuples are denoted with parenthesis.
Difference between List and Tuple:
4. Ans):
5,Ans)
Lambda functions in Python, also known as anonymous functions, are small, unnamed
functions defined using the lambda keyword. They are primarily used for short, throwaway
functions that are not going to be reused elsewhere in your code.
Example:
Output:
8
8
6.Ans)
Negative indexing in Python is a way to access elements of a sequence (such as a list, tuple,
or string) from the end of the sequence rather than from the beginning. Negative indices
start from -1, which corresponds to the last element of the sequence, -2 to the second-to-
last element, and so on.
7.Ans):
In Python, strings can be converted to numbers using built-in functions like int(), float(), and
complex(). These functions convert a string representation of a number into an actual
number of the appropriate type.
8.Ans):
In Python, self is a conventional name used for the first parameter of methods in a class. It
represents the instance of the class and allows access to the attributes and methods of the
class in object-oriented programming.
9.Ans):
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
Neha
10.Ans):
11.Ans)
Slicing in Python refers to extracting a subset of elements from a sequence (such as a list,
tuple, or string) based on specified start, stop, and step indices. It allows you to create a
new sequence containing a portion of the original sequence.
12.Ans):
List comprehensions are a concise way to create lists in Python by applying an expression to
each item in an existing iterable (like a list, tuple, or string) and optionally including a
condition to filter items. They provide a more compact and readable alternative to
traditional loops for generating lists.
Example:
13.Ans):
The statement "Hello World"[::-1] reverses the string "Hello World" using slicing with a step
of -1.
Output:
'dlroW olleH'
14.Ans):
The given expression ["".join([i, j]) for i, j in zip(list1, list2)] creates a new list by
concatenating corresponding elements from list1 and list2 using the zip() function and list
comprehension.
zip(list1, list2) pairs up elements from list1 and list2 element-wise. It creates an iterator of
tuples where each tuple contains corresponding elements from list1 and list2.
"".join([i, j]) concatenates the elements i and j from each tuple into a single string.
The list comprehension [...] iterates over each tuple of paired elements generated by zip()
and applies the concatenation operation.
Output:
15.Ans):
Inheritance in Python is a mechanism that allows a class (subclass or derived class) to inherit
properties and behaviors (attributes and methods) from another class (superclass or base
class). This promotes code reusability and helps create a hierarchical structure among
classes.
Key Concepts
1. Base Class (Superclass): The class whose properties and behaviors are inherited by
another class. It's also referred to as the superclass or parent class.
2. Derived Class (Subclass): The class that inherits properties and behaviors from
another class. It's also referred to as the subclass or child class.
3. Syntax:
python
Copy code
class BaseClass:
# Base class definition
class DerivedClass(BaseClass):
# Derived class definition
4. Inheriting Attributes and Methods: The subclass automatically gains access to all
attributes and methods of the superclass.
5. Overriding Methods: The subclass can override methods of the superclass by
providing its own implementation.
6. Adding New Features: The subclass can add new attributes and methods or modify
existing ones.
16.Ans):
Polymorphism, in the context of object-oriented programming, refers to the ability of
different objects to respond to the same message or method call in different ways. It allows
objects of different classes to be treated as objects of a common superclass, providing
flexibility and extensibility in code design.
Key Concepts
Types of Polymorphism
17.Ans) :
def check_even_odd(number):
if number % 2 == 0:
return "Even”
else:
return "Odd"
print(check_even_odd(5))
print(check_even_odd(10))
OUTPUT:
Odd
Even
18.Ans):
class Student:
self.name = name
self.age = age
self.grade = grade
def print_info(self)
print(f"Name: {self.name}")
print(f"Age: {self.age}")
print(f"Grade: {self.grade}")
student1.print_info()
OUTPUT:
Name: Naha
Age: 22
Grade: A
19.Ans):
def count_consonants(word):
consonants = 'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ'
count = 0
if char in consonants:
count += 1
return count
word = "Hello"
OUTPUT:
20.Ans):
common_elements = []
if item in list2:
common_elements.append(item)
return common_elements
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
OUTPUT:
21.Ans):
def second_largest_number(numbers):
OUTPUT:
22.Ans):
expected_sum = n * (n + 1) // 2
actual_sum = sum(numbers)
numbers = [1, 2, 4, 5, 6]
n=6
OUTPUT:
Missing number: 3
23.Ans):
def is_palindrome_after_deletion(s):
def is_palindrome(string):
if is_palindrome(s):
return True
for i in range(len(s)):
if is_palindrome(modified_string):
return True
return False
string = "radar"
OUTPUT:
24.Ans):
def most_repeated_element(lst):
count_dict = {}
if element in count_dict:
count_dict[element] += 1
else:
count_dict[element] = 1
max_count = max(count_dict.values())
lst = [1, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5]
OUTPUT:
25.Ans):
def sum_of_series(n):
total = 0
total += 1 / i
return total
n=5
OUTPUT: