0% found this document useful (0 votes)
5 views

Python Daily Questions (1-50)

Python

Uploaded by

doddi.ajith2003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Python Daily Questions (1-50)

Python

Uploaded by

doddi.ajith2003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

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 is initialized with a value of 6.

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'.

Therefore, The Final Answer is → Even

__________________________________________________________________________________

2)
num1 = 5

num2 = 3

sum = num1 + num2

print("The Sum is:",sum)

Explanation:-
1. Variable Initialization:

num1 is initialized with a value of 5.

num2 is initialized with a value of 3.

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.

sum = num1 + num2

3. Print Statement:

The code prints a message indicating the sum of the two numbers using the print() function.

print("The Sum is:", sum)

4. Output:

The output of the code will be "The Sum is: 8".

So, The Final Answer is → The Sum is: 8

__________________________________________________________________________________

3)

string = 'hello'

reversed_string = string[::-1]

print(reversed_string)

Explanation:-

1. String Initialization:

A string variable named string is initialized with the value 'hello'.

string = 'hello'

2. Reversing the String:


The [::-1] slicing syntax is used to reverse the string. This slicing technique creates a new string that
starts from the last character of the original string (-1) and ends at the first character of the original
string (0).

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'.

So,The Final Answer is → olleh

__________________________________________________________________________________

4)
num1 = 10

num2 = 20

num3 = 15

max_num = max(num1,num2,num3)

print("The maximum number is:",max_num)

Explanation:-

1. Variable Initialization:

num1 is initialized with a value of 10.

num2 is initialized with a value of 20.

num3 is initialized with a value of 15.

num1 = 10

num2 = 20

num3 = 15

2. Finding the Maximum:


The max() function is used to find the maximum value among num1, num2, and num3.

max_num = max(num1, num2, num3)

3. Print Statement:

The code prints a message indicating the maximum number found using the print() function.

print("The maximum number is:", max_num)

4. Output:

The output of the code will be "The maximum number is: 20".

So, The Final Answer is → The maximum number is: 20

_________________________________________________________________________________

5)

base = 5

height= 8

area = 0.5 * base * height

print(area)

Explanation:-

1. Variable Initialization:

base is initialized with a value of 5.

height is initialized with a value of 8.

base = 5

height = 8

2. Area Calculation:

area = 0.5 * base * height

area = 0.5 * 5 * 8 → 20.0


3. Print Statement:

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.

So, The Final Answer 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

print("factorial of", num, "is:", factorial(num))

5. Output:

The output of the code will be the factorial of 5, which is 120.

So, The Final Answer is → factorial of 5 is → 120

__________________________________________________________________________________

7)
num1 = "5"

num2 = "3"

sum = num1 + num2

print(sum)

Explanation:-

1. Variable Initialization:

num1 is initialized with the string "5".

num2 is initialized with the string "3".

num1 = "5"

num2 = "3"

2. String Concatenation:

The variables num1 and num2 are concatenated together using the + operator, resulting in the
string "53".

sum = num1 + num2


3. Print Statement:

The code prints the concatenated string sum using the print() function.

print(sum)

4. Output:

The output of the code will be "53".

So, The Final Answer is → 53

__________________________________________________________________________________

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).

def count_occurrences(string, char):

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

3. Loop through the String:

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.

The count_occurrences function is called with my_string and char_to_count as arguments.

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.

So, The Final Answer is → 2

__________________________________________________________________________________

9)
def is_vowel(char):

vowels = "aeiou"

return char.lower() in vowels

character = "A"

if is_vowel(character):

print(character,"is a vowel")
else:

print(character,"is not a vowel")

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"

3. Check if Character is a Vowel:

The function checks if the lowercase version of the input character char is present in the vowels
string using the in operator.

return char.lower() in vowels

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):

print(character, "is a vowel")

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.

So, The Final Answer is → A is a vowel

__________________________________________________________________________________

