0% found this document useful (0 votes)
7 views3 pages

Python Assignment Theory

The document outlines ten Python assignments, each with a theory/concept explanation and example code. Assignments include calculating product or sum based on a threshold, swapping variables, reversing strings, counting substrings, checking for palindromes, calculating income tax, determining leap years, multiplying numbers, inputting multiple names, and calculating percentages with a zero check. Each assignment is accompanied by example outputs to illustrate functionality.

Uploaded by

Tamseel Akhund
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)
7 views3 pages

Python Assignment Theory

The document outlines ten Python assignments, each with a theory/concept explanation and example code. Assignments include calculating product or sum based on a threshold, swapping variables, reversing strings, counting substrings, checking for palindromes, calculating income tax, determining leap years, multiplying numbers, inputting multiple names, and calculating percentages with a zero check. Each assignment is accompanied by example outputs to illustrate functionality.

Uploaded by

Tamseel Akhund
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

Python Assignment (Theory + Code)

1) Product ≤ 1000 → Product else Sum


Theory / Concept:
We take two integer numbers from the user. First, we calculate their product. If the product is less
than or equal to 1000, we display the product; otherwise, we display the sum.
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

product = num1 * num2

if product <= 1000:


print("Result:", product)
else:
print("Result:", num1 + num2)

Example Output:
Enter first number: 20
Enter second number: 30
Result: 600

2) Swap Variables
Theory / Concept:
Swapping means exchanging the values of variables. Python allows swapping without a temp
variable.
a = int(input("Enter a: "))
b = int(input("Enter b: "))

a, b = b, a

print("a:", a)
print("b:", b)

Example Output:
Enter a: 5
Enter b: 10
a: 10
b: 5

3) Reverse String
Theory / Concept:
[::-1] reverses the string using slicing.
text = input("Enter string: ")
print("Reversed:", text[::-1])

Example Output:
Python → nohtyP

4) Count 'Emma'
Theory / Concept:
count() function counts occurrences of a substring.
str_x = "Emma is good developer. Emma is a writer"

count = str_x.count("Emma")
print("Emma appears:", count, "times")

Output:
Emma appears: 2 times

5) Palindrome Number
Theory / Concept:
Palindrome reads same forwards and backwards.
num = input("Enter number: ")

if num == num[::-1]:
print("Palindrome")
else:
print("Not Palindrome")

Example:
121 → Palindrome

6) Income Tax
Theory / Concept:
Tax calculated using income ranges and conditions.
income = float(input("Enter income: "))

tax = 0

if income <= 10000:


tax = 0
elif income <= 20000:
tax = (income - 10000) * 0.10
else:
tax = 10000 * 0.10 + (income - 20000) * 0.20

print("Tax:", tax)

Example:
Income 25000 → Tax 2000.0

7) Leap Year
Theory / Concept:
Leap year divisible by 4 but not 100 unless divisible by 400.
year = int(input("Enter year: "))

if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):


print("Leap Year")
else:
print("Not Leap Year")

Example:
2024 → Leap Year

8) Product of Two Numbers


Theory / Concept:
Simple multiplication.
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

print("Product:", num1 * num2)

Example:
7 × 8 → 56

9) Three Names Input


Theory / Concept:
split() separates input values.
name1, name2, name3 = input("Enter three names: ").split()

print(name1)
print(name2)
print(name3)

Example:
Ali Ahmed Sara

10) Percentage with Check


Theory / Concept:
Denominator check avoids division by zero.
num = float(input("Enter numerator: "))
den = float(input("Enter denominator: "))

if den == 0:
print("Denominator can't be zero")
else:
percentage = (num / den) * 100
print("Percentage: {:.2f}%".format(percentage))

Example:
75 / 100 → 75.00%

You might also like