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

Python Qna

The document contains various Python programming tasks including basic arithmetic operations, loops for summing numbers, random selection from lists, signal validation, electricity bill calculation, character type checking, digit counting, grade calculation, and more. Each task is presented with code snippets demonstrating the required functionality. The document serves as a practical guide for implementing fundamental programming concepts in Python.

Uploaded by

Pyae Sone
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 views6 pages

Python Qna

The document contains various Python programming tasks including basic arithmetic operations, loops for summing numbers, random selection from lists, signal validation, electricity bill calculation, character type checking, digit counting, grade calculation, and more. Each task is presented with code snippets demonstrating the required functionality. The document serves as a practical guide for implementing fundamental programming concepts in Python.

Uploaded by

Pyae Sone
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

Write a Python program to accept two numbers and an operator from a menu.

num1 = float(input("Enter first number: "))


num2 = float(input("Enter second number: "))

print("1. Addition")
print("2. Subtraction")
print("3. Exit")

choice = input("Enter choice: ")

if choice == "1":
print("Result:", num1 + num2)
elif choice == "2":
print("Result:", num1 - num2)
else:
print("Exit")

Write a program using FOR loop to accept 12 numbers and display sum.
total = 0
for i in range(12):
num = int(input("Enter number: "))
total += num
print("Total sum:", total)

Create list 1–15 and randomly select 3 numbers.


import random
nums = list(range(1,16))
selected = [Link](nums, 3)
print(selected)

Display only valid signals.


signals = ["NM", "V-3", "D+", "XY", "Z9", "+P"]

for s in signals:
if len(s) >= 2 and "+" not in s and "-" not in s:
print(s)

Separate good and bad signals.


signals = ["V1", "N-3", "J+", "A", "M5", "T+", "H-1"]
good_signals = []
bad_signals = []

for s in signals:
if "+" in s:
good_signals.append(s)
if "-" in s or len(s) < 2:
bad_signals.append(s)

print("Good:", good_signals)
print("Bad:", bad_signals)

Electricity bill calculation.


units = int(input("Enter units: "))
bill = 0

if units <= 100:


bill = units * 2
elif units <= 200:
bill = 100 * 2 + (units - 100) * 4
else:
bill = 100 * 2 + 100 * 4 + (units - 200) * 6

print("Bill amount:", bill)

Randomly select students.


import random
students = ["Aung", "Bella", "Cherry", "Diana", "Emma", "Pinky"]

print("All Students:")
for s in students:
print("-", s)

selected = [Link](students, 3)
print("Selected:", selected)

Character type checker.


ch = input("Enter a character: ")

if [Link]():
print("Uppercase alphabet")
elif [Link]():
print("Lowercase alphabet")
elif [Link]():
print("Digit")
else:
print("Special symbol")
Find output of the given program.
a = 20
b=5
a += 5
print(a) # 25
b *= 3
print(b) # 15
a //= b
print(a) # 1
b -= 2
print(b) # 13
a += b
print(a) # 14
a %= b
print(b) # 13

Count digits using WHILE and FOR.


num = int(input("Enter a number: "))
count = 0
while num != 0:
num //= 10
count += 1
print("Total digits:", count)

num = input("Enter a number: ")


count = 0
for d in num:
count += 1
print("Total digits:", count)

Grade calculator.
mark = int(input("Enter mark: "))

if mark >= 90:


print("Grade A")
elif mark >= 80:
print("Grade B")
elif mark >= 70:
print("Grade C")
elif mark >= 60:
print("Grade D")
elif mark >= 50:
print("Grade E")
else:
print("Grade F")
Number pattern program.
n = int(input("Enter number: "))

for i in range(1, n+1):


for j in range(1, i+1):
print(j, end=" ")
print()

Sum of digits using FOR and WHILE.


num = int(input("Enter a number: "))
total = 0
while num > 0:
digit = num % 10
total += digit
num //= 10
print("Sum:", total)

num = input("Enter a number: ")


total = 0
for d in num:
if [Link]():
total += int(d)
print("Sum:", total)

Temperature message program.


temp = int(input("Enter temp: "))

if temp < 14:


print("Cold")
elif temp <= 25:
print("Warm")
else:
print("Hot")

Edit list commands.


trees = ["oak", "beech", "willow", "maple"]

del trees[1]
trees[0] = "eucalyptus"
[Link]("teak")

print(trees)
Age-based ticket system.
age = int(input("Enter age: "))

if age < 10:


print("Free")
elif age <= 18:
print("Price: 5000")
else:
print("Price: 8000")

Total price from list.


prices = [200, 350, 75, 300, 150]
total = 0
for p in prices:
total += p
print("Total:", total)

Star pattern using nested loops.


for i in range(1, 6):
for j in range(i):
print("*", end=" ")
print()

Shop discount program.


amount = float(input("Enter amount: "))

if amount > 1000:


discount = amount * 0.10
else:
discount = amount * 0.05

print("Discount:", discount)

Find highest score without max().


scores = [88, 72, 69, 20, 87]
highest = scores[0]

for s in scores:
if s > highest:
highest = s
print("Highest score:", highest)

Weather temperature analysis.


temps = []
count_hot = 0

for i in range(5):
t = int(input("Enter temperature: "))
[Link](t)
if t > 30:
count_hot += 1

filtered = [t for t in temps if t >= 25]

if count_hot > 2:
print("Hot Weather Period")
else:
print("Normal Period")

print("Filtered list:", filtered)

You might also like