10)
string = "Hello"

num = 123

result = string + str(num)

print(result)

Explanation:-

1. Variable Initialization:

String is initialized with the value "Hello".

num is initialized with the value 123.

string = "Hello"

num = 123

2. Converting Number to String:

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.

result = string + str_num

4. Print Statement:
The code prints the concatenated result using the print() function.

print(result)

5. Output:

The output of the code will be the concatenated string "Hello123".

So , The Final Answer is → Hello123

__________________________________________________________________________________

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".

So, The Final Answer is → Square of 4 is 16

__________________________________________________________________________________

12)

list1 = [1,2,3]

list2 = [4,5,6]

result = list1 + list2

print(result)

Explanation:-

1. List Initialization:

list1 is initialized with the list [1, 2, 3].

-list2 is initialized with the list [4, 5, 6].

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.

result = list1 + 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].

So, The Final Answer is → [1, 2, 3, 4, 5, 6]

__________________________________________________________________________________

13)

character = "*"

repetitions = 5

repeated_character = character * repetitions

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.

repeated_character = character * repetions

3. Print Statement:

- The code prints the resulting string repeated_character.

print(repeated_character)

4. Output:

- The output of the code will be the string "*****", consisting of the character "*" repeated 5
times.

So, The Final Answer is → *****


14)
string = "The Number is:"

number = 42

result = string + str(number)

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.

string = "The Number is:"

number = 42

2. String Concatenation:

- The variable result is assigned the result of concatenating the string with the string representation
of the number number.

result = string + str(number)

3. Print Statement:

- The code prints the resulting string result.

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.

So, The Final Answer is → The Number is:42


15)
a=6

b=1

c=a+b

print(c)

Explanation:-

1. Variable Initialization:

- a is initialized with the value 6.

- b is initialized with the value 1.

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:

- The code prints the value of c.

print(c)

4. Output:

- The output of the code will be the sum of a and b, which is 7.

So, The Final Answer is → 7

__________________________________________________________________________________

16)
original_list = [1,2,3]

repeated_list = [i * 3 for i in original_list]

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.

repeated_list = [i * 3 for i in original_list]

2. Print Statement:

- The code prints the resulting list repeated_list.

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.

So, The Final Answer is → [3, 6, 9]

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:

- my_dict is initialized as a dictionary containing key-value pairs representing information about a


person (name, age, and city).

my_dict = {"name": "Mani", "age": 24, "city": "Hyderabad"}

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.

So, The Final Answer is → dict_keys(['name', 'age', 'city'])

__________________________________________________________________________________

18)
my_dict = {"name":"Mani", "age":24,"city":"Hyderabad"}

print(my_dict.values())

Explanation:-

1. Dictionary Initialization:

- my_dict is initialized as a dictionary containing key-value pairs representing information about a


person (name, age, and city).

my_dict = {"name": "Mani", "age": 24, "city": "Hyderabad"}

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.

So, The Final Answer is → dict_values(['Mani', 24, 'Hyderabad'])


19)
a = ("mani"[1+1])

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:

- The character 'n' is assigned to the variable a.

a = "n"

5. Print Statement:

- The code prints the value of a.

print(a)

6. Output:

- The output of the code will be the character 'n'.

So, The Final Answer is → n

__________________________________________________________________________________

20)
a=2

b = 20

b *=a * b + 10

print(b)
Explanation:-

1. Variable Initialization:

- a is initialized with the value 2.

- b is initialized with the value 20.

a=2

b = 20

2. Expression Evaluation:

- The expression a * b + 10 is evaluated first.

- This evaluates to 2 * 20 + 10 = 40 + 10 = 50.

3. Compound Assignment:

- The compound assignment operator *= is used to multiply b by the result of the expression
evaluated in the previous step (50).

- So, b is updated to b * (a * b + 10) = 20 * 50 = 1000.

b *= a * b + 10

4. Print Statement:

