Python Basics - Interview Questions and Answers
It used to create small anonymous functions at run time.
sq=lambda x: x**2
print(sq(2))
Output:- 4
The best and easiest way is to use ‘unittest' python standard library is to test units/classes. The features supported are very similar to the other unit testing tools such as JUnit, TestNG.
Python does not support Arrays. However, you can use List collection type which can store an unlimited number of elements.
No, there are subtle but considerable differences-
- We use constructors to define and initialize non-static variables; we use methods to represent business logic to perform operations.
- Whenever we create an object, it executes a constructor; whenever we call a method, it executes a method.
- We must name a constructor in the name of the class; a method name can be anything.
Python files are first compiled to bytecode and are then executed by the host. “OR” Type python.pv at the command line.
The module is the way to structure a program. Each Python program file is a module, which imports other modules like objects and attributes.
The folder of Python program is a package of modules. A package can have modules or subfolders.
The break statement states that the function of the loop is over and to move on to the next block of code. For example, when the item being searched is found, there is no need to keep looping. The break statement comes into play here and the loop terminates and the execution moves on to the next section of the code.
Yes
A namespace is basically a system to make sure that all the names in a program are unique and can be used without any conflict.
A namespace is a mapping from names to objects. Python implements namespaces as dictionaries
These are the most commonally used basic Data Types in Python:
- Number
- Boolean
- String
- Dictionary
- List
- Sets
- Tuple
A tuple is a collection of objects which ordered and immutable. Tuples are sequences, just like lists.
The Key Difference between a List and a Tuple. The main difference between lists and tuples is the fact that lists are mutable whereas tuples are immutable
List comprehensions provide a concise way to create lists A list is traditionally created using square brackets. But with a list comprehension, these brackets contain an expression followed by a for clause and then if clauses, when necessary. Evaluating the given expression in the context of these for and if clauses produces a list.
list1 = [1, 0, -2, 4, -3]
list2 = [x**2 for x in list1 if x > 0]
print(list2)
Output:-[1,16]
There may be times in our code when we haven’t decided what to do yet, but we must type something for it to be syntactically correct. In such a case, we use the pass statement.
def func(*args):
pass
Using the reverse() method.
a = [1,2,3]
a.reverse()
print(a)
Output:- [3, 2, 1]
With the operators ‘in’ and ‘not in’, we can confirm if a value is a member in another.
'mic' in 'dynamicduniya'
Output:- True
A variable is a named reference to a value stored in memory. It allows you to store, modify, and reuse data.
You simply assign a value to a name:
x = 10
name = "Alice"
is_active = True
No, Python automatically determines the type of a variable when you assign a value.
No, variable names must start with a letter (a-z, A-Z) or an underscore (_) but not a digit
Yes, myVar
and myvar
are different variables.
int
, float
, str
, bool
. Example
x = 5 # int
y = 3.14 # float
name = "Bob" # str
flag = True # bool
Using type()
x = 10
print(type(x)) # Output: <class 'int'>
int
represents whole numbers, while float
represents numbers with decimal points.
a = 10 # int
b = 10.5 # float
Using type conversion functions
x = 5 # int
y = float(x) # Convert int to float
print(y) # Output: 5.0
False
, because 0 is considered "falsy" in Python.
Using single or double quotes
s1 = 'Hello'
s2 = "World"
Using +
operator:
str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
print(result) # Output: Hello World
Using len()
name = "Alice"
print(len(name)) # Output: 5
Using indexing
word = "Python"
print(word[0]) # Output: P
print(word[-1]) # Output: n
Using .upper()
and .lower()
text = "Hello"
print(text.upper()) # Output: HELLO
print(text.lower()) # Output: hello
True
and False
, which represent logical values.
is_active = True
is_admin = False
False
, because an empty string is considered "falsy" in Python.
==, !=, >, <, >=, <=
print(5 > 3) # Output: True
print(5 == 5) # Output: True
print(5 != 3) # Output: True
2
, because True
is treated as 1
and False
as 0
print(True + True) # Output: 2
print(False + True) # Output: 1
0
, because and
returns the first falsy value or the last truthy value.
+ (addition), - (subtraction), * (multiplication), / (division), // (floor division), % (modulus), ** (exponentiation)
/
returns a float, while //
returns an integer (floor division)
print(10 / 3) # Output: 3.3333
print(10 // 3) # Output: 3
1
, because %
gives the remainder of division.
8
, because **
is the exponentiation operator (2?
).
Using +=
operator:
x = 5
x += 1
print(x) # Output: 6
Using input()
, which returns a string
name = input("Enter your name: ")
print("Hello, " + name)
Using int()
age = int(input("Enter your age: "))
print("Next year, you will be", age + 1)
It will raise a ValueError
.
Using f-strings
name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")
Using round()
print(round(3.14159, 2)) # Output: 3.14
Random Blogs
- Datasets for Exploratory Data Analysis for Beginners
- Python Challenging Programming Exercises Part 1
- Why to learn Digital Marketing?
- OLTP vs. OLAP Databases: Advanced Insights and Query Optimization Techniques
- Convert RBG Image to Gray Scale Image Using CV2
- Loan Default Prediction Project Using Machine Learning
- Data Analytics: The Power of Data-Driven Decision Making
- Understanding OLTP vs OLAP Databases: How SQL Handles Query Optimization
- What is YII? and How to Install it?
- The Ultimate Guide to Data Science: Everything You Need to Know