0% found this document useful (0 votes)
21 views9 pages

1) What Is Python?: List Tuple

Uploaded by

xcsplcareers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views9 pages

1) What Is Python?: List Tuple

Uploaded by

xcsplcareers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1)What is Python?

Python is an interpreted, object-oriented, high-level programming language


known for its readability and ease of use. Developed by Guido van Rossum, it
was first released in 1991 and is widely used for web development, data
analysis, machine learning, automation, and more due to its dynamic
semantics and extensive libraries.

Is Python a compiled language or an interpreted


language?
Python is an interpreted language, which means the source code of a Python
program is converted into byte-code then it is executed by the Python virtual
machine(P.V.M).

2)Difference between list and tuple?

1. list is ordered, mutable, allows duplicates and heterogenous.


Denoted by []
2. Tuple is ordered, immutable, allows duplicates and heterogenous.
Denoted by ()

3)Difference between list and dictionary?


1. List: ordered, mutable, Allows duplicates and
heterogeneous. Represented by square brackets [].
2. Dictionary: ordered,mutable, Does not allow duplicate keys and
heterogeneous . Represented by curly braces {key: value}.

4)Difference between list and set?


1. list is ordered, mutable, allows duplicates and heterogenous.Denoted by
[]
2. Set is unordered, mutable, does not allow duplicates and heterogenous.
Denoted by {}

5) What is the difference between a Set and Dictionary?


1. Set: A collection of unique items, unordered , accessed by iterating.
Useful for operations like union and intersection.
2. Dictionary: A collection of key-value pairs, where keys are unique, and
values are accessed by keys. Ideal for mapping relationships.

6)Difference between list and array ?


1. List: Holds elements of different data types, defined with square brackets
[], and supports many operations.
2. Array: Stores elements of the same data type, requires the array module,
and more memory-efficient for large datasets.

7)What is list comprehension?


List comprehension is a concise way to create lists using a single line of
code. It combines a for loop and optional conditions to generate a new list
based on an existing one.

EX: squares = [x**2 for x in range(10)]

EX: list1 = [2, 3, 4, 5, 6]

>>> [each*each for each in list1]

8)What is Dictionary comprehension?


Dictionary comprehension is a concise way to create dictionaries in a single
line, using a similar structure to list comprehension. It combines a for loop and
optional conditions to generate key-value pairs.

EX: square_dict = {x: x**2 for x in range(10)}

EX: >>> squares = {x: x**2 for x in range(5)}

9)What is a lambda function and give an example?

lambda function is a anonymous function defined with the keyword lambda. It


can take multiple arguments but can have only one expression, and it returns
the result of particular expression.

 Lambda functions are often used for quick, simple operations where
defining a full function is unnecessary, such as in sorting or filtering lists.

EX: add = lambda x, y: x + y


print(add(2, 3))

10)Explain exception handling in python ?


Exception handling is done using the try, except, else, and finally blocks to
manage errors gracefully.

1) try block: This is where you put the code that might cause an error.
Python will “try” to run it, and if there’s an error, it’ll stop here and jump to
the except block.
2) except block: This block runs only if there’s an error in the try block. You
can have multiple except blocks to handle different types of errors.
3) else block (optional): This block runs if there was no error in the try block.
It let’s you write code that only makes sense if an exception wasn't
thrown.
4) finally block (optional): This block always runs, whether there was an
error or not. It’s usually for cleanup tasks, like closing files or freeing
resources.

In short:

 try to run the code.


 except handles errors if they happen.
 else runs if everything goes well.
 finally runs no matter what happens.

Type Error: This exception is raised when an operation or function is applied


to an object of the wrong type, such as adding a string to an integer.
Name Error: This exception is raised when a variable or function name is not
found in the current scope.
Index Error: This exception is raised when an index is out of range for a list,
tuple, or other sequence types.
Key Error: This exception is raised when a key is not found in a dictionary.
Value Error: This exception is raised when a function or method is called with
an invalid argument or input, such as trying to convert a string to an integer
when the string does not represent a valid integer.
Attribute Error: This exception is raised when an attribute or method is not
found on an object, such as trying to access a non-existent attribute of a class
instance.
Io Error: This exception is raised when an I/O operation, such as reading or
writing a file, fails due to an input/output error.
Zero-division Error: This exception is raised when an attempt is made to
divide a number by zero.
Import Error: This exception is raised when an import statement fails to find
or load a module.

11)What are *args(Arbitrary Positional Arguments) and


**kwargs(Arbitrary Keyword argument)?
*args and **kwargs are used for allowing functions to accept a variable
number of arguments.

1. *args: Allows a function to accept any number of positional arguments


as a tuple.
2. **kwargs: Allows a function to accept any number of keyword
arguments as a dictionary.
12)How are arguments passed by value or by reference in
Python?
Pass by reference means that “ Instead of passing the actual value to a
function, we pass the address of the variable.” This way, any changes made
in the function directly affect the original variable.

Pass by value means that “ A copy of the actual value is passed to the
function. ” This way, any changes made in the function do not affect the
original variable. The function works only with this copy.

13)What is pass statement ?


The pass statement in Python is a placeholder that does nothing when
executed. It's used when a statement is syntactically required but no action is
needed, allowing you to define an empty code block.

14)What is continue statement ?


The continue statement in Python skips the current iteration of a loop and
moves to the next one. It is often used to skip certain conditions within loops
without stopping the entire loop.

15)What is break statement ?


The break statement in Python immediately exits a loop, stopping further
iterations. It’s often used when a specific condition is met and there’s no need
to continue the loop.

