Python Daily Questions (1-50)
Python Daily Questions (1-50)
Prepared By:
Mani Shankar
Python Developer
1)
num = 6
if num % 2 == 0:
print('Even')
else:
print('Odd')
Explanation:-
1. Variable Initialization:
num = 6
2. Conditional Statement:
The code checks if the remainder of num divided by 2 is equal to 0. This condition determines
whether num is even or odd.
If the condition is true (i.e., if num is divisible by 2 without a remainder), it prints Even. Otherwise,
it prints Odd.
if num % 2 == 0:
print('Even')
else:
print('Odd')
3. Output:
Since 6 is divisible by 2 without a remainder, the output of the code will be 'Even'.
__________________________________________________________________________________
2)
num1 = 5
num2 = 3
Explanation:-
1. Variable Initialization:
num1 = 5
num2 = 3
2. Sum Calculation:
The variables num1 and num2 are added together, and the result is stored in a variable named
sum.
3. Print Statement:
The code prints a message indicating the sum of the two numbers using the print() function.
4. Output:
__________________________________________________________________________________
3)
string = 'hello'
reversed_string = string[::-1]
print(reversed_string)
Explanation:-
1. String Initialization:
string = 'hello'
reversed_string = string[::-1]
3. Print Statement:
The code prints the reversed string using the print() function.
print(reversed_string)
4. Output:
The output of the code will be the reversed string of 'hello', which is 'olleh'.
__________________________________________________________________________________
4)
num1 = 10
num2 = 20
num3 = 15
max_num = max(num1,num2,num3)
Explanation:-
1. Variable Initialization:
num1 = 10
num2 = 20
num3 = 15
3. Print Statement:
The code prints a message indicating the maximum number found using the print() function.
4. Output:
The output of the code will be "The maximum number is: 20".
_________________________________________________________________________________
5)
base = 5
height= 8
print(area)
Explanation:-
1. Variable Initialization:
base = 5
height = 8
2. Area Calculation:
The code prints the calculated area using the print() function.
print(area)
4. Output:
The output of the code will be the calculated area of the triangle, which is 20.0.
__________________________________________________________________________________
6)
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
num = 5
print("factorial of",num,"is:",factorial(num))
Explanation:-
1. Function Definition:
The factorial function takes one parameter n, which represents the number whose factorial needs
to be computed.
def factorial(n):
2. Base Case:
If n is equal to 0, which is the base case for factorial calculations, the function returns 1.
if n == 0:
return 1
3. Recursive Case:
If n is not equal to 0, the function recursively calls itself with the argument n-1 and multiplies the
result by n.
else:
return n * factorial(n-1)
4. Function Call:
The variable num is assigned the value 5, and then the factorial function is called with num as the
argument.
num = 5
5. Output:
__________________________________________________________________________________
7)
num1 = "5"
num2 = "3"
print(sum)
Explanation:-
1. Variable Initialization:
num1 = "5"
num2 = "3"
2. String Concatenation:
The variables num1 and num2 are concatenated together using the + operator, resulting in the
string "53".
The code prints the concatenated string sum using the print() function.
print(sum)
4. Output:
__________________________________________________________________________________
8)
def count_occurrences(string,char):
count = 0
for c in string:
if c == char:
count +=1
return count
my_string = "hello"
char_to_count = "l"
print(count_occurrences(my_string,char_to_count))
Explanation:-
1. Function Definition:
The count_occurrences function takes two parameters: string (the input string) and char (the
character to count occurrences of).
2. Initialization:
The variable count is initialized to 0. This variable will keep track of the number of occurrences of
the specified character.
count = 0
The function iterates over each character c in the input string string using a for loop.
for c in string:
4. Count Occurrences:
Within the loop, if the current character c is equal to the specified character char, the count
variable is incremented by 1.
if c == char:
count += 1
5. Return Count:
After iterating through the entire string, the function returns the final count of occurrences.
return count
6. Function Call:
The variables my_string and char_to_count are assigned the values "hello" and "l" respectively.
my_string = "hello"
char_to_count = "l"
print(count_occurrences(my_string, char_to_count))
7. Output:
The output of the code will be the count of occurrences of the character "l" in the string "hello",
which is 2.
__________________________________________________________________________________
9)
def is_vowel(char):
vowels = "aeiou"
character = "A"
if is_vowel(character):
print(character,"is a vowel")
else:
Explanation:-
1. Function Definition:
The is_vowel function takes one parameter char, which represents the character to be checked.
def is_vowel(char):
2. Vowels Initialization:
The variable vowels is initialized with a string containing all lowercase vowels ("aeiou").
vowels = "aeiou"
The function checks if the lowercase version of the input character char is present in the vowels
string using the in operator.
The char.lower() method is used to convert the input character to lowercase before checking for its
presence in the vowels string. This ensures that both uppercase and lowercase versions of vowels are
correctly identified.
4. Character to Check:
The variable character is assigned the value "A", which is the character to be checked for being a
vowel.
character = "A"
5. Conditional Statement:
The code checks if the character is a vowel using the is_vowel function.
If the character is a vowel, it prints a message indicating that the character is a vowel. Otherwise, it
prints a message indicating that the character is not a vowel.
if is_vowel(character):
else:
print(character, "is not a vowel")
6. Output:
The output of the code will be based on whether the character "A" is a vowel or not.
__________________________________________________________________________________
10)
string = "Hello"
num = 123
print(result)
Explanation:-
1. Variable Initialization:
string = "Hello"
num = 123
The str() function is used to convert the number num to a string. This is necessary because you
cannot directly concatenate a string with a number.
str_num = str(num)
3. Concatenation:
The string string and the string representation of the number num are concatenated together using
the `+` operator and stored in the variable result.
4. Print Statement:
The code prints the concatenated result using the print() function.
print(result)
5. Output:
__________________________________________________________________________________
11)
def square(n):
return n * n
num = 4
print("Square of",num,"is",square(num))
Explanation:-
1. Function Definition:
The square function takes one parameter n, which represents the number to be squared.
def square(n):
2. Square Calculation:
The function returns the square of the input number n, which is computed by multiplying n by
itself.
return n * n
3. Variable Initialization:
The variable num is assigned the value 4, which is the number whose square is to be computed.
num = 4
4. Function Call:
The square function is called with the argument num, and the result is printed along with a
message.
print("Square of", num, "is", square(num))
5. Output:
The output of the code will be the square of 4, which is 16, along with the message "Square of 4 is
16".
__________________________________________________________________________________
12)
list1 = [1,2,3]
list2 = [4,5,6]
print(result)
Explanation:-
1. List Initialization:
list1 = [1, 2, 3]
list2 = [4, 5, 6]
2. List Concatenation:
The `+` operator is used to concatenate list1 and list2 together, resulting in a new list containing all
elements from list1 followed by all elements from list2.
3. Print Statement:
The code prints the concatenated list result using the print() function.
print(result)
4. Output:
The output of the code will be the concatenated list [1, 2, 3, 4, 5, 6].
__________________________________________________________________________________
13)
character = "*"
repetitions = 5
print(repeated_character)
Explanation:-
1. Variable Initialization:
- character is initialized with the string "*", which represents the character to be repeated.
- repetitions is initialized with the value 5, which represents the number of times the character will
be repeated.
character = "*"
repetitions = 5
2. String Repetition:
- The variable `repeated_character` is assigned the result of repeating the character character for
repetitions times using the * operator.
3. Print Statement:
print(repeated_character)
4. Output:
- The output of the code will be the string "*****", consisting of the character "*" repeated 5
times.
number = 42
print(result)
Explanation:-
1. Variable Initialization:
- string is initialized with the string "The Number is:", which represents the prefix of the final result.
- number is initialized with the integer 42, which represents the number to be concatenated with
the string.
number = 42
2. String Concatenation:
- The variable result is assigned the result of concatenating the string with the string representation
of the number number.
3. Print Statement:
print(result)
4. Output:
- The output of the code will be the string "The Number is:42", where the number 42 is
concatenated after the string prefix.
b=1
c=a+b
print(c)
Explanation:-
1. Variable Initialization:
a=6
b=1
2. Addition:
- The values of a and b are added together, and the result is stored in the variable c.
c=a+b
3. Print Statement:
print(c)
4. Output:
__________________________________________________________________________________
16)
original_list = [1,2,3]
print(repeated_list)
Explanation:-
1. List Comprehension:
- The code uses a list comprehension to iterate over each element i in the original_list.
- For each element i, it multiplies it by 3 and adds the result to the new list repeated_list.
2. Print Statement:
print(repeated_list)
3. Output:
- The output of the code will be the new list repeated_list, where each element is three times the
corresponding element from the original_list.
This is because each element from the original list [1, 2, 3] has been multiplied by 3, resulting in [3, 6,
9].
__________________________________________________________________________________
17)
my_dict = {"name":"Mani", "age":24,"city":"Hyderabad"}
print(my_dict.keys())
Explanation:-
1. Dictionary Initialization:
2. keys() Method:
- The keys() method is used to retrieve a view object that displays a list of all the keys in the
dictionary.
print(my_dict.keys())
3. Print Statement:
- The code prints the view object returned by the keys() method.
print(my_dict.keys())
4. Output:
- The output of the code will be a view object containing the keys of the dictionary my_dict.
__________________________________________________________________________________
18)
my_dict = {"name":"Mani", "age":24,"city":"Hyderabad"}
print(my_dict.values())
Explanation:-
1. Dictionary Initialization:
2. values() Method:
- The values() method is used to retrieve a view object that displays a list of all the values in the
dictionary.
print(my_dict.values())
3. Print Statement:
- The code prints the view object returned by the values() method.
print(my_dict.values())
4. Output:
- The output of the code will be a view object containing the values of the dictionary my_dict.
print(a)
Explanation:-
1. String Indexing:
- "mani" is a string.
- Indexing starts from 0, so "mani"[1] refers to the character at index 1 in the string, which is 'a'.
2. Mathematical Operation:
- 1 + 1 is evaluated to 2.
3. Indexing:
- "mani"[2] is performed, which retrieves the character at index 2 in the string "mani", which is 'n'.
4. Variable Assignment:
a = "n"
5. Print Statement:
print(a)
6. Output:
__________________________________________________________________________________
20)
a=2
b = 20
b *=a * b + 10
print(b)
Explanation:-
1. Variable Initialization:
a=2
b = 20
2. Expression Evaluation:
3. Compound Assignment:
- The compound assignment operator *= is used to multiply b by the result of the expression
evaluated in the previous step (50).
b *= a * b + 10
4. Print Statement:
print(b)
5. Output:
__________________________________________________________________________________
21)
a = 01#2#3
print(a)
Explanation:-
The statement a = 01#2#3 would indeed result in a syntax error in Python. The reason is that in
Python 3, leading zeros are not allowed for integer literals. So, when the Python interpreter
encounters `01`, it interprets it as an octal (base-8) number. However, octal literals must consist only
of digits from 0 to 7. Since 01 includes the digit 1, which is not valid in octal representation, it raises a
syntax error.
__________________________________________________________________________________
22)
a,b = "06"
b,c = "26"
print(a + b + c)
Explanation:-
1. Variable Assignment:
- b,c = "26" reassigns "2" to b (overwriting the previous value) and "6" to c.
2. Print Statement:
__________________________________________________________________________________
23)
a = [1,2,3,4,5,6],
print(a[7:])
Explanation:
When you execute print(a[7:]), Python returns an empty tuple () instead of an empty list. This is
because slicing an empty list in Python returns an empty tuple, not an empty list.
a[7:] tries to access elements from index 7 onwards, but since there are no elements beyond index 5
in list `a`, an empty tuple () is returned.
Then, print(a[7:] ) prints an empty tuple. Therefore, the output of print(a[7:]) would indeed be ().
24)
a = [1,2,[3,4],["mani"]]
print(a[3][0])
Explanation:-
-1
-2
To access the elements within sublists, you use indexing twice: first to access the sublist and then to
access the element within that sublist.
a[3][0] accesses the first element of the sublist ["mani"], which is "mani".
__________________________________________________________________________________
25)
_26 = {2,6}
print(_26 * 26)
Explanation:-
However, in Python, multiplication between a set and an integer is not defined, and it will raise a
TypeError.
__________________________________________________________________________________
26)
a = 3 ** 2 ** 3
print(a)
Explanation:-
In Python, the exponentiation operator (**) has right-to-left associativity. This means that in an
expression like a ** b ** c, the operation with the highest precedence (the one closer to the right) is
performed first.
1. 2 ** 3: This evaluates to 8.
__________________________________________________________________________________
27)
num = 0
num = num + 3
print (num)
Explanation:-
The while loop iterates as long as the num is less than 26. In each iteration, num is incremented by 3.
After the 9th iteration, the condition num < 26 fails, so the loop exits.
Output:
print(num) # Output: 27
__________________________________________________________________________________
28)
x=5
y=2
c = x//y
print(c)
Explanation:
c = x//y: Attempts to assign the result of integer division of x by y to the variable c. However, there is
an indentation error here, as theres an extra space before the variable c.
Due to the indentation error in line 3, Python will raise an "IndentationError". This error occurs
because the indentation level for line 3 is unexpected. Python expects the same level of indentation
for consecutive lines within a block of code.
a = [1,2,3,4]
a.append([4,5])
print(len(a))
Explanation:
when the append method is used, it becomes [1, 2, 3, 4, [4, 5]], which contains five elements.
__________________________________________________________________________________
30)
a = [6,1,2000,26,4]
b = [60,62,10,40]
print(a>b)
Explanation:-
In Python, when you compare two lists using the greater than (>) operator, it compares the lists
element-wise based on their lexicographical order. The comparison stops at the first pair of elements
that are not equal.
1. Comparison:
- The first elements of both lists are compared: 6 and 60. Since 6 is less than 60, the comparison
stops here, and a is considered less than b.
2. Output:
__________________________________________________________________________________
31)
my_list = [1,2]
my_list.extend([4,5])
print(my_list)
Explanation:-
The extend() method in Python is used to append elements from an iterable (like a list, tuple, or set)
to the end of the list.
- The extend() method appends the elements [4, 5] to the end of my_list.
__________________________________________________________________________________
32)
a = {4,4,4,4,2,2,6,6,6,6}
b = list(a)
print(b[2]) → 6
Explanation:
Step1: Initialization of Set a:
In a set, each element is unique, so even though there are repeated elements in the initialization,
they are considered only once due to the sets property of uniqueness.
When you convert a set to a list, the order of elements may change, but duplicates are removed, so
the resulting list b will contain unique elements from set a in an arbitrary order.
In Python, indexing starts from 0, so b[2] refers to the third element of the list b.
33)
a = "26_26"
b = "mani"
print(float(a))
print(int(b))
print(str(b))
Explanation:
print(float(a)): This line tries to convert the string "26_26" to a floatingpoint number and then
prints it.
• float(a): Attempts to convert the string "26_26" to a float. Python ignores the
underscore character when converting to a float, so "26_26" is effectively 2626.
Therefore, float(a) successfully converts "26_26" to 2626.0.
print(int(b)): This line tries to convert the string "mani" to an integer and then prints it.
• int(b): Tries to convert the string "mani" to an integer. Since "mani" is not a valid
integer, Python raises a ValueError because it cant convert it directly to an integer.
__________________________________________________________________________________
34)
a,b = "16"
b,c = "26"
print(a+b+c) → 126
Explanation:
a, b = "16": This line assigns the string "16" to variables a and b. Since "16" is a string with two
characters, Python unpacks it and assigns the first character "1" to a and the second character "6" to
b.
b, c = "26": This line assigns the string "26" to variables b and c. Similarly, Python unpacks it and
assigns the first character "2" to b and the second character "6" to c.
print(a + b + c): This line concatenates the values of a, b, and c and then prints the result.
• a + b + c concatenates the values of a, b, and c, which are strings "1", "2", and "6"
respectively.
b = 26
print(a is b) → True
x = 16
y = 16
print(x is y) → False
Explanation :
1. Variables Assignment:
When you use the is operator, Python checks if two variables refer to the same object in memory.
print(a is b) will output `True` because a and b refer to the same object, which is 26.
4. Variables Assignment:
Again, the is operator is used to check if x and y refer to the same object in memory.
However, in this case, x is a string object ("16") and y is an integer object (16).
6. Printing the Result:
print(x is y) will output False because although x and y have the same value ("16" and 16), they are
different types and thus stored as different objects in memory.
__________________________________________________________________________________
36)
a=6
def add():
a = 26
a=a+4
print(a)
a = 36
print(a)
Explanation:
2. Function Definition:
Inside the add() function, a new local variable a is created with the value 26.
This local a is distinct from the global a defined outside the function.
Outside the function, the global variable a is reassigned to the value 36.
print(a) outside the function will output 36, which is the final value of the global variable a.
__________________________________________________________________________________
37)
a = [1,2,3,4,5]
b=a
b[2] = 26
print(b)
Explanation:
1. List Initialization:
a = [1, 2, 3, 4, 5]
2. Aliasing:
b is assigned to a. This means b is now referring to the same list object that a is referring to, rather
than creating a new copy of the list. Both a and b now point to the same list in memory.
b=a
3. Modification:
The third element of list b is modified to 26. Since b is referring to the same list object as a, this
change affects the original list a as well.
b[2] = 26
4. Printing:
__________________________________________________________________________________
38)
a = "one"
for a in range(2):
print(a * 2)
Explanation:
1. Variable Initialization:
a = "one"
2. Loop:
The for loop iterates over the range from 0 to 1, inclusive. During each iteration, the variable a is
reassigned to the loop variable which takes on values from the range.
for a in range(2):
3. Print Statement:
Inside the loop, a now holds the integer values 0 and 1, not the string "one". Multiplying an integer
by 2 concatenates the string representation of the integer twice.
print(a * 2)
4. Output:
2
39)
def sum(num):
total = 0
for i in num:
total +=i
return float(total)
print(sum((6,26,4,1)))
Explanation:-
1. Function Definition:
def sum(num):
2. Initializing Total:
Inside the function, a variable total is initialized to 0. This variable will store the running total of the
sum of the numbers in the iterable.
total = 0
for i in num:
4. Adding to Total:
total += i
After the loop finishes, the function returns the total as a float.
return float(total)
6. Function Call:
The sum function is called with a tuple (6, 26, 4, 1) as the argument, and the result is printed.
This code will output the sum of the numbers (6, 26, 4, 1), which is 37.0 as a float.
__________________________________________________________________________________
40)
a = 26
for i in range(1,26):
pass
print(a)
Explanation:-
1. Variable Initialization:
a = 26
2. Loop:
- The `for` loop iterates over the range from 1 to 25 (inclusive), but since theres only a `pass`
statement in the loop body, it doesnt actually do anything during each iteration.
pass
3. Print Statement:
print(a)
4. Output:
The value of a, which is 26, will be printed.
__________________________________________________________________________________
41)
a = 26
print(True)
else:
print(False)
Explanation:-
and: Both conditions must be True for the whole expression to be True.
or: At least one condition must be True for the whole expression to be True.
Since none of the conditions evaluate to True, the if block would not be executed, and the else block
will be executed, printing False.
__________________________________________________________________________________
42)
a = True
b = False
print(a + b)
Explanation:-
In Python, True and False are treated as 1 and 0 respectively when used in numerical operations. So,
when you add True (1) and False (0), you get 1 + 0 = 1. Therefore, the output of print(a + b) would be:
1
__________________________________________________________________________________
43)
a = {1:6,2:26}
print(i,j,sep = ":")
Explanation:-
In this code, a is a dictionary with keys 1 and 2, each mapped to values 6 and 26 respectively.
The for loop iterates over the items of the dictionary using a.items(). In each iteration, i represents
the key and j represents the value.
The print() function prints each key-value pair separated by a colon (:) using the sep parameter.
1:6
2:26
__________________________________________________________________________________
44)
a = [1,2,3,4,5]
for i in a:
a.remove(i)
print(a)
Explanation:-
1. List Initialization:
a = [1, 2, 3, 4, 5]
2. Loop:
Inside the loop, you're attempting to remove each element i from the list a using a.remove(i).
for i in a:
a.remove(i)
3. Result:
print(a)
4. Output:
During the first iteration of the loop, i is 1, and 1 is removed from a, resulting in [2, 3, 4, 5].
During the second iteration, i is now 3 (as 2 was shifted due to removal of 1), and 3 is removed from
a, resulting in [2, 4, 5].
__________________________________________________________________________________
45)
_1 = [2,4,6,8]
_2 = _1
_3 = [2,4,6,8]
print(_1 is _3)
Explanation:-
1. Variable Initialization:
_1 = [2, 4, 6, 8]
_2 = _1
_3 = [2, 4, 6, 8]
2. Identity Check:
print(_1 is _3)
3. Output:
Explanation:
_1 and _3 are initialized as two separate list objects containing the same elements [2, 4, 6, 8].
_2 is assigned to the same object as _1, so _1 and _2 refer to the same list object in memory.
However, _1 and _3 are two different list objects, even though they have the same contents.
Therefore, _1 is _3 evaluates to False because they are not the same object in memory.
__________________________________________________________________________________
46)
a = (6 + 26) * 2/ 4 - 8 +26
print(a)
Explanation:-
(6 + 26) = 32.
32 * 2 = 64.
64 / 4 = 16.
16 - 8 = 8.
8 + 26 = 34.
__________________________________________________________________________________
47)
a = "i"
b = "Love you"
c = "Mani"
if a or b and c:
print(True)
else:
print(False)
Explanation:-
1. Variable Initialization:
a = "i"
b = "Love you"
c = "Mani"
2. Conditional Check:
The if statement checks if a evaluates to True, or if b evaluates to True and c evaluates to True.
If any of these conditions are true, it prints True, otherwise, it prints False.
if a or b and c:
print(True)
else:
print(False)
3. Evaluation:
b and c are also non-empty strings, but the and operation between them doesn't affect the result
because b and c are both non-empty strings, so the result is True regardless of the and condition.
4. Output:
__________________________________________________________________________________
49)
def string(a,b,separator):
return a+separator + b
x = string('Hii','Mani', ',')
print(x)
explanation:-
1. Function Definition:
2. String Concatenation:
Inside the function, strings a and b are concatenated together, with separator added between
them.
return a + separator + b
3. Function Call:
The function is called with arguments 'Hii', 'Mani', and ',', and the result is stored in variable x.
4. Print Statement:
print(x)
5. Output:
The function concatenates the strings `'hii'` and `'Mani'` with a comma ',' between them, resulting in
the string Hii,Mani', which is then printed.
__________________________________________________________________________________
50)
followers = 0
followers += 200
break
Explanation:-
followers = 0
Day 1:
Day 2:
Day 3:
After the loop finishes (whether it reaches 25 days or breaks early), print the total number of
followers after 25 days.
Output:
The output indicates that after 25 days, the total number of followers is 5000.