- The code prints the value of b.

print(b)

5. Output:

- The output of the code will be 1000.

So, The Final Answer → 1000

__________________________________________________________________________________

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.

Therefore, executing a = 01#2#3 would indeed lead to a syntax error in Python.

So, The Final Answer is → Syntax Error

__________________________________________________________________________________

22)
a,b = "06"

b,c = "26"

print(a + b + c)

Explanation:-

1. Variable Assignment:

- a,b = "06" assigns "0" to a and "6" to b.

- b,c = "26" reassigns "2" to b (overwriting the previous value) and "6" to c.

2. Print Statement:

- print(a + b + c) concatenates the strings stored in a, b, and c, resulting in "0626".

So, The Final Answer is → 0626

__________________________________________________________________________________

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:-

a = [1, 2, [3, 4], ["mani"]]

- a is a list with four elements:

-1

-2

- [3, 4] (a nested list)

- ["mani"] (a nested list)

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] accesses the fourth element of the list a, which is ["mani"].

a[3][0] accesses the first element of the sublist ["mani"], which is "mani".

Therefore, the expression a[3][0] will evaluate as "mani".

So, The Final Answer is → mani

__________________________________________________________________________________

25)

_26 = {2,6}

print(_26 * 26)

Explanation:-

-> Set Definition: _26 = {2, 6}

This line defines a set _26 containing the elements 2 and 6.

->Multiplication Operation: _26 * 26


The attempt here seems to be to multiply the set _26 by the integer 26.

However, in Python, multiplication between a set and an integer is not defined, and it will raise a
TypeError.

So, The Final Answer is → 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.

Let's break down the expression step by step:

1. 2 ** 3: This evaluates to 8.

2. 3 ** 8: This evaluates to 6561.

So, The Final Answer is → 6561.

__________________________________________________________________________________

27)

num = 0

while num < 26 :

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.

Let's go through the iterations:

1. Iteration 1: num is 0, after adding 3, num becomes 3.


2. Iteration 2: num is 3, after adding 3, num becomes 6.

3. Iteration 3: num is 6, after adding 3, num becomes 9.

4. Iteration 4: num is 9, after adding 3, num becomes 12.

5. Iteration 5: num is 12, after adding 3, num becomes 15.

6. Iteration 6: num is 15, after adding 3, num becomes 18.

7. Iteration 7: num is 18, after adding 3, num becomes 21.

8. Iteration 8: num is 21, after adding 3, num becomes 24.

9. Iteration 9: num is 24, after adding 3, num becomes 27.

After the 9th iteration, the condition num < 26 fails, so the loop exits.

Output:

The final value of num, which is 27, is printed.

print(num) # Output: 27

So, The Final Answer is → 27

__________________________________________________________________________________

28)

x=5

y=2

c = x//y

print(c)

Explanation:

x = 5: Assigns the value 5 to the variable x.

y = 2: Assigns the value 2 to the variable y.

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.

print(c): Prints the value stored in 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.

The Final Answer is “error”


29)

a = [1,2,3,4]

a.append([4,5])

print(len(a))

Explanation:

The a is initially defined as [1, 2, 3, 4].

when the append method is used, it becomes [1, 2, 3, 4, [4, 5]], which contains five elements.

When we print(len(a)) is executed, it returns the length of the a, which is 5.

The Final output is 5

__________________________________________________________________________________

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.

Let's break down the comparison between lists a and b:

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:

- The result of the comparison a > b is False.


Output:

print(a > b) # Output: False

So, The Final Answer is →False.

__________________________________________________________________________________

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.

1. List Initialization: my_list = [1, 2]

- my_list is initialized as a list containing [1, 2].

2. Extend Operation: my_list.extend([4, 5])

- The extend() method appends the elements [4, 5] to the end of my_list.

- After this operation, my_list becomes [1, 2, 4, 5].

3. Print Statement: print(my_list)

- This line prints the modified list my_list.

So, The Final Answer is → [1, 2, 4, 5].

__________________________________________________________________________________

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:

We initialize a set called a with the following elements: {4, 4, 4, 4, 2, 2, 6, 6, 6, 6}.

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.

Step2: Converting Set a to List b:

We convert the set a to a list b using the list() function.

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.

Step3: Printing the Third Element of List b:

We print the third element of list b using print(b[2]).

In Python, indexing starts from 0, so b[2] refers to the third element of the list b.

The output will be the value at the index 2 in list b.

So the final output is 6


__________________________________________________________________________________

33)

a = "26_26"

b = "mani"

print(float(a))

print(int(b))

print(str(b))

Explanation:

a = "26_26": This line assigns the string "26_26" to the variable a.

b = "mani": This line assigns the string "mani" to the variable b.

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.

• The print() function then prints 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.

• So, this line will result in an error.

• So the Final output is


2626.0
error

__________________________________________________________________________________

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.

• So, "1" + "2" + "6" results in the string "126".

• Finally, print("126") prints "126".

So, the Final Output is 126


35)
a = 26

b = 26

print(a is b) → True

x = 16

y = 16

print(x is y) → False

Explanation :

1. Variables Assignment:

a is assigned the integer value 26.

b is also assigned the integer value 26.

2. Identity Comparison (is):

When you use the is operator, Python checks if two variables refer to the same object in memory.

In this case, a and b both refer to the integer object 26.

3. Printing the Result:

print(a is b) will output `True` because a and b refer to the same object, which is 26.

Now, lets move on to the second part:

4. Variables Assignment:

x is assigned the string value "16".

y is assigned the integer value 16.

5. Identity Comparison (is):

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:

1. Variable Assignment Outside the Function:

a is initially assigned the value 6.

2. Function Definition:

We define a function called add().

3. Variable Assignment Inside the Function:

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.

4. Updating the Local Variable Inside the Function:

The local variable a is incremented by 4, so it becomes 26 + 4 = 30.

5. Printing the Local Variable Inside the Function:

print(a) inside the function will output 30.


6. Variable Reassignment Outside the Function:

Outside the function, the global variable a is reassigned to the value 36.

7. Printing the Global Variable Outside the Function:

print(a) outside the function will output 36, which is the final value of the global variable a.

So The Final Answer is 36

__________________________________________________________________________________

37)

a = [1,2,3,4,5]

b=a

b[2] = 26

print(b)

Explanation:

1. List Initialization:

a is initialized as a list containing integers [1, 2, 3, 4, 5].

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:

b is printed, which now reflects the modification made earlier.


print(b) # Output: [1, 2, 26, 4, 5]

So the Final output is [1, 2, 26, 4, 5]

__________________________________________________________________________________

38)

a = "one"

for a in range(2):

print(a * 2)

Explanation:

1. Variable Initialization:

a is initially assigned the string value "one".

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:

The loop prints 0 * 2 = 0 and 1 * 2 = 2.

So the Final Output is

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:

The sum function is defined with a single parameter num.

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

3. Iterating Over the Iterable:

A for loop iterates over each element i in the iterable num.

for i in num:

4. Adding to Total:

- Inside the loop, each element i is added to the total variable.

total += i

5. Returning the Result:

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.

print(sum((6, 26, 4, 1)))

This code will output the sum of the numbers (6, 26, 4, 1), which is 37.0 as a float.

So the Final output is 37.0

__________________________________________________________________________________

40)

a = 26
for i in range(1,26):

pass

print(a)

Explanation:-

1. Variable Initialization:

a is initialized with the value 26.

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.

for i in range(1, 26):

pass

3. Print Statement:

After the loop, the value of a is printed.

print(a)

4. Output:
The value of a, which is 26, will be printed.

So, the final output is → 26

__________________________________________________________________________________

41)
a = 26

if a >36 and a <26 or a!=26:

print(True)

else:

print(False)

Explanation:-

1. a > 36 is False because a is 26.

2. a < 26 is also False because a is not less than 26.

3. a != 26 is False because a is indeed equal to 26.

Now, considering the logical operators:

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.

So, the final output IS → 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

So the Final Output is → 1

__________________________________________________________________________________

43)

a = {1:6,2:26}

for i,j in a.items():

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 is initialized as a list containing integers [1, 2, 3, 4, 5].

a = [1, 2, 3, 4, 5]
2. Loop:

The for loop iterates over each element i in the list a.

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:

After the loop finishes, you print the resulting list a.

print(a)

4. Output:

- The output will be [2, 4].

Let's See why the output is [2, 4]:

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].

The loop ends because there are no more elements in a.

Therefore, the Final Answer of a is [2, 4].

__________________________________________________________________________________

45)

_1 = [2,4,6,8]

_2 = _1

_3 = [2,4,6,8]

print(_1 is _3)
Explanation:-

1. Variable Initialization:

_1 and _3 are initialized as lists [2, 4, 6, 8].

_2 is assigned to the same object as _1.

_1 = [2, 4, 6, 8]

_2 = _1

_3 = [2, 4, 6, 8]

2. Identity Check:

print(_1 is _3) checks if _1 and _3 refer to the same object in memory.

print(_1 is _3)

3. Output:

The output of the code will be False.

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.

Therefore, the value of the expression (6+26)*2/4-8+26 is 34.

So, when you print a, you will get 34.0

So The Final Answer is 34.0

__________________________________________________________________________________

47)

a = "i"

b = "Love you"

c = "Mani"

if a or b and c:

print(True)

else:

print(False)

Explanation:-

1. Variable Initialization:

a, b, and c are initialized as strings.

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:

Since a is a non-empty string, it evaluates to True.

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:

The output of the code will be True.

So The Final Output is True

__________________________________________________________________________________

49)

def string(a,b,separator):

return a+separator + b

x = string('Hii','Mani', ',')

print(x)

explanation:-

1. Function Definition:

The string function takes three parameters: a, b, and separator.

def string(a, b, separator):

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.

x = string('Hii', 'Mani', ',')

4. Print Statement:

The result stored in x is printed.

print(x)

5. Output:

The output of the code will be Hii,Mani.

The function concatenates the strings `'hii'` and `'Mani'` with a comma ',' between them, resulting in
the string Hii,Mani', which is then printed.

So, The Final output is Hii,Mani

__________________________________________________________________________________

50)
followers = 0

for day in range(1, 26):

followers += 200

if followers >= 5500:

print(f'Congratulations! We reached 5500 followers on day {day}.')

break

print(f'Total Followers after 25 days: {followers}')

Explanation:-

Step 1: Variable Initialization

followers = 0

Initialize the variable followers to 0.


Step 2: Loop Iteration

for day in range(1, 26):

Start a loop that iterates over each day from 1 to 25.

Step 3: Increment Followers and Check Condition

Day 1:

followers += 200: Increment followers by 200 (followers = 0 + 200 = 200).

Check if followers (200) is greater than or equal to 5500 (False).

Day 2:

followers += 200: Increment followers by 200 (followers = 200 + 200 = 400).

Check if followers (400) is greater than or equal to 5500 (False).

Day 3:

followers += 200: Increment followers by 200 (followers = 400 + 200 = 600).

Check if followers (600) is greater than or equal to 5500 (False).

- ... (continue this process for all 25 days)

Step 4: Print Total Followers

print(f'Total Followers after 25 days: {followers}')

After the loop finishes (whether it reaches 25 days or breaks early), print the total number of
followers after 25 days.

Output:

Total Followers after 25 days: 5000

The output indicates that after 25 days, the total number of followers is 5000.

So, The Final output is Total Followers after 25 days: 5000

eND (TO Be CONTiNueD…)

You might also like