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

Python Assignment Solution-1

The document provides Python assignment solutions covering key concepts such as mutable vs immutable objects, types of operators, operator precedence, loops, string and list methods, and practical programming tasks like grade calculation and factorial computation. It includes code examples for each topic to illustrate the concepts effectively. Additionally, it demonstrates list comprehension for generating even numbers and calculating statistics from user input.

Uploaded by

anshgautam026
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)
6 views3 pages

Python Assignment Solution-1

The document provides Python assignment solutions covering key concepts such as mutable vs immutable objects, types of operators, operator precedence, loops, string and list methods, and practical programming tasks like grade calculation and factorial computation. It includes code examples for each topic to illustrate the concepts effectively. Additionally, it demonstrates list comprehension for generating even numbers and calculating statistics from user input.

Uploaded by

anshgautam026
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 Solutions

1. Mutable vs Immutable Objects


Mutable objects can be changed after creation (e.g., list, dictionary). Immutable objects cannot be
changed after creation (e.g., int, string, tuple).
# Mutable example
my_list = [1,2,3]
my_list[0] = 10
print(my_list)

# Immutable example
x = 5
x = x + 2
print(x)

2. Types of Operators in Python


Python supports multiple operators including arithmetic, comparison, assignment, and bitwise
operators.
a = 10
b = 3

# Arithmetic
print(a + b)
print(a - b)
print(a * b)
print(a / b)

# Comparison
print(a > b)
print(a == b)

# Assignment
x = 5
x += 2
print(x)

# Bitwise
print(a & b)
print(a | b)

3. Operator Precedence and Associativity


Operator precedence determines which operation happens first. Associativity determines the order
when operators have the same precedence.
result = 10 + 5 * 2
print(result)

result = 10 - 5 - 2
print(result)

4. For Loop vs While Loop


For loop is used when number of iterations is known. While loop is used when a condition controls
the loop.
# for loop
for i in range(5):
print(i)

# while loop
i = 0
while i < 5:
print(i)
i += 1

5. String Methods, List Methods, and List Comprehension


String methods manipulate text. List methods manipulate list elements. List comprehension creates
lists in a concise way.
text = "python programming"
print([Link]())
print([Link]())

numbers = [1,2,3]
[Link](4)

squares = [x*x for x in range(1,6)]


print(squares)

6. Program to Calculate Grade


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

if marks >= 90:


print("Grade A")
elif marks >= 75:
print("Grade B")
elif marks >= 60:
print("Grade C")
elif marks >= 40:
print("Grade D")
else:
print("Fail")

7. Factorial Using While Loop


num = int(input("Enter a number: "))

fact = 1
i = 1

while i <= num:


fact = fact * i
i += 1

print("Factorial =", fact)

8. Reverse String and Count Vowels


text = input("Enter a string: ")
reverse = text[::-1]

vowels = "aeiouAEIOU"
count = 0

for ch in text:
if ch in vowels:
count += 1

print("Reversed:", reverse)
print("Vowels:", count)

9. List Program (Sum, Average, Max, Min)


numbers = []

for i in range(5):
num = int(input("Enter number: "))
[Link](num)

print("Sum =", sum(numbers))


print("Average =", sum(numbers)/len(numbers))
print("Maximum =", max(numbers))
print("Minimum =", min(numbers))

10. Even Numbers Using List Comprehension


even_numbers = [x for x in range(1,21) if x % 2 == 0]
print(even_numbers)

You might also like