(X) Unit 4 Programming in Python

1. Choose the correct answer. i. Which statement is used to display output in Python? a) input ( ) b) print ( ) c) display ( ) d) output ( ) ii. Which of the following is NOT a valid Python data type mentioned in the text? a) int b) float c) character d) bool iii. What type of values does the Boolean data type hold? a) Whole numbers b) Decimal numbers c) Text d) True or False iv. Which of the following is an example of a relational operator? a) + b) * c) == d) and v. Which logical operator returns True if both conditions are true? a) or b) not c) and d) != vi. What is the purpose of the if statement in Python? a) To repeat a block of code. b) To define a function. c) To execute a block of code only if a condition is true. d) To store multiple items in a single variable. vii. Which conditional statement allows you to check multiple conditions in sequence? a) if b) if-else c) nested if d) if-elif-else viii. What is the term for repeating a block of code multiple times? a) Selection b) Iteration c) Condition d) Assignment ix. Which type of loop is used when you know the number of times you want to repeat a block of code? a) while loop c) if loop b) for loop d) nested loop x. Which data structure is used to store multiple items in a single variable in Python, as shown in the example ["Computer", "Science", 20, True]? a) String b) Tuple c) List d) Dictionary 2. Write short answers to these questions. a) What is the primary function of the input() function in Python? Ans: The input() function is used to take input from the user through the keyboard. b) Provide an example of a string literal in Python. Answer: "Hello, World!" or 'Python' are examples of string literals. c) What is an identifier in the context of Python programming? Ans: An identifier is the name used to identify variables, functions, classes, or other objects in a Python program. d) Explain the difference between the division operator (/) and the floor division operator (//) in Python. Ans: The / operator returns the division result as a decimal (float), while the // operator returns the quotient without the decimal part (integer). Example: 10 / 3 = 3.3333 10 // 3 = 3 e) Define the concept of data types in Python. Ans: Data types specify the type of value a variable can store, such as integers, floats, strings, and Boolean values. f) What is the purpose of relational operators in Python? Give one example. Ans: Relational operators are used to compare two values and return either True or False. Example: 10 > 5 returns True. g) Describe the functionality of the else block in an if-else statement. Ans: The else block executes when the condition in the if statement is False. h) What is a nested if statement? Ans: A nested if statement is an if statement placed inside another if or else block to check additional conditions. i) When would you typically use a for loop instead of a while loop? Ans: A for loop is used when the number of iterations is known in advance or when looping through a sequence like a list or string. j) What is the role of the range() function often used with for loops? Answer: The range() function generates a sequence of numbers, which is commonly used to control the number of repetitions in a for loop. k) Define a Python list and provide a simple example. Ans: A Python list is an ordered collection of multiple items stored in a single variable. Example: fruits = ["Apple", "Banana", "Mango"] l) Discuss the concept of iteration in programming. Ans: Iteration is the process of repeating a block of code multiple times until a condition is met or all items in a sequence have been processed.