16)What is swapcase function in Python?


The swapcase() function in Python returns a new string where all uppercase
letters are converted to lowercase and all lowercase letters are converted to
uppercase.

17)Difference between for loop and while loop in Python ?


‘for loop‘ is used to iterate over a sequence of items, such as a Python tuple,
list, string, or range. The loop will execute a block of statements for each item
in the sequence.

‘while loop’ is used to repeatedly execute a block of statements while a


condition is true. The loop will continue to run as long as the condition
remains true.
18)Can we Pass a function as an argument in Python?
Yes, in Python, we can pass a function as an argument to another function.
This is possible because functions in Python are treated as first-class objects,
meaning they can be assigned to variables, stored in data structures, and
passed as arguments.

19)What is the difference between xrange and range


functions?
The range() function returns a sequence of numbers, in a given range. The
most common use of it is to iterate sequences on a sequence of numbers
using Python loops. he range() function in Python is used in Python 3.x.

The xrange() function in Python is used to generate a sequence of numbers,


similar to the Python range() function. The Python xrange() is used only in
Python 2.x

20)What is the difference between a shallow copy and a


deep copy?
Shallow Copy stores the references of objects to the original memory
address.

 It reflects changes made to the new/copied object in the original object.


 It stores the copy of the original object and points the references to the
objects.
 shallow copy is faster.

Deep copy stores copies of the object’s value.

 It doesn’t reflect changes made to the new/copied object in the original


object.
 It stores the copy of the original object and recursively copies the objects
as well.
 Deep copy is comparatively slower.

21)What is docstring in Python?


A Python doc-string is a string used to document a Python module, class,
function or method.Doc-strings are defined with triple quotes (""" """) directly
below the function, class, or module definition. Doc-strings can be accessed
using the .__doc__ attribute, and they are essential for creating readable and
maintainable code.

22)What are Built-in data types in Python?


The standard built-in data types in Python are:

Numeric Types: Represents numeric values, including integers (int), floating-


point numbers (float), complex numbers (complex), and Booleans (bool).

Sequence Types: Ordered collections that can hold similar or different types
of data. Examples include:

 String (str): A sequence of characters.


 List (list): An ordered, mutable collection.
 Tuple (tuple): An ordered, immutable collection.
 Range (range): A sequence of numbers used in loops.

Mapping Type: Represents key-value pairs where each key maps to a


specific value. Python’s main mapping type is:

 Dictionary (dict): A mutable collection of key-value pairs.

Set Types: Unordered collections with unique elements, ideal for membership
testing and eliminating duplicates. They include:

 Set (set): Mutable, unordered collection of unique items.


 Frozenset (frozenset): Immutable version of a set.

23)What is a decorator?
A decorator in Python is a special function that modifies or extends the
behavior of another function or method without changing its actual code. It
"wraps" the original function, adding extra functionality that runs either before,
after, or around the main function.

24)What is a generator?
A generator in Python is a function that produces values one at a time, using
the yield statement instead of return. Each call to the generator resumes
where it left off, making it memory-efficient for handling large sequences.

25)What is the init() method?

The __init__() method in Python is a special method used to initialize a newly


created object (instance) of a class. It's commonly known as the constructor.
This method is called automatically when an object is created from a class,
allowing you to set initial values for the object's attributes.

26)What are user defined functions in python?


User Defined Functions(UDFs) are the functions defined by user to perform
specific task. Created using the “def” keyword.It can take any number of
parameters as inputs called “arguments”. A user-defined function can have
only one return statement but it can return multiple objects.

27)What is If__main in python ?


if __name__ == "__main__": statement is used to determine whether a
Python script is being run as the main program or if it is being imported as a
module into another script.

Key Points:

 Main Program: When the script is run directly, __name__ is set to


"__main__". This allows you to execute specific code only when the
script is the main program.
 Module Import: If the script is imported into another module, __name__
is set to the module's name, and the code under the if __name__ ==
"__main__": block will not be executed.

Purpose:

This construct is commonly used to:


1. Prevent certain code from running when the file is imported.
2. Organize code into functions or classes that can be reused in other
scripts.

28)What is Inheritance?

 Inheritance can be defined as the mechanism that permits the newly


created classes to inherit the methods and attributes of the existing class
or parent class.
 The classes that inherit the methods and attributes from the parent class
are called subclass and the existing class is called a superclass.
 As soon as a subclass inherits the superclass, it gains all of the methods
and attributes of the superclass. This allows us to reuse the code as well
as we can extend or modify the behavior of the superclass.
Different types of inheritance:

Single Inheritance :

 Single inheritance can be defined as an inheritance where


a subclass or derived class inherits from a single superclass or parent
class.
 In simple words, the derived class or subclass has only one direct parent
class.

Multiple Inheritance:

 Python supports multiple-class inheritance and can be defined as an


inheritance where a subclass or child class inherits from more than one
superclass.
 In short, a subclass has more than one direct parent class or superclass.

Multilevel Inheritance:

 Multi-level inheritance can be defined as where a subclass inherits from


the single subclass and then another subclass inherits from the first
subclass.
 By this, the second subclass can access all the attributes and methods
from both the first subclass and the superclass.
Hierarchical Inheritance:

 Hierarchical inheritance can be defined as when more than one derived


class inherits from a single base class.

Hybrid Inheritance:

 This type of inheritance is a blend of different inheritance which means it


has a combination of two different types of inheritance like multiple or
multi-level inheritances or multiple or single inheritances.

You might also like