0% found this document useful (0 votes)
4 views8 pages

Python Coding Questions

The document contains a collection of Python coding questions and solutions categorized into Beginner, Basic, Intermediate, Advanced, Expert, and AI ML Based levels. Each section includes various coding challenges such as checking even or odd numbers, generating Fibonacci series, and implementing algorithms like two-sum and moving average. The solutions are presented in code snippets with brief explanations.

Uploaded by

ry242887
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

Python Coding Questions

The document contains a collection of Python coding questions and solutions categorized into Beginner, Basic, Intermediate, Advanced, Expert, and AI ML Based levels. Each section includes various coding challenges such as checking even or odd numbers, generating Fibonacci series, and implementing algorithms like two-sum and moving average. The solutions are presented in code snippets with brief explanations.

Uploaded by

ry242887
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Top Python

Coding
Questions
@Tajamulkhann
@[Link]
Beginner
EVEN OR ODD CHECKER
# Check if a number is even or odd
num = int(input("Enter a number: "))
print("Even" if num % 2 == 0 else "Odd")

REVERSE A STRING
# Reverse a string using slicing
s = "Python"
print(s[::-1]) # Output: "nohtyP"

FIBONACCI SERIES GENERATOR


# Generate Fibonacci series up to n terms
n = int(input("Enter the number of terms: "))
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b

CHECK LEAP YEAR


# Check if a given year is a leap year
year = int(input("Enter a year: "))
if (year % 4==0 and year % 100!=0) or (year % 400==0):
print("Leap Year")
else:
print("Not a Leap Year")

PALINDROME CHECKER

# Function to check if a string is a palindrome


def is_palindrome(s):
return [Link]() == [Link]()[::-1]
print(is_palindrome("madam")) # Output: True

@Tajamulkhann
@[Link]
Basic
LIST COMPREHENSION CHALLENGE
# Generate squares using list comprehension
squares = [x**2 for x in range(1, 11)]
print(squares) # Output: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

ARMSTRONG NUMBER CHECKER


def is_armstrong(n):
num_str = str(n)
power = len(num_str)
return n == sum(int(digit) ** power for digit in num_str)
print(is_armstrong(153)) # Output: True

LAMBDA FUNCTION USAGE

# Lambda function to compute square of a number


square = lambda x: x ** 2
print(square(5)) # Output: 25

FIZZBUZZ IMPLEMENTATION
# Classic FizzBuzz problem
for i in range(1, 101):
if i % 3 == 0 and i % 5 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)

MAP FUNCTION IMPLEMENTATION

# Using map() to double values in a list


nums = [1, 2, 3, 4]
doubled = list(map(lambda x: x * 2, nums))
print(doubled) # Output: [2, 4, 6, 8]

@Tajamulkhann
@[Link]
Intermediate
FILTER FUNCTION USAGE

# Filter even numbers using filter()


nums = [1, 2, 3, 4, 5, 6]
evens = list(filter(lambda x: x % 2 == 0, nums))
print(evens) # Output: [2, 4, 6]

REMOVE DUPLICATES FROM LIST

# Remove duplicates from a list


duplist = [1, 2, 3, 4, 4, 5, 5]
unique_list = list(set(duplist))
print(unique_list) # Output: [1, 2, 3, 4, 5]

VOWEL COUNTER

# Function to count vowels in a given string


def count_vowels(s):
return sum(1 for char in [Link]() if char in "aeiou")
print(count_vowels("Hello World")) # Output: 3

ANAGRAM CHECKER
def check_anagram(s1, s2):
return sorted([Link]()) == sorted([Link]())
print(check_anagram("listen", "silent")) # Output: True

MISSING NUMBER FINDER


def find_missing(nums, n):
return n * (n + 1) // 2 - sum(nums)
print(find_missing([1, 2, 4, 5], 5)) # Output: 3

@Tajamulkhann
@[Link]
Advanced
CHARACTER FREQUENCY IN A STRING
def char_frequency(text):
freq = {}
for char in [Link]():
freq[char] = [Link](char, 0) + 1
return freq
print(char_frequency("Hello World"))
# Output: {'h':1, 'e':1, 'l':3, 'o':2, ' ':1, 'w':1, 'r':1, 'd':1}

REVERSE WORDS IN A SENTENCE


def reverse_words(sentence):
words = [Link]()
reversed_words = [word[::-1] for word in words]
return " ".join(reversed_words)
print(reverse_words("Hello World")) # Output: "olleH dlroW"

COMMON ELEMENTS OF TWO LISTS

list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 6]
print(list(set(list1) & set(list2))) # Output: [3, 4]

ITERATE OVER A DICTIONARY


fruits = {"apple": 2, "banana": 3, "cherry": 1}
total_quantity = 0
for fruit, quantity in [Link]():
print(fruit, quantity)
total_quantity += quantity
print("Total:", total_quantity) # Output: apple 2, banana 3, cherry 1, Total : 6

REMOVE DUPLICATES
my_list = [1, 2, 3, 4, 2, 5, 3, 6, 3]
from collections import Counter
count = Counter(my_list)
duplicates = {item: freq for item, freq in [Link]() if freq > 1} # {2: 2, 3: 3}

@Tajamulkhann
@[Link]
Expert
PRINT PYRAMID PATTERN
n = 5
for i in range(1, n + 1):
print(" " * (n - i) + "*" * (2 * i - 1))

SECOND LARGEST ELEMENT

def second_largest(nums):
nums = list(set(nums)) # Remove duplicates first
[Link](reverse=True)
return nums[1] if len(nums) > 1 else None
print(second_largest([10, 20, 40, 30])) # Output: 30

NUMBER REVERSAL

def reverse_number(n):
reversed_num = 0 # Start with 0 as the reversed number
while n > 0: # Repeat until no digits left in n
digit = n % 10 # Get the last digit of n
reversed_num = reversed_num * 10 + digit # Add digit to the reversed number
n = n // 10 # Remove the last digit from n
return reversed_num # Return the reversed number
print(reverse_number(12345)) # Output will be 54321

TWO SUM PROBLEM

def two_sum(nums, target):


seen = {} # Dictionary to store number and its index
for i, num in enumerate(nums): # Loop through the list with index
complement = target - num # Find the number needed to reach the target
if complement in seen: # If we've already seen the complement
return [seen[complement], i] # indices of complement and current number
seen[num] = i # Store the current number with its index
return [] # Return empty list if no pair found
# Example usage
print(two_sum([2, 7, 11, 15], 9)) # Output: [0, 1]

@Tajamulkhann
@[Link]
AI ML Based
TOP-K FREQUENT ELEMENTS
from collections import Counter
def top_k_frequent(nums, k):
counts = Counter(nums)
return [item for item, _ in counts.most_common(k)]

SORT LIST OF DICTS BY KEY

def sort_dicts(arr, key):


return sorted(arr, key=lambda x: x[key])

MOVING AVERAGE
def moving_average(nums, k):
window_sum = sum(nums[:k])
averages = [window_sum / k]
for i in range(k, len(nums)):
window_sum += nums[i] - nums[i - k]
[Link](window_sum / k)
return averages

GROUP INTEGERS INTO EVEN AND ODD

def group_even_odd(nums):
even = [x for x in nums if x % 2 == 0]
odd = [x for x in nums if x % 2 != 0]
return even, odd

@Tajamulkhann
@[Link]
Found
Helpful ?
Repost

@Tajamulkhann
@[Link]

Follow for more!

You might also like