0% found this document useful (0 votes)
11 views

python assignment

Uploaded by

iamsam100806
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)
11 views

python assignment

Uploaded by

iamsam100806
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
You are on page 1/ 12

1. What are identifiers in Python?

Identifiers are names used to identify variables, functions, classes, or other objects in Python.
An identifier must begin with a letter (a-z, A-Z) or an underscore (_), followed by letters, digits
(0-9), or underscores. Identifiers are case-sensitive and cannot be Python reserved keywords
(like if, else, class, etc.).

Validity of identifiers:
- xyz: Valid (it starts with a letter, and contains only letters).
- 123: Invalid (it starts with a digit).
- _pqr12: Valid (it starts with an underscore and contains letters and digits).
- #Axy: Invalid (it starts with a special character #).

2. Write the output of the following Python expression:


print((4>9) and (5!=1) or (5<9))

*Evaluation:*
- (4 > 9) is *False*.
- (5 != 1) is *True*.
- (5 < 9) is *True*.

The expression evaluates as:


python
(False and True) or True

Which simplifies to:


False or True # evaluates to True
*Output:*

True

*For the other expressions:*


a) 6 ** 2 + 9 * 4 // 5 - 11
6 ** 2 = 36
9 * 4 = 36
36 // 5 = 7
36 + 7 - 11 = 32

*Output:*
32

b) 3 > 9 and 19 > 17 or not 10 > 13


3 > 9 is False
19 > 17 is True
10 > 13 is False, so not False is True

So, the expression becomes:


False and True or True

This simplifies to:


False or True # evaluates to True

*Output:*
True

3. Draw the flowchart to explain any one of the following:


a) nested if conditional statement
b) nested while conditional statement
c) nested for conditional statement

*Flowchart for Nested if Conditional Statement*


*Flowchart for nested while conditional statement*
*Flowchart for nested for conditional statement*

4. *Program to check if a character is uppercase, lowercase, digit, or special


character.*

char = input("Enter a character: ")

if char.isupper():
print("The character is an uppercase letter.")
elif char.islower():
print("The character is a lowercase letter.")
elif char.isdigit():
print("The character is a digit.")
else:
print("The character is a special character.")

*OR:*

*Mutable and Immutable objects in Python:*


- *Mutable* objects can be changed after they are created (e.g., lists, dictionaries, sets).
- *Immutable* objects cannot be changed after they are created (e.g., integers, strings, tuples).

5. *WAP to check whether a given rectangle is square or not.*


length = float(input("Enter length of the rectangle: "))
breadth = float(input("Enter breadth of the rectangle: "))

if length == breadth:
print("The rectangle is a square.")
else:
print("The rectangle is not a square.")

*OR:*

*Menu-driven program for area calculation:*


print("Menu:")
print("1. Area of Square")
print("2. Area of Rectangle")
print("3. Area of Circle")

choice = int(input("Enter your choice: "))

if choice == 1:
side = float(input("Enter the side of the square: "))
area = side ** 2
print("Area of square:", area)

elif choice == 2:
length = float(input("Enter the length of the rectangle: "))
breadth = float(input("Enter the breadth of the rectangle: "))
area = length * breadth
print("Area of rectangle:", area)

elif choice == 3:
radius = float(input("Enter the radius of the circle: "))
area = 3.14 * radius ** 2
print("Area of circle:", area)

else:
print("Invalid choice!")

6. *What will be the output of the following code?*


str = "Python Programming"
new_str = ""
for i in range(len(str)):
if i != 3 and i != 10:
new_str = new_str + str[i]
print(new_str)

*Explanation:*
- The loop iterates over each index of the string str.
- Characters at index 3 (h) and index 10 (g) are skipped.

*Output:*

Pyton Proramming

7. *Write the output of the following codes:*


for i in range(3):
for j in range(3):
if j == i:
break
print(i, j)

*Explanation:*
- This nested loop prints i, j values before the break statement is executed.
- When j == i, the break exits the inner loop.

*Output:*
10
20
21

8. *Python program to find sum and average of a list of marks input by the
user.*

marks = input("Enter marks separated by space: ").split()


marks = [int(mark) for mark in marks]
sum_marks = sum(marks)
avg_marks = sum_marks / len(marks)

print("Sum:", sum_marks)
print("Average:", avg_marks)

*OR:*

*Expression to return 'y' in the nested list:*


List1 = [100, [32, 200, [3, 'python', 2]], 'prog']
print(List1[1][2][1][1])

# This will print 'y'

9. *What is the error in the following code?*


dict1 = {"Amit": 100, "Ajay": 40}
dict2 = {"Amit": 400, "Ajay": 40}
print(dict1 > dict2)

*Error Explanation:*
- The > operator is not supported for dictionaries in Python.
- You cannot compare dictionaries directly using comparison operators like > or <.

*Output:*

TypeError: '>' not supported between instances of 'dict' and 'dict'

*OR:*

*Output for tuple code:*


tuple1 = (18,)
print(tuple1 * 2)

*Output:*

(18, 18)

10. *Output for the following code:*


def Fun(x, y=50):
print("x:", x)
print("y:", y)
Fun(10)

*Explanation:*
- The function is called with x=10 and y takes the default value 50.

*Output:*

x: 10
y: 50

*OR:*

def z(x, y):


while(y):
x, y = y, x % y
print(x, y)
return abs(x)

print(z(60, 48))

*Explanation:*
- The function z() calculates the greatest common divisor (GCD) using the Euclidean algorithm.

*Output:*
12 0
12

You might also like