PSPP VQ
PSPP VQ
PSPP VQ
Python is a high-level, interpreted programming language known for its simplicity and readability. Its key
features include dynamic typing, automatic memory management, and a rich standard library. It also has
strong community support and is used for various applications such as web development, data analysis,
and machine learning.
LIST TUPLE
Lists are mutable. Tuples are immutable.
Denoted by square brackets. Denoted by parenthesis.
Slower due to dynamic resizing and modifications. Faster due to fixed size and immutability.
SET DICTIONARY
It is a collection of unique elements. It is a collection of key-value pairs.
Created using the set() function. Created using the dict() function.
In Python, "self" refers to the instance of a class that a method calls. It is typically used within a method
to refer to instance variables or contact other instance methods. When a method calls on an instance of a
class, the self keyword accesses the instance's attributes and methods.
In Python, a lambda function is a small, anonymous function that can have any number of arguments but
can only have one expression. Lambda functions are a shorthand for creating simple functions that are
only needed once. They are made using the lambda keyword, followed by the function's arguments and a
colon, and then the expression for evaluation.
In Python 2. x, "range" and "xrange" generate integer sequences. However, "range" yields a list of
integers simultaneously, while "xrange" generates them on-the-fly as needed. It can be more memory-
efficient when working with large ranges, as it generates only one number simultaneously. In Python 3. x,
"range" has been modified to behave like "xrange," and "xrange" no longer exists.
In Python, the __init__ method is a unique method that looks like a constructor, though it is not. It is used
for instantiating objects of a class. It initializes the object's attributes with default values or values passed
during object creation. This method is commonly used to set up an object's initial state and can also be
used to perform other initialization tasks.
In Python, the type() function determines the type of a variable. For example, type(variable) will return
the style of the variable.Alternatively, the isinstance() function can be used to check if a variable is an
instance of a particular class. For example, isinstance(variable, int) will return True if the variable is an
instance of the int class.
The "is" operator checks if two objects are the same object in memory. It returns True if both objects are
identical, meaning they have the same memory address.On the other hand, the "==" operator checks if
two objects have the same value. It returns True if the values of the two objects are equal, regardless of
whether they are the same object in memory.
10. What are decorators in Python, and how are they used?
In Python, decorators are functions that help the compiler know about the unique property associated with
a particular function. By wrapping a function with another function, decorators can modify the input or
output values of the function or add functionality to it before or after it executes. Decorators are often
used to add cross-cutting concerns like logging, caching, or authentication in a reusable manner.
Python is useful for various applications, including web development, scientific computing, data
analysis, machine learning, artificial intelligence, and automation. It is used by organizations such
as Google, NASA, and Netflix and is also widely used in academia.
One of the main advantages of Python is its simplicity and readability. The syntax is easy to
understand and write, making it accessible to every programmer.
def keyword is used to create a function in Python, followed by the name of the function and any
parameters the function will accept in parentheses. The code inside the function should be indented to
show that it is a part of it. Here is an example:
A list is a mutable data type in Python that uses square brackets [] to store an ordered collection of
items.On the other hand, a tuple is an immutable data type in Python that uses parentheses () to store an
ordered collection of items. Tuples are faster and more memory-efficient than lists, especially for larger
data collections.
A module in Python is a file containing Python code that can be used in other Python programs.A module
is a self-contained unit of code that can include variables, functions, and classes that can be accessed and
used in other Python programs. By organizing code into modules, you can avoid duplicating code across
different programs and instead import and use the same code in multiple places, making it easier to
maintain and reuse your code.Python has many built-in modules that can be used for various purposes,
such as working with files, network communication, data processing, and more. In addition, third-party
modules can be installed and used in Python programs to extend their functionality.To use a module in a
Python program, you first need to import it using the import statement. Here is an example:
import math
result = math.sqrt(12)
print(result)
You can also try this code with Online Python Compiler
Break and Continue are two keywords in Python that are used to change the flow of a loop. These
keywords are used inside loops, such as for and while loops.Both keywords are used to change the flow
of a loop, but they have different effects on the loop:
Break - It exits the loop entirely and continues with the next statement after the loop.
Continue- It skips the current iteration and moves on to the next iteration of the loop.
for i in range(10):
if i == 5:
continue
print(i)
Let's consider the following code to explain iteration over a list in Python.
numbers = [1, 2, 3, 4, 5]
for num in numbers:
if num % 2 == 0:
print(num, "is even")
else:
print(num, "is odd")
You can also try this code with Online Python Compiler
Explanation: We have a list called numbers containing five integers. We use a for loop to iterate over the
list, and on each iteration, we assign the current item in the list to the variable num. Then, we check if the
number is even or odd using the modulus operator (%) and print out the result.The 'for' loop iterates over
the list from the first to the last item and executes the indented code block once for each item.
A dictionary is a collection of key-value pairs that store and retrieve data. In other words, a dictionary is
like a map that connects keys to values.
Example:
student = {
"name": "Jaideep",
"age": 22,
"major": "Computer Science"
}
print(student["name"])
print(student["age"])
print(student["major"])
18. Write a Python factorial program without using if-else, for, and ternary operators.
We can use a recursive function that calculates the factorial of a given number without using if-else, for,
and ternary operators:The function recursively calls itself until it reaches the base case of n=1, at which
point it returns 1. Each recursive call multiplies the current value of n by the result of the previous call,
effectively calculating the factorial.
def factorial(n):
return (n==1) or (n * factorial(n-1))
print(factorial(4))
You can also try this code with Online Python Compiler
Python
if number == sum:
print(number, "is an Armstrong number")
else:
print(number, "is not an Armstrong number")
20. What is the difference between deep and shallow copying of an object in Python?
Shallow copying creates and populates a new object referencing the original object's data. Suppose the
original object contains mutable objects as elements. In that case, the new entity will reference the same
mutable objects, and changes to the mutable objects are reflected in both the new and original objects.
Deep copying creates a new object and recursively copies the original object's data and the data of any
things it references. It means that the new entity and its simulated data are entirely independent of the
original object and its data.