Basic Programs (4)
Basic Programs (4)
Basic Programs
2. If and elif:
3. For loop and while loop
4. Nested loops
5. Strings
• Objective: Write a program that takes a score and outputs a grade based on the
following conditions: 90+ = A, 80-89 = B, 70-79 = C, 60-69 = D, below 60 = F.
Python code:
grade = 'A'
grade = 'B'
grade = 'C'
grade = 'D'
else:
grade = 'F'
print("Grade:", grade)
Python code:
bill = 0
bill = units * 5
else:
• Objective: Calculate the BMI and classify it based on the value: Underweight
(<18.5), Normal (18.5-24.9), Overweight (25-29.9), Obesity (30+).
Python:
print("Category: Underweight")
print("Category: Normal")
print("Category: Overweight")
else:
print("Category: Obesity")
Python code:
if a == b == c:
elif a == b or b == c or a == c:
else:
Python code: s
if num > 1:
if num % i == 0:
break
else:
• Objective: Calculate the sum of odd and even numbers in a range separately.
Python code;
sum_even = 0
sum_odd = 0
if num % 2 == 0:
sum_even += num
else:
sum_odd += num
a, b = 0, 1
c=a+b
a, b = b, c
power = len(str(num))
if num == sum_of_digits:
4. Nested Loops
print()
for i in range(rows):
for i in range(rows):
num = 1
num = num * (i - j) // (j + 1)
print()
for i in range(n):
5. Strings
vowels = "aeiouAEIOU"
consonant_count = sum(1 for char in text if char.isalpha() and char not in vowels)
print("Vowels:", vowel_count)
print("Consonants:", consonant_count)
5. Strings
vowels = "aeiouAEIOU"
consonant_count = sum(1 for char in text if char.isalpha() and char not in vowels)
print("Vowels:", vowel_count)
print("Consonants:", consonant_count)
if text == text[::-1]:
else:
frequency = {}
for char in text:
frequency[char] = frequency.get(char, 0) + 1
for i in range(len(text)):
print(text[i:j])