This blog is based on Questions on python programming and we have tried to give you a wide variety. Various examples have been categorized based on different topics. For instance, List, strings, dictionary, tuple, sets, etc. All of these problems have been solved with multiple approaches. The questions are explained below with answers.
Also Read: Create a module in python: Different methods
Common Questions on Python Programming
Q1. What type of language is python? Programming or scripting?
In a general sense, Python is regarded as a general-purpose programming language. However, you should keep in mind that Python is capable of Scripting too.
Q2. What is the difference between list and tuples in Python?
|Lists are mutable i.e. it can be edited.||Tuples are immutable that means it cannot be edited.|
|Lists are slower in comparison to Tuples.||Tuples are faster than lists.|
|Syntax: list_1 = [10, ‘Chelsea’, 20]||Syntax: tup_1 = (10, ‘Chelsea’ , 20)|
Q3. What are python modules? Name some commonly used built-in modules in Python?
The files that contain Python code are known as Python modules. This code can be one of two i.e. functions classes or variables. A Python module is a .py file that contains executable code.
So, following are the commonly used built-in modules:
- data time
Q4. What are local variables and global variables in Python?
Global variables are those variables that are declared outside a function or in global space. In the program, these variables are accessible by any function.
Local Variables can be considered as any variable that is declared inside a function. This variable can be found in the local space only and not in the global space.
a=2 def add(): b=3 c=a+b print(c) add()
If you try to access the local variable outside the function add(), then it will show an error.
Q5. What are the key features of Python?
- Firstly, Python is an interpreted language. This means that on contrary to languages like C and its variants, there is no need to compile Python before running it. And other interpreted languages are PHP and Ruby.
- Secondly, Python is dynamically typed. So, it can be inferred that there is no need to state the types of variables when you declare them. You have the option to do things such as
x="I'm a string"without error.
- Lastly, Python code is quick in writing but when you run it, this becomes slower in comparison to the compiled languages. But the bright side is that Python allows the inclusion of C-based extensions. So, bottlenecks are often optimized in this way. A good example of this is the NumPy package as it is very quick.
Q6. Is python case sensitive?
The answer is Yes, the Python case is sensitive.
Q7. What is type conversion in Python?
The conversion of one data type into another is called as Type conversion.
float() – This converts any data type into float type.
ord() – This converts characters into integer.
hex() – This converts integers to hexadecimal.
oct() – This converts integer to octal.
tuple() – This function converts to a tuple.
set() – This function can return the type after converting to set.
list() – This function can convert any data type to a list type.
dict() – This function can convert a tuple of order (key, value) into a dictionary.
str() – This is used to convert integer into a string.
complex(real,imag) – This function can convert real numbers to complex (real,imag) number.
Q8. What is __init__?
In Python, _init_ is a constructor or method. So, this is used as a method to automatically call in order to allocate memory when a new object/ instance of a class is created. Every class has the _init_ method.
For instance, you can use it as follows:
class Employee: def __init__(self, name, age,salary): self.name = name self.age = age self.salary = 20000 E1 = Employee("XYZ", 23, 20000) # E1 is the instance of class Employee. #__init__ allocates memory for E1. print(E1.name) print(E1.age) print(E1.salary)
Q9. What is self in Python?
In basic terms, the Self is an instance of an object of a class. In terms of Python, it is explicitly but included as the first parameter. But when you compare it with Java then it is but not the case as in Java it’s optional. So, this is helpful to distinguish between the methods and attributes but of a class with local variables.
In the init method, the self variable refers but to the newly created object whereas in other methods, refers to the object in which method can be called.
Q10. What is a lambda function?
Lambda function is known as an anonymous function. So to sum it up but this function can have just one statement while it can have any number of parameters.
a = lambda x,y : x+y print(a(5, 6))
Q11. How does break, continue and pass work?
|Break||It allows loop termination when some condition is met. And the control transfers to the next statement.|
|Continue||It Allows skipping some part of a loop. This happens when some specific condition is met and the control is transferred to the beginning of the loop.|
|Pass||It is used when you need some block of code syntactically. But you have to skip its execution. This is a null operation. So, nothing happens when this is executed.|
Questions on python programming is a lot of people are searching out here. So, here we are some of the best questions on python programming that you can get and also providing the best answers available to pass an interview or a test. Hope you find these questions useful. Thank you for the read.