Python Coding Questions by Topic
Input and Output
Print Full Name
Description:
Take first and last name as input and print full name.
Input and Output Format:
Input: Two strings
Output: Full name
Constraints:
1 <= length <= 100
Explanation for Sample Test Case:
Concatenate first and last names.
Sample Test Cases:
Input: John Doe
Output: John Doe
Area of Circle
Description:
Take radius as input and compute the area.
Input and Output Format:
Input: Float radius
Output: Float area
Constraints:
0 < r <= 1000
Explanation for Sample Test Case:
Use formula: Area = πr²
Sample Test Cases:
Input: 2
Output: 12.566
Sum of Two Integers
Description:
Read two integers and print their sum.
Input and Output Format:
Input: Two integers
Output: Their sum
Constraints:
1 <= a,b <= 1000
Explanation for Sample Test Case:
Just add the two numbers.
Sample Test Cases:
Input: 5 10
Output: 15
Multiply Three Numbers
Description:
Multiply three inputs and display the result.
Input and Output Format:
Input: Three integers
Output: Product
Constraints:
1 <= n <= 100
Explanation for Sample Test Case:
Simple multiplication.
Sample Test Cases:
Input: 2 3 4
Output: 24
Echo the Input
Description:
Print the same input back to the user.
Input and Output Format:
Input: String
Output: Same string
Constraints:
1 <= len <= 500
Explanation for Sample Test Case:
Print the input directly.
Sample Test Cases:
Input: Hello GPT
Output: Hello GPT
Strings
Reverse String
Description:
Reverse a given string.
Input and Output Format:
Input: String
Output: Reversed string
Constraints:
1 <= len <= 1000
Explanation for Sample Test Case:
Use slicing.
Sample Test Cases:
Input: hello
Output: olleh
Count Vowels
Description:
Count vowels in the string.
Input and Output Format:
Input: String
Output: Integer count
Constraints:
1 <= len <= 1000
Explanation for Sample Test Case:
Check each character.
Sample Test Cases:
Input: python
Output: 1
Check Palindrome
Description:
Check if string is a palindrome.
Input and Output Format:
Input: String
Output: Yes or No
Constraints:
1 <= len <= 1000
Explanation for Sample Test Case:
Reverse == original.
Sample Test Cases:
Input: madam
Output: Yes
To Uppercase
Description:
Convert to uppercase.
Input and Output Format:
Input: String
Output: Uppercase string
Constraints:
1 <= len <= 1000
Explanation for Sample Test Case:
Use upper().
Sample Test Cases:
Input: abc
Output: ABC
Word Count
Description:
Count words in a sentence.
Input and Output Format:
Input: Sentence
Output: Integer count
Constraints:
1 <= len <= 1000
Explanation for Sample Test Case:
Split and count.
Sample Test Cases:
Input: Hello world GPT
Output: 3
Replace Substring
Description:
Replace one word with another.
Input and Output Format:
Input: String, old, new
Output: Modified string
Constraints:
1 <= len <= 1000
Explanation for Sample Test Case:
Use replace().
Sample Test Cases:
Input: hello world, world, GPT
Output: hello GPT
Find Substring
Description:
Check if a substring exists.
Input and Output Format:
Input: Two strings
Output: Yes/No
Constraints:
1 <= len <= 1000
Explanation for Sample Test Case:
Use in keyword.
Sample Test Cases:
Input: Hello GPT, GPT
Output: Yes
Frequency of Characters
Description:
Count each character’s frequency.
Input and Output Format:
Input: String
Output: Character counts
Constraints:
1 <= len <= 1000
Explanation for Sample Test Case:
Use dictionary.
Sample Test Cases:
Input: abcab
Output: a:2 b:2 c:1
Remove Whitespaces
Description:
Remove all spaces.
Input and Output Format:
Input: String
Output: String without spaces
Constraints:
1 <= len <= 1000
Explanation for Sample Test Case:
Use replace(' ', '')
Sample Test Cases:
Input: a b c
Output: abc
Title Case
Description:
Convert string to title case.
Input and Output Format:
Input: String
Output: Title case string
Constraints:
1 <= len <= 1000
Explanation for Sample Test Case:
Capitalize words.
Sample Test Cases:
Input: hello python
Output: Hello Python
Lists
Sum of List
Description:
Compute sum of all elements.
Input and Output Format:
Input: List of integers
Output: Integer
Constraints:
1 <= n <= 1000
Explanation for Sample Test Case:
Use sum().
Sample Test Cases:
Input: [1,2,3]
Output: 6
Maximum in List
Description:
Find maximum number.
Input and Output Format:
Input: List
Output: Integer max
Constraints:
1 <= n <= 1000
Explanation for Sample Test Case:
Use max().
Sample Test Cases:
Input: [2,5,1]
Output: 5
List Sorting
Description:
Sort list in ascending order.
Input and Output Format:
Input: List
Output: Sorted list
Constraints:
1 <= n <= 1000
Explanation for Sample Test Case:
Use sort().
Sample Test Cases:
Input: [3,1,2]
Output: [1,2,3]
List Reversal
Description:
Reverse the list.
Input and Output Format:
Input: List
Output: Reversed list
Constraints:
1 <= n <= 1000
Explanation for Sample Test Case:
Use reverse().
Sample Test Cases:
Input: [1,2,3]
Output: [3,2,1]
Duplicate Removal
Description:
Remove duplicates from list.
Input and Output Format:
Input: List
Output: List
Constraints:
1 <= n <= 1000
Explanation for Sample Test Case:
Convert to set.
Sample Test Cases:
Input: [1,2,2,3]
Output: [1,2,3]
Index of Element
Description:
Find index of a value.
Input and Output Format:
Input: List and value
Output: Index
Constraints:
1 <= n <= 1000
Explanation for Sample Test Case:
Use index().
Sample Test Cases:
Input: [4,5,6], 5
Output: 1
Count Occurrences
Description:
Count times an element appears.
Input and Output Format:
Input: List and element
Output: Count
Constraints:
1 <= n <= 1000
Explanation for Sample Test Case:
Use count().
Sample Test Cases:
Input: [1,1,2], 1
Output: 2
List Comprehension Squares
Description:
Return squares of list elements.
Input and Output Format:
Input: List
Output: List of squares
Constraints:
1 <= n <= 1000
Explanation for Sample Test Case:
Use [x*x for x in list]
Sample Test Cases:
Input: [1,2,3]
Output: [1,4,9]
Even Numbers Only
Description:
Filter only even numbers.
Input and Output Format:
Input: List
Output: Even list
Constraints:
1 <= n <= 1000
Explanation for Sample Test Case:
Use mod operator.
Sample Test Cases:
Input: [1,2,3,4]
Output: [2,4]
Merge Two Lists
Description:
Combine two lists.
Input and Output Format:
Input: Two lists
Output: Merged list
Constraints:
1 <= n <= 1000
Explanation for Sample Test Case:
Use + operator.
Sample Test Cases:
Input: [1,2] [3,4]
Output: [1,2,3,4]
Data Types
Identify Data Type
Description:
Print the type of a given input value.
Input and Output Format:
Input: A single value
Output: Type as string
Constraints:
May be int, float, str
Explanation for Sample Test Case:
Use type() function.
Sample Test Cases:
Input: 42
Output: int
Type Conversion
Description:
Convert float to int and vice versa.
Input and Output Format:
Input: A float or int
Output: Converted value
Constraints:
Valid number input
Explanation for Sample Test Case:
Use int() and float().
Sample Test Cases:
Input: 5.9
Output: 5
Check Boolean Type
Description:
Determine if a value is boolean.
Input and Output Format:
Input: Any value
Output: True/False
Constraints:
Input may be any data type
Explanation for Sample Test Case:
Compare with bool.
Sample Test Cases:
Input: True
Output: True
String to List
Description:
Convert string to list of characters.
Input and Output Format:
Input: String
Output: List of characters
Constraints:
1 <= len <= 1000
Explanation for Sample Test Case:
Use list() constructor.
Sample Test Cases:
Input: hello
Output: ['h','e','l','l','o']
Integer to Binary
Description:
Convert an integer to binary.
Input and Output Format:
Input: Integer
Output: Binary string
Constraints:
1 <= n <= 1000
Explanation for Sample Test Case:
Use bin() function.
Sample Test Cases:
Input: 5
Output: 0b101
Operators
Add Two Numbers
Description:
Use + operator for addition.
Input and Output Format:
Input: Two integers
Output: Sum
Constraints:
1 <= n <= 1000
Explanation for Sample Test Case:
Simple addition.
Sample Test Cases:
Input: 3 4
Output: 7
Use Modulo
Description:
Find remainder of division.
Input and Output Format:
Input: Two integers
Output: Remainder
Constraints:
1 <= n <= 1000
Explanation for Sample Test Case:
Use % operator.
Sample Test Cases:
Input: 10 3
Output: 1
Comparison Operators
Description:
Compare two numbers using >.
Input and Output Format:
Input: Two integers
Output: True/False
Constraints:
1 <= n <= 1000
Explanation for Sample Test Case:
Use > operator.
Sample Test Cases:
Input: 4 3
Output: True
Logical AND
Description:
Evaluate a and b.
Input and Output Format:
Input: Two boolean values
Output: True/False
Constraints:
Boolean input
Explanation for Sample Test Case:
Use and operator.
Sample Test Cases:
Input: True False
Output: False
Exponent Operator
Description:
Compute power using **.
Input and Output Format:
Input: Two integers
Output: a ** b
Constraints:
1 <= a, b <= 10
Explanation for Sample Test Case:
Use exponentiation.
Sample Test Cases:
Input: 2 3
Output: 8
Conditional Statements
Positive or Negative
Description:
Check sign of number.
Input and Output Format:
Input: Integer
Output: Positive/Negative/Zero
Constraints:
-1000 <= n <= 1000
Explanation for Sample Test Case:
Use if-elif-else.
Sample Test Cases:
Input: -1
Output: Negative
Check Even/Odd
Description:
Check parity of number.
Input and Output Format:
Input: Integer
Output: Even or Odd
Constraints:
1 <= n <= 1000
Explanation for Sample Test Case:
Use % 2.
Sample Test Cases:
Input: 4
Output: Even
Eligibility to Vote
Description:
Age >= 18 means eligible.
Input and Output Format:
Input: Age
Output: Eligible/Not Eligible
Constraints:
1 <= age <= 150
Explanation for Sample Test Case:
Use if condition.
Sample Test Cases:
Input: 20
Output: Eligible
Max of Two Numbers
Description:
Print maximum of two numbers.
Input and Output Format:
Input: Two integers
Output: Larger integer
Constraints:
1 <= n <= 1000
Explanation for Sample Test Case:
Use if-else.
Sample Test Cases:
Input: 3 5
Output: 5
Check Divisibility
Description:
Check if number is divisible by 5 and 11.
Input and Output Format:
Input: Integer
Output: Yes/No
Constraints:
1 <= n <= 10000
Explanation for Sample Test Case:
Use % operator and if.
Sample Test Cases:
Input: 55
Output: Yes
Nested Conditional Statements
Grade Evaluation
Description:
Assign grades based on marks using nested if.
Input and Output Format:
Input: Integer marks
Output: Grade
Constraints:
0 <= marks <= 100
Explanation for Sample Test Case:
Use nested if-else to assign A/B/C/D/F.
Sample Test Cases:
Input: 85
Output: Grade A
Age Category
Description:
Categorize a person based on age.
Input and Output Format:
Input: Age
Output: Infant/Child/Teen/Adult/Senior
Constraints:
0 <= age <= 120
Explanation for Sample Test Case:
Use nested conditions.
Sample Test Cases:
Input: 10
Output: Child
Check Leap Year
Description:
Use nested if to check leap year.
Input and Output Format:
Input: Year
Output: Yes/No
Constraints:
1000 <= year <= 9999
Explanation for Sample Test Case:
Use rules: divisible by 4, 100, 400.
Sample Test Cases:
Input: 2000
Output: Yes
Login System
Description:
Check username and password using nested if.
Input and Output Format:
Input: username, password
Output: Login Success or Failure
Constraints:
Predefined values
Explanation for Sample Test Case:
First check username, then password.
Sample Test Cases:
Input: admin 1234
Output: Login Success
Nested Number Check
Description:
Check if number is even and greater than 10.
Input and Output Format:
Input: Integer
Output: Yes/No
Constraints:
1 <= n <= 1000
Explanation for Sample Test Case:
Use if inside if.
Sample Test Cases:
Input: 12
Output: Yes
Switch Case (match-case)
Day of the Week
Description:
Print day based on number using match-case.
Input and Output Format:
Input: Integer (1-7)
Output: Day name
Constraints:
1 <= n <= 7
Explanation for Sample Test Case:
Use match-case to map days.
Sample Test Cases:
Input: 1
Output: Monday
Basic Calculator
Description:
Perform operation using match-case.
Input and Output Format:
Input: Two numbers and operator
Output: Result
Constraints:
Valid operators: + - * /
Explanation for Sample Test Case:
Use match-case for operator.
Sample Test Cases:
Input: 4 2 *
Output: 8
Month Name
Description:
Return month name from number.
Input and Output Format:
Input: 1-12
Output: Month name
Constraints:
1 <= n <= 12
Explanation for Sample Test Case:
Use match-case
Sample Test Cases:
Input: 3
Output: March
Grade Descriptor
Description:
Return grade meaning using match-case.
Input and Output Format:
Input: A/B/C/D/F
Output: Description
Constraints:
Valid grade inputs
Explanation for Sample Test Case:
Map A=Excellent, etc.
Sample Test Cases:
Input: B
Output: Good
Traffic Light Action
Description:
Return action based on traffic light color.
Input and Output Format:
Input: Red/Yellow/Green
Output: Action
Constraints:
Valid traffic light input
Explanation for Sample Test Case:
Red=Stop, Yellow=Wait, Green=Go
Sample Test Cases:
Input: Red
Output: Stop
Control Statements
Break in Loop
Description:
Stop loop on specific value.
Input and Output Format:
Input: List of integers
Output: Stop at value
Constraints:
1 <= n <= 100
Explanation for Sample Test Case:
Use break statement.
Sample Test Cases:
Input: [1,2,3,4,5], stop=3
Output: 1 2
Continue in Loop
Description:
Skip even numbers.
Input and Output Format:
Input: List
Output: Odd numbers only
Constraints:
1 <= n <= 100
Explanation for Sample Test Case:
Use continue.
Sample Test Cases:
Input: [1,2,3,4]
Output: 1 3
Pass Statement
Description:
Write pass for empty loop block.
Input and Output Format:
Input: N
Output: No output (just pass)
Constraints:
1 <= N <= 10
Explanation for Sample Test Case:
Syntax check only.
Sample Test Cases:
Input: 3
Output: (no output)
Nested Loop with Break
Description:
Break from inner loop.
Input and Output Format:
Input: Matrix
Output: Partial traversal
Constraints:
2D list input
Explanation for Sample Test Case:
Use nested break.
Sample Test Cases:
Input: [[1,2],[3,4]]
Output: 1 2
Loop Until Prime
Description:
Use while to find first N primes.
Input and Output Format:
Input: Integer N
Output: First N primes
Constraints:
1 <= N <= 50
Explanation for Sample Test Case:
Use while with control.
Sample Test Cases:
Input: 3
Output: 2 3 5
Tuples
Create and Access Tuple
Description:
Create tuple and access elements.
Input and Output Format:
Input: Elements
Output: Accessed item
Constraints:
1 <= len <= 100
Explanation for Sample Test Case:
Index access in tuple.
Sample Test Cases:
Input: (1,2,3), index 1
Output: 2
Tuple Concatenation
Description:
Join two tuples.
Input and Output Format:
Input: Two tuples
Output: Concatenated tuple
Constraints:
Valid tuple input
Explanation for Sample Test Case:
Use + operator.
Sample Test Cases:
Input: (1,2), (3,4)
Output: (1,2,3,4)
Tuple Length
Description:
Find number of items.
Input and Output Format:
Input: Tuple
Output: Length
Constraints:
1 <= len <= 100
Explanation for Sample Test Case:
Use len().
Sample Test Cases:
Input: (1,2,3)
Output: 3
Tuple Slicing
Description:
Slice a portion from tuple.
Input and Output Format:
Input: Tuple
Output: Sliced part
Constraints:
Valid indexes
Explanation for Sample Test Case:
Use [:] slicing.
Sample Test Cases:
Input: (1,2,3,4)[1:3]
Output: (2,3)
Check Element in Tuple
Description:
Find if element exists.
Input and Output Format:
Input: Tuple and value
Output: Yes/No
Constraints:
1 <= len <= 100
Explanation for Sample Test Case:
Use 'in' keyword.
Sample Test Cases:
Input: (1,2,3), 2
Output: Yes