Mastering Python programming requires more than just understanding syntax — it’s about building logic and predicting output accurately. In this article, we present a curated set of short, practical, and output-based Python questions with answers.
What will be the output of the following code?
x = 10 y = 5 print(x & y) print(x | y) print(x ^ y)
Answer
NIST Cybersecurity Framework
Binary Representation:
x = 10 → 1010 (binary)
y = 5 → 0101 (binary)
0
15
15
Python was developed by __________ in the year __________.
Answer
Python was developed by Guido van Rossum in the year 1991.
Which of the following is a valid variable name in Python?
a) 2value b) value_2 c) value-2 d) @valueAnswer
b) value_2
Explanation:
Valid Python variable names must:
Start with a letter (A–Z or a–z) or an underscore (_)
Can contain letters, digits (0–9), and underscores (_)
Cannot start with a digit or use special characters like - or @
Options Breakdown:
a) 2value → ❌ Invalid (starts with a digit)
b) value_2 → ✅ Valid
c) value-2 → ❌ Invalid (- is not allowed in variable names)
d) @value → ❌ Invalid (@ is not allowed in variable names)
Short code-based
x = 5 y = "5" print(x == y) print(x is y)
What will be the output of the above code?
Answer
Explanation: x == y → Compares values 5 (int) ≠ "5" (str) → So the result is False x is y → Compares identity (whether both refer to the same object in memory) Different types, stored in different memory locations → Also False
Output:
false
false
Define type casting with an example.
Answer
Type casting is the process of converting one data type into another. It is used to ensure that variables are in the correct type for operations.
There are two types:
Implicit Type Casting – Python automatically converts the data type.
Explicit Type Casting – You manually convert the data type using functions like int(), float(), str(), etc.
x = "10" y = 5 # Convert x to integer before adding z = int(x) + y print(z) Output: 15
Write output
a = 10 b = 3 print(a // b) print(a % b)
Answer
a // b → 10 // 3 = 3 (quotient without the decimal part) a % b → 10 % 3 = 1 (remainder of the division)
Define indentation in Python. Why is it important?
Answer
✅ Indentation in Python Indentation refers to the spaces or tabs used at the beginning of a line of code to define code blocks. 🔹 Why is it important? In Python, indentation is not just for readability—it's syntactically required. It tells Python which statements belong together (e.g., under a function, loop, if condition). Missing or incorrect indentation leads to an IndentationError.
Which of the following is mutable?
a) Tuple b) Set c) String d) IntegerAnswer
b) Set
Output?
a = 7 b = 2 print(a // b) print(a % b)
Answer
Output: 3 1 Explanation: a // b → 7 // 2 = 3 (quotient without the decimal part) a % b → 7 % 2 = 1 (remainder of the division)
You are creating a program to take a user's name and display a greeting. Fill in the missing code:
name = input("Enter your name: ") print("Hello, __________")
Answer
Enter your name: John
Hello, John
You want to write a loop that prints numbers from 1 to 5. Complete the code:
for i in __________: print(__________)
Answer
for i in range(1, 6): # range(1, 6) generates numbers from 1 to 5 print(i) Explanation: range(1, 6) generates numbers starting from 1 up to, but not including, 6, effectively printing 1, 2, 3, 4, and 5. Output: 1 2 3 4 5
Short Code Output
for i in range(1, 4): print("Day", i)
Answer
Day 1 Day 2 Day 3
Output?
x = 5 y = 2 print(x ** y)
Answer
25
Output?
a = "10" b = 3 print(a * b)
Answer
Explanation: a = "10" is a string. b = 3 is an integer. When you use * between a string and an integer, Python repeats the string b times. "10" * 3 results in "101010" (the string "10" repeated 3 times).
What will be the Output?
data = [2, 4, 6] data.append(8) print(data)
Answer
[2, 4, 6, 8]
Output?
info = {"name": "Sara", "age": 20} print(info["age"])
Answer
20
Output?
for i in range(1, 4): print(i * "*")
Answer
* ** ***
Output
text = "python" print(text.upper())
Answer
PYTHON
0 Comments