1170059794-MQP-1 Answers PYTHON
1170059794-MQP-1 Answers PYTHON
ANSWERS
1) a) Define Python programming. List and explain any five features of python.
Easy to learn and use: It is much easier to learn than any other programming languages like C, C++, Java, Ruby
and Swift. It has few keywords and thus very less syntactical constraints. Thus the language is very developer friendly
and easy to pick up.
Free and Open Source → Python is a open sourced language. Thus it is publicly available to download
and use and even anyone can contribute to its development.
Easy to maintain → Python code base is very easy to maintain.
Object Oriented Language → Python is an Object Oriented language. Thus is provides all the key
features of an Object Oriented Programming language like Encapsulation, Inheritance, Abstraction and
Polymorphism. Nevertheless, Python also offers a scope to write code as in a Procedural Programming
Language.
Extensibility → Python is very extensible. We can write and integrate some Python code into C and
C++ and hence compile it using its irrespective compilers.
Portability → Python provides high portability. Thus same piece of code can be run in all platforms of
Windows, UNIX and Linux.
Large Standard Library → Python has a large set of large standard library which possess some inbuilt
rich set of functions such that we need not to write code for every single thing.
Dynamically Typed Language → Python is a dynamically typed language. Here, we need not to
declare the type of the variable like int, float and char. It is also auto-set as we assign a value to the variable.
1 .b) With proper syntax and examples explain any three type conversion
functions in python.
There can be two types of Type Casting in Python –
Implicit Type Casting
Explicit Type Casting
In implicit type conversion, Python converts data types automatically, without any user
involvement. This happens when we perform operations between different data types. For
example:
a=5 # an integer
In explicit type conversion, Python requires user involvement to convert one data type to another.
This is done using type casting functions such as:
a=5 # an integer
x = 3.14 # a float
print(y) # Output: 3
m = 42 # an integer
Operators in Python have a specific precedence and associativity, which determine the order in
which they are evaluated when multiple operators appear in an expression.
Precedence refers to the priority of an operator over other operators, with higher precedence
operators evaluated first. For example, multiplication and division have a higher precedence than
addition and subtraction. If an expression contains operators of equal precedence, then the order
of evaluation is determined by their associativity.
Associativity refers to the direction in which an operator is evaluated when it has the same
precedence as other operators. In Python, most operators have left-to-right associativity, which
means that expressions are evaluated from left to right.
return a + b
result = sum(3, 5)
print(result) # output: 8
return a + b
print(result) # output: 8
print(a +b)
greet(12) # output: 22
Indexing: One way is to treat strings as a list and use index values. For example,
Local Variables:
def my_function():
x = 10 # x is a local variable
print("Value of x inside the function:", x)
my_function()
Global Variables:
x = 10 # x is a global variable
def my_function():
A list can contain elements of different data types, such as integers, floats, strings, or
even other lists.
my_list = [1, 2, 3, 2, 4, 2, 5]
count = my_list.count(2)
my_list = [3, 5, 1, 4, 2]
sorted_list = sorted(my_list)
Here's an example:
my_list.remove(2)
To create a new copy of the list, we need to perform list cloning. List cloning creates a completely new
copy of the original list, so any changes made to the cloned list will not affect the original list and vice
versa.
cloned_list = original_list.copy()
cloned_list[2] = 6
output-
freq = {}
if char in freq:
freq[char] += 1
# Otherwise, add the character to the dictionary with a count of
1
else:
freq[char] = 1
output of code
1. Encapsulation: the ability to bundle data and methods together into a single unit, called
a class, and restrict access to the data from outside the class.
2. Inheritance: the ability to create new classes by deriving them from existing classes,
which inherit the attributes and methods of the parent class.
3. Polymorphism: the ability to use a single interface to represent multiple types of
objects.
4. Abstraction: the ability to simplify complex systems by breaking them down into
smaller, simpler components.
class Person:
self.name = name
self.age = age
def greet(self):
Ullas.greet()
Ramesh.greet()
output
In Python, exceptions are errors that occur during program execution. They can be
handled using try-except blocks to gracefully handle errors and continue executing
the program. Python provides many built-in exceptions for handling different types
of errors. Some of the commonly used built-in exceptions are:
Handling exceptions in Python is a powerful way to write robust code that can
handle errors gracefully and continue executing even in the face of unexpected
input or conditions
With suitable examples discuss the difference between function overloading and [07 Marks] CO5 L2
function overriding concepts in Python.
Function Overloading: Function overloading refers to defining
multiple functions with the same name in a single class, but with different
parameters. The function to be called is determined at compile time
based on the number, order, and types of the arguments passed to the
function.
class MyClass:
print("x =", x)
print("y =", y)
obj = MyClass()
# Call func with one argument
obj.func(10)
obj.func(10, 20)
class Animal:
def make_sound(self):
class Dog(Animal):
def make_sound(self):
print("Bark")
class Cat(Animal):
def make_sound(self):
print("Meow")
my_animal = Animal()
my_dog = Dog()
my_cat = Cat()
Here are the different modes that can be used while opening files in Python: