Python Interview Questions-Ccbp Format
Python Interview Questions-Ccbp Format
Applications of Python
Python is a versatile language that has applications in almost every field
Artificial intelligence (AI)
Machine Learning (ML)
Big Data
Smart Devices/Internet of Things (IoT)
Cyber Security
Game Development
Backend Development, etc
4. Is Python case-sensitive?
Yes, Python is case-sensitive.
a // b
print(3 // 2) //output : 1
BODMAS
The standard order of evaluating an expression
Brackets (B)
Orders (O)
Division (D)
Multiplication (M)
Addition (A)
Subtraction (S)
(5 * 2) + (3 * 4 + 4 / 2)
(10) + (3 * 4 + 2)
(10) + (12 + 2)
(10) + (14)
24
Integers
Float
Complex Numbers
Some of the mutable data types in Python are list, dictionary, set and user-defined classes.
Some of the immutable data types in Python are tuple, integer, boolean, string, etc.
Immutable Data Types Mutable Data Types
Cannot be changed after creation Can be modified after creation
Can be used as dictionary keys Cannot be used as dictionary keys
Examples: int, float, str, tuple Examples: list, dict, set
String to Integer
Integer to Float
Float to String and so on.
String to Integer
int() converts valid data of any type into integer
Capital Letters ( A – Z )
Small Letters ( a – z )
Digits ( 0 – 9 )
Special Characters (~ ! @ # $ % ^ . ?,)
Space
Some examples:
"Hello, World!"
"some@example.com"
"1234"
Slicing to End
If the end index is not specified, slicing stops at the end of the string.
message = "Hi Ravi"
part = message[3:]
print(part) //Ravi
Syntax:
round(number, digits(optional))
digits -> defines the number of decimal digits to be considered for rounding.
a = round(3.14159, 2)
print(a)
a = round(5.6777)
print(a)
3.14
6
//If Block
Inside I
If-Else statement
When If-Else conditional statement is used, the Else block of code executes if the condition is False.
a = int(input())
if a > 0:
print("Positive")
else:
print("Not Positive")
print("End")// input :- 2 //Positive
End
While Loop
For Loop
a = int(input())
counter = 0
while counter < 3:
a=a+1
print(a)
counter = counter + 1 //input :- 4 //output :- 5 6 7
In do-while loops, the code in the loop runs at least once before checking if the condition is True.
#include <stdio.h>
int main () {
int a = 1;
do {
printf("%d\n", a);
a = a + 1;
}while( a < 4 );
return 0;
}
xrange():
The xrange() function generates a sequence of integers starting from 0 similar to range() function.
Note: The xrange() function is deprecated from Python3. You cannot run xrange() function in our
code playground
a = 10
while a > 3:
a=a+1
print(a)
The Inner Loop will be executed one time for each iteration of the Outer Loop.
for i in range(2):
print("Outer: " + str(i))
for j in range(2):
print(" Inner: " + str(j))
// output
Outer: 0
Inner: 0
Inner: 1
Outer: 1
Inner: 0
Inner: 1
for i in range(5):
if i == 3:
break
print(i)
print("END")
//0
1
2
END
0
1
2
4
END
not:-The interviewer expects you to print the odd numbers on the first line and the even numbers
on the second line, for numbers ranging from 1 to N.
even_numbers_string = ""
print(odd_numbers_string)
print(even_numbers_string)
1
13
13
The interviewer expects you to print the odd numbers as a right angled triangle of N rows
rows = int(input())
odd_number = 1
for i in range(1, rows + 1):
odd_number = 1
output = ""
for j in range(1, i + 1):
output += str(odd_number) + " "
odd_number += 2
print(output)
he outer loop for i in range(1, rows + 1)represents the range of rows, the inner loop for j in range(1,
i + 1) iterates through the rows, prints the odd numbers, and then increments the odd number
odd_number += 2.
def print_triangle(n):
current_number = 1
for i in range(1, n + 1):
row_values = ""
for j in range(i):
row_values += str(current_number) + " "
current_number += 2
print(row_values)
rows = int(input())
print_triangle(rows)
List
Tuple
Set
Dictionary
. What is a list?
The List is the most versatile python data structure that holds an ordered sequence of items.
Creating a List
A List can be created by enclosing elements within [square] brackets where each item is separated
by a comma.
a=2
list_a = [5, "Six", a, 8.2]
print(type(list_a))
print(list_a)//<class 'list'>
[5, 'Six', 2, 8.2]
Syntax
list.append(value)
list_a = []
for x in range(1, 4):
list_a.append(x)
print(list_a)
[1, 2, 3]
Extend
Adds all the elements of a sequence to the end of the list.
Syntax
list_a.extend(list_b)
list_a = [1, 2, 3]
list_b = [4, 5, 6]
list_a.extend(list_b)
print(list_a)
[1, 2, 3, 4, 5]
Insert
Inserts the given element at the specified index.
Syntax
list.insert(index, value)
list_a = [1, 2, 3]
list_a.insert(1, 4)
print(list_a)
[1, 4, 2, 3]
Pop
Removes the last item from the list.
Syntax
list.pop()
list_a = [1, 2, 3]
list_a.pop()
print(list_a)
[1, 2]
Remove
Removes the first matching element from the list.
Syntax
list.remove(value)
list_a = [1, 3, 2, 3]
list_a.remove(3)
print(list_a)
[1, 2, 3]
Clear
Removes all the items from the list.
Syntax
list.clear()
list_a = [1, 2, 3]
list_a.clear()
print(list_a) /[]
Index
Returns the index of the first occurrence of the specified item in the list.
Syntax
list.index(item)
list_a = [1, 3, 2, 3]
index = list_a.index(3)
print(index)
Count
Returns the number of elements with the specified value.
Syntax
list.count(value)
list_a = [1, 2, 3]
count = list_a.count(2)
print(count)
Sort
Sorts the items of a list in ascending ascending order. The sort() method modifies the original list.
Syntax
list.sort()
list_a = [1, 3, 2]
list_a.sort()
print(list_a)//[1, 2, 3]
Sorted
Sorts the items of a list in ascending ascending order. The sorted() method returns the modified list.
Syntax
sorted()
Q. With which data types can we use the pop() method in Python?
The pop() method can be used with three different data types in Python: lists, dictionaries, and sets.
Lists: The pop() method removes the specified item from a list. If no index is specified, the last
element is removed.
Dictionaries: The pop() method removes the specified item from a dictionary. The method returns
the value of the removed item.
Sets: The pop() method removes a random item from a set. The method returns the removed item.
The append() method is used to add an entire list to another list. In this case, temp is appended to
lst, resulting in lst containing 1, 2, and the entire temp list.
The extend() method is used to add the elements of one list to the end of another list. In this case,
the elements of temp are added to the end of lst. Therefore, lst will contain the following elements: 1,
2, [3, 4], 3, 4.
Extending a list in Python means adding one or more elements to the end of the list. There are
several ways to extend a list in Python, including:
Using the extend() method: Adds all the elements of an iterable to the end of the list.
Using the + operator: Concatenate two lists together.
Using list slicing: Insert a new element or list of elements into the end of a list.
insert(): This method inserts the element at a specific position in the list.
extend(): Adds all the elements of an iterable to the end of the list.
Q. What is the difference between the append() and extend() methods for lists in Python?
When I use append(), it adds a single element to the end of the list. This element can be of any data
type, and it's added as a single item. So, if I append another list to an existing list, the entire list will be
added as a single element.
On the other hand, extend() is used to merge two lists or add multiple elements. When I use
extend() with a list as an argument, each element of the list argument is added individually to the end
of the original list.
Syntax:
str_var.split(separator)
Syntax:
Syntax:
variable[start_index:end_index:step]
sequence[start:stop:step]