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

Python Coding Questions-1

The document contains various Python code snippets demonstrating different programming concepts, including checking even or odd numbers, reversing strings, generating Fibonacci series, and more. It also includes functions for checking leap years, palindromes, Armstrong numbers, and implementing FizzBuzz, among others. Each code snippet is self-contained and showcases a specific functionality or algorithm.

Uploaded by

kirannakedar02
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)
2 views8 pages

Python Coding Questions-1

The document contains various Python code snippets demonstrating different programming concepts, including checking even or odd numbers, reversing strings, generating Fibonacci series, and more. It also includes functions for checking leap years, palindromes, Armstrong numbers, and implementing FizzBuzz, among others. Each code snippet is self-contained and showcases a specific functionality or algorithm.

Uploaded by

kirannakedar02
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
You are on page 1/ 8

@Tajamulkhann

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 s.lower() == s.lower()[::-1]
print(is_palindrome("madam")) # Output: True

@Tajamulkhann
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
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 s.lower() if char in "aeiou")
print(count_vowels("Hello World")) # Output: 3

ANAGRAM CHECKER
def check_anagram(s1, s2):
return sorted(s1.lower()) == sorted(s2.lower())
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
CHARACTER FREQUENCY IN A STRING
def char_frequency(text):
freq = {}
for char in text.lower():
freq[char] = freq.get(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 = sentence.split()
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 fruits.items():
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 count.items() if freq > 1} # {2: 2, 3: 3}

@Tajamulkhann
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
nums.sort(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
Follow for more!
Follow for more!

You might also like