Sum of All Elements
lst = [1, 2, 3, 4, 5]
total = 0
for num in lst:
total += num
print(total)
Remove Duplicates
lst = [1, 2, 2, 3, 4, 4, 5]
unique = []
for num in lst:
if num not in unique:
unique.append(num)
print(unique)
Second Largest Element
lst = [10, 20, 4, 45, 99, 99]
unique = []
for num in lst:
if num not in unique:
unique.append(num)
unique.sort()
print(unique[-2])
Reverse a List
def reverse_list(lst):
return lst[::-1]
print(reverse_list([1, 2, 3, 4, 5]))
Sum of All Elements
def sum_list(lst):
total = 0
for num in lst:
total += num
return total
print(sum_list([1, 2, 3, 4, 5]))
Remove Duplicates from a List
def remove_duplicates(lst):
return list(set(lst))
print(remove_duplicates([1, 2, 2, 3, 4, 4, 5]))
Reverse a List
def reverse_list(lst):
return lst[::-1]
Find Maximum Element
lst = [10, 45, 2, 99, 23]
max_num = lst[0]
for i in lst:
if i > max_num:
max_num = i
print("Max:", max_num)
Count Even and Odd Numbers
lst = [1, 2, 3, 4, 5, 6,9]
even = 0
odd = 0
for i in lst:
if i % 2 == 0:
even += 1
else:
odd += 1
print("Even:", even, "Odd:", odd)
Check if all elements of a tuple are even
t = (2, 4, 6, 8)
if all(x % 2 == 0 for x in t):
print("All elements are even")
else:
print("Not all elements are even")
Find the largest number in a tuple using if and
else.
t = (5, 12, 7, 3)
largest = t[0]
for num in t:
if num > largest:
largest = num
print("Largest:", largest)
Palindrome if tuple reads the same forwards
& backwards.
t = (1, 2, 3, 2, 1)
if t == t[::-1]:
print("Palindrome tuple")
else:
print("Not a palindrome")
Check if Tuples have a common element
t1 = (1, 2, 3, 4)
t2 = (5, 6, 2, 8)
found = False
for i in t1:
if i in t2:
found = True
break
if found:
print("Tuples have a common
element")
else:
print("No common elements")
Replace all negative numbers in a tuple
with
t = (-1, 2, -3, 4, -5)
new_t = tuple(0 if x < 0 else x for x in t)
print(new_t)
Check if all elements of a tuple are even
t = (2, 4, 6, 8)
t1 = (1, 2, 3, 4)
t2 = (5, 6, 2, 8)
found = False
for i in t1:
if i in t2:
found = True
break
if found:
print("Tuples have a common
element")
else:
print("No common elements")
Check if a tuple has more positive or
negative numbers.
t = (-1, 2, -3, 4, -5, 6)
pos = sum(1 for x in t if x > 0)
neg = sum(1 for x in t if x < 0)
if pos > neg:
print("More positive numbers")
elif neg > pos:
print("More negative numbers")
else:
print("Equal positives and negatives")
Grade system using a tuple of marks.
marks = (85, 70, 90)
avg = sum(marks) / len(marks)
if avg >= 80:
print("Grade: A")
elif avg >= 60:
print("Grade: B")
else:
print("Grade: C")
Check if a tuple has more positive or
negative numbers.
t = (-1, 2, -3, 4, -5, 6)
pos = sum(1 for x in t if x > 0)
neg = sum(1 for x in t if x < 0)
if pos > neg:
print("More positive numbers")
elif neg > pos:
print("More negative numbers")
else:
print("Equal positives and negatives")
Grade system using a tuple of marks.
marks = (85, 70, 90)
avg = sum(marks) / len(marks)
if avg >= 80:
print("Grade: A")
elif avg >= 60:
print("Grade: B")
else:
print("Grade: C")
First Repeating Element
def first_repeating(lst):
seen = set()
for num in lst:
if num in seen:
return num
seen.add(num)
return None
nums = [10, 5, 3, 4, 3, 5, 6]
print("First repeating:",
first_repeating(nums))
from collections import Counter
def are_anagrams(s1, s2):
return Counter(s1) == Counter(s2)
print(are_anagrams("listen", "silent")) #
True
print(are_anagrams("hello", "world"))
Check if two sets are disjoint
a = {1, 2, 3}
b = {4, 5, 6}
if a.isdisjoint(b):
print("Sets are disjoint (no common
elements)")
else:
print("Sets are not disjoint")
largest set among three sets.
a = {1, 2}
b = {3, 4, 5}
c = {6, 7, 8, 9}
largest = max([a, b, c], key=len)
if largest == a:
print("Set A is largest")
elif largest == b:
print("Set B is largest")
else:
print("Set C is largest")
Count frequency of elements across
multiple sets
from collections import Counter
a = {1, 2, 3}
b = {2, 3, 4}
c = {3, 4, 5}
all_items = list(a) + list(b) + list(c)
freq = Counter(all_items)
print(freq)
Set of Characters from String
def unique_vowels(text):
vowels = {'a', 'e', 'i', 'o', 'u'}
return {ch for ch in text.lower() if ch in
vowels}
print(unique_vowels("Programming in
Python"))
Dictionary questions
Find the sum of all dictionary values.
d = {"a": 10, "b": 20, "c": 30}
print(sum(d.values()))
Create a dictionary with squares of numbers from 1 to 5.
d = {x: x**2 for x in range(1, 6)}
print(d)
Find keys in dictionary with even values.
d = {"a": 11, "b": 20, "c": 15, "d": 30}
result = [k for k, v in d.items() if v % 2 == 0]
print(result)
Count the frequency of characters in a string using a dictionary.
s = "mississippi"
freq = {}
for ch in s:
freq[ch] = freq.get(ch, 0) + 1
print(freq)
# Calculator program
while True:
print("n--- Simple Calculator ---")
print("Available operations: + - * /")
choice = input("Enter operation (or 'q' to
quit): ")
if choice == "q":
print("Exiting Calculator... Goodbye!")
break
if choice in operations:
try:
num1 = float(input("Enter first
number: "))
num2 = float(input("Enter second
number: "))
result = operations[choice](num1,
num2) # Call function from dictionary
print("Result:", result)
except ValueError:
print("Invalid input! Please enter
numbers only.")
else:
print("Invalid operation! Try again.")
# Define operations as functions
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
if b != 0:
return a / b
else:
return "Error! Division by zero"
# Mapping operators to functions using
dictionary
operations = {
"+": add,
"-": subtract,
"*": multiply,
"/": divide
}
accounts = {"12345": {"name": "John", "balance": 5000, "pin": "1111"}}
acc_no = input("Enter Account Number: ")
pin = input("Enter PIN: ")
if acc_no in accounts and accounts[acc_no]["pin"] == pin:
print(f"Welcome {accounts[acc_no]['name']}")
choice = input("1. Check Balance 2. Withdraw Money: ")
if choice == "1":
print("Balance:", accounts[acc_no]["balance"])
elif choice == "2":
amount = int(input("Enter amount: "))
if amount <= accounts[acc_no]["balance"]:
accounts[acc_no]["balance"] -= amount
print("Withdrawal Successful! New Balance:", accounts[acc_no]
["balance"])
else:
print("Insufficient Balance")
else:
print("Invalid Choice")
else:
print("Invalid Account Number or PIN")
Student Report Card System
students = {
"Alice": {"Math": 85, "Science": 90, "English": 78},
"Bob": {"Math": 45, "Science": 50, "English": 35},
"Charlie": {"Math": 92, "Science": 88, "English": 95}
}
for name, marks in students.items():
total = sum(marks.values())
average = total / len(marks)
if average >= 80:
grade = "A"
elif average >= 60:
grade = "B"
elif average >= 40:
grade = "C"
else:
grade = "Fail"
print(f"{name}: Total={total}, Average={average:.2f}, Grade={grade}")
Simple Contact Book
contacts = {}
while True:
choice = input("n1. Add Contact 2. Search Contact 3. Exit: ")
if choice == "1":
name = input("Enter Name: ")
phone = input("Enter Phone: ")
contacts[name] = phone
print("Contact Saved!")
elif choice == "2":
name = input("Enter Name to Search: ")
if name in contacts:
print(f"{name}'s number is {contacts[name]}")
else:
print("Contact not found")
elif choice == "3":
break
else:
print("Invalid Option")
Shopping Cart Billing System
store = {"apple": 50, "banana": 20, "milk": 30, "bread": 25}
cart = {}
while True:
item = input("Enter item (or 'done' to finish): ").lower()
if item == "done":
break
if item in store:
qty = int(input("Enter quantity: "))
cart[item] = cart.get(item, 0) + qty
else:
print("Item not available in store.")
# Bill calculation
total = 0
print("n--- Bill ---")
for item, qty in cart.items():
price = store[item] * qty
total += price
print(f"{item} x {qty} = {price}")
if total > 200:
discount = total * 0.1
total -= discount
print("10% discount applied!")
Library Management System
library = {"Python101": True, "DataScience": True, "AI": False}
# True = Available, False = Borrowed
book = input("Enter book name: ")
if book in library:
if library[book]:
print(f"{book} is available. You can borrow it.")
library[book] = False # Now borrowed
else:
print(f"Sorry, {book} is already borrowed.")
else:
print("Book not found in library")

Practise best aws it is just use this Questions.pptx

  • 2.
    Sum of AllElements lst = [1, 2, 3, 4, 5] total = 0 for num in lst: total += num print(total) Remove Duplicates lst = [1, 2, 2, 3, 4, 4, 5] unique = [] for num in lst: if num not in unique: unique.append(num) print(unique) Second Largest Element lst = [10, 20, 4, 45, 99, 99] unique = [] for num in lst: if num not in unique: unique.append(num) unique.sort() print(unique[-2]) Reverse a List def reverse_list(lst): return lst[::-1] print(reverse_list([1, 2, 3, 4, 5])) Sum of All Elements def sum_list(lst): total = 0 for num in lst: total += num return total print(sum_list([1, 2, 3, 4, 5])) Remove Duplicates from a List def remove_duplicates(lst): return list(set(lst)) print(remove_duplicates([1, 2, 2, 3, 4, 4, 5])) Reverse a List def reverse_list(lst): return lst[::-1] Find Maximum Element lst = [10, 45, 2, 99, 23] max_num = lst[0] for i in lst: if i > max_num: max_num = i print("Max:", max_num) Count Even and Odd Numbers lst = [1, 2, 3, 4, 5, 6,9] even = 0 odd = 0 for i in lst: if i % 2 == 0: even += 1 else: odd += 1 print("Even:", even, "Odd:", odd)
  • 3.
    Check if allelements of a tuple are even t = (2, 4, 6, 8) if all(x % 2 == 0 for x in t): print("All elements are even") else: print("Not all elements are even") Find the largest number in a tuple using if and else. t = (5, 12, 7, 3) largest = t[0] for num in t: if num > largest: largest = num print("Largest:", largest) Palindrome if tuple reads the same forwards & backwards. t = (1, 2, 3, 2, 1) if t == t[::-1]: print("Palindrome tuple") else: print("Not a palindrome") Check if Tuples have a common element t1 = (1, 2, 3, 4) t2 = (5, 6, 2, 8) found = False for i in t1: if i in t2: found = True break if found: print("Tuples have a common element") else: print("No common elements") Replace all negative numbers in a tuple with t = (-1, 2, -3, 4, -5) new_t = tuple(0 if x < 0 else x for x in t) print(new_t) Check if all elements of a tuple are even t = (2, 4, 6, 8) t1 = (1, 2, 3, 4) t2 = (5, 6, 2, 8) found = False for i in t1: if i in t2: found = True break if found: print("Tuples have a common element") else: print("No common elements")
  • 4.
    Check if atuple has more positive or negative numbers. t = (-1, 2, -3, 4, -5, 6) pos = sum(1 for x in t if x > 0) neg = sum(1 for x in t if x < 0) if pos > neg: print("More positive numbers") elif neg > pos: print("More negative numbers") else: print("Equal positives and negatives") Grade system using a tuple of marks. marks = (85, 70, 90) avg = sum(marks) / len(marks) if avg >= 80: print("Grade: A") elif avg >= 60: print("Grade: B") else: print("Grade: C") Check if a tuple has more positive or negative numbers. t = (-1, 2, -3, 4, -5, 6) pos = sum(1 for x in t if x > 0) neg = sum(1 for x in t if x < 0) if pos > neg: print("More positive numbers") elif neg > pos: print("More negative numbers") else: print("Equal positives and negatives") Grade system using a tuple of marks. marks = (85, 70, 90) avg = sum(marks) / len(marks) if avg >= 80: print("Grade: A") elif avg >= 60: print("Grade: B") else: print("Grade: C")
  • 5.
    First Repeating Element deffirst_repeating(lst): seen = set() for num in lst: if num in seen: return num seen.add(num) return None nums = [10, 5, 3, 4, 3, 5, 6] print("First repeating:", first_repeating(nums)) from collections import Counter def are_anagrams(s1, s2): return Counter(s1) == Counter(s2) print(are_anagrams("listen", "silent")) # True print(are_anagrams("hello", "world")) Check if two sets are disjoint a = {1, 2, 3} b = {4, 5, 6} if a.isdisjoint(b): print("Sets are disjoint (no common elements)") else: print("Sets are not disjoint") largest set among three sets. a = {1, 2} b = {3, 4, 5} c = {6, 7, 8, 9} largest = max([a, b, c], key=len) if largest == a: print("Set A is largest") elif largest == b: print("Set B is largest") else: print("Set C is largest") Count frequency of elements across multiple sets from collections import Counter a = {1, 2, 3} b = {2, 3, 4} c = {3, 4, 5} all_items = list(a) + list(b) + list(c) freq = Counter(all_items) print(freq) Set of Characters from String def unique_vowels(text): vowels = {'a', 'e', 'i', 'o', 'u'} return {ch for ch in text.lower() if ch in vowels} print(unique_vowels("Programming in Python"))
  • 6.
    Dictionary questions Find thesum of all dictionary values. d = {"a": 10, "b": 20, "c": 30} print(sum(d.values())) Create a dictionary with squares of numbers from 1 to 5. d = {x: x**2 for x in range(1, 6)} print(d) Find keys in dictionary with even values. d = {"a": 11, "b": 20, "c": 15, "d": 30} result = [k for k, v in d.items() if v % 2 == 0] print(result) Count the frequency of characters in a string using a dictionary. s = "mississippi" freq = {} for ch in s: freq[ch] = freq.get(ch, 0) + 1 print(freq) # Calculator program while True: print("n--- Simple Calculator ---") print("Available operations: + - * /") choice = input("Enter operation (or 'q' to quit): ") if choice == "q": print("Exiting Calculator... Goodbye!") break if choice in operations: try: num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: ")) result = operations[choice](num1, num2) # Call function from dictionary print("Result:", result) except ValueError: print("Invalid input! Please enter numbers only.") else: print("Invalid operation! Try again.") # Define operations as functions def add(a, b): return a + b def subtract(a, b): return a - b def multiply(a, b): return a * b def divide(a, b): if b != 0: return a / b else: return "Error! Division by zero" # Mapping operators to functions using dictionary operations = { "+": add, "-": subtract, "*": multiply, "/": divide }
  • 7.
    accounts = {"12345":{"name": "John", "balance": 5000, "pin": "1111"}} acc_no = input("Enter Account Number: ") pin = input("Enter PIN: ") if acc_no in accounts and accounts[acc_no]["pin"] == pin: print(f"Welcome {accounts[acc_no]['name']}") choice = input("1. Check Balance 2. Withdraw Money: ") if choice == "1": print("Balance:", accounts[acc_no]["balance"]) elif choice == "2": amount = int(input("Enter amount: ")) if amount <= accounts[acc_no]["balance"]: accounts[acc_no]["balance"] -= amount print("Withdrawal Successful! New Balance:", accounts[acc_no] ["balance"]) else: print("Insufficient Balance") else: print("Invalid Choice") else: print("Invalid Account Number or PIN") Student Report Card System students = { "Alice": {"Math": 85, "Science": 90, "English": 78}, "Bob": {"Math": 45, "Science": 50, "English": 35}, "Charlie": {"Math": 92, "Science": 88, "English": 95} } for name, marks in students.items(): total = sum(marks.values()) average = total / len(marks) if average >= 80: grade = "A" elif average >= 60: grade = "B" elif average >= 40: grade = "C" else: grade = "Fail" print(f"{name}: Total={total}, Average={average:.2f}, Grade={grade}")
  • 8.
    Simple Contact Book contacts= {} while True: choice = input("n1. Add Contact 2. Search Contact 3. Exit: ") if choice == "1": name = input("Enter Name: ") phone = input("Enter Phone: ") contacts[name] = phone print("Contact Saved!") elif choice == "2": name = input("Enter Name to Search: ") if name in contacts: print(f"{name}'s number is {contacts[name]}") else: print("Contact not found") elif choice == "3": break else: print("Invalid Option") Shopping Cart Billing System store = {"apple": 50, "banana": 20, "milk": 30, "bread": 25} cart = {} while True: item = input("Enter item (or 'done' to finish): ").lower() if item == "done": break if item in store: qty = int(input("Enter quantity: ")) cart[item] = cart.get(item, 0) + qty else: print("Item not available in store.") # Bill calculation total = 0 print("n--- Bill ---") for item, qty in cart.items(): price = store[item] * qty total += price print(f"{item} x {qty} = {price}") if total > 200: discount = total * 0.1 total -= discount print("10% discount applied!")
  • 9.
    Library Management System library= {"Python101": True, "DataScience": True, "AI": False} # True = Available, False = Borrowed book = input("Enter book name: ") if book in library: if library[book]: print(f"{book} is available. You can borrow it.") library[book] = False # Now borrowed else: print(f"Sorry, {book} is already borrowed.") else: print("Book not found in library")