Category: interview Question
-
modules and packages in Python?
Python packages and Python modules are two mechanisms that allow for modular programming in Python. Modularizing has several advantages – Modules, in general, are simply Python files with a .py extension and can have a set of functions, classes, or variables defined and implemented. They can be imported and initialized once using the import statement. If partial functionality is…
-
use of self in Python?
Self is used to represent the instance of the class. With this keyword, you can access the attributes and methods of the class in python. It binds the attributes with the given arguments. self is used in different places and often thought to be a keyword. But unlike in C++, self is not a keyword in…
-
floor a number in Python?
To floor a number in Python, you can use the math.floor() function, which returns the largest integer less than or equal to the given number. import math n = 3.7 F_num = math.floor(n) print(F_num) Output 3
-
for loop and while loop in Python?
for i in range(5): print(i) c = 0 while c < 5: print(c) c += 1 Output 0 1 2 3 4 0 1 2 3 4
-
concatenate two lists in Python?
We can concatenate two lists in Python using the +operator or the extend() method. 1. Using the + operator: This creates a new list by joining two lists together. a = [1, 2, 3] b = [4, 5, 6] res = a + b print(res) Output [1, 2, 3, 4, 5, 6] 2. Using the extend() method:…
-
What is init in Python?
The __init__() method is known as a constructor in object-oriented programming (OOP) terminology. It is used to initialize an object’s state when it is created. This method is automatically called when a new instance of a class is instantiated. Purpose: Example: We have created a book_shop class and added the constructor and book() function. The constructor will store the book title…
-
Python lists and tuples?
Lists and tuples are fundamental Python data structures with distinct characteristics and use cases. List: Example: a_list = [“Data”, “Camp”, “Tutorial”] a_list.append(“Session”) print(a_list) # Output: [‘Data’, ‘Camp’, ‘Tutorial’, ‘Session’]Powered By Tuple: Example: a_tuple = (“Data”, “Camp”, “Tutorial”)print(a_tuple) # Output: (‘Data’, ‘Camp’, ‘Tutorial’)