Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Python Q11 to Q20

Uploaded by

mehularora555
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Python Q11 to Q20

Uploaded by

mehularora555
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Ch-5 Introduction To Python

Q11. Explain the concept and significance of control statements in Python.


 Control statements in Python determine the flow of execution in a program.
 They allow the program to make decisions, repeat actions, or terminate the loop early.
The three main types of control statements are:

 Conditional Statements (if, elif, else): Direct the program to execute specific code blocks based on conditions.
 Loops (for, while): Repeat a block of code multiple times.
 Loop Control Statements (break, continue, pass): Manage the behavior of loops.

Significance:
 Control statements enable dynamic decision-making and iteration, making Python programs flexible and efficient for
various use cases like data processing, automation, and algorithm implementation.

Q12. Differentiate between for loop and while loop.


Aspect For Loop While Loop
Usage Iterates over a sequence (e.g., list, range). Repeats based on a condition.
Iteration Predetermined: Iterates a fixed number of Indeterminate: Continues until the condition
times. fails.
Syntax for i in range(5): while i < 5:
Best Use Case When working with known data sets. When condition-based repetition is needed.

Q13. Elaborate on the concept and usage of looping control statements in Python.
Looping control statements modify the execution flow of loops.
They enhance control over how iterations are performed:
 break: Terminates the loop prematurely. Example: Exiting a loop when a condition is met.
 continue: Skips the current iteration and moves to the next. Example: Ignoring specific values in a sequence.
 pass: Placeholder that does nothing. Example: Used for future code or when no action is required in a block.
Usage: These statements are crucial for handling edge cases, improving performance, or simplifying logic within loops.

Q14. What are Lists in Python, and how are they useful?
Lists are ordered, mutable collections of elements, defined using square brackets ([]). They can hold elements of any data
type and allow duplicate values. Example: my_list = [1, "hello", 3.5].
Usefulness:
 Store multiple data items in one variable.
 Allow dynamic resizing.
 Provide versatile methods for data manipulation like sorting, slicing, and appending.
 Facilitate easy iteration and searching.
Q15. Explain how to access elements in a list in Python using indexing.
Elements in a Python list can be accessed using indexing, where each element is assigned a positional index starting from 0:

Positive Indexing: Access elements from the beginning. Example: my_list[0] gives the first element.
Negative Indexing: Access elements from the end. Example: my_list[-1] gives the last element.
Indexes can also be used with slicing to access a range of elements.

Q16. Differentiate between Lists and Tuples.


Aspect Lists Tuples
Mutability Mutable (elements can be modified). Immutable (elements cannot be modified).
Syntax Defined using []. Defined using ().
Performance Slower due to dynamic resizing. Faster due to immutability.
Use Case For data that changes frequently. For fixed data like coordinates or configs.
Q18. Describe how to add elements to a list in Python using appropriate methods.
Python provides several methods to add elements to a list:

append(element): Adds a single element to the end. Example: my_list.append(5)


extend(iterable): Adds all elements of an iterable (e.g., another list). Example: my_list.extend([6, 7])
insert(index, element): Adds an element at a specific position.Example: my_list.insert(1, "hello")

Q19. Explain the process of removing elements from a list in Python using methods.
Python offers multiple methods for element removal:

remove(value): Removes the first occurrence of a value. Example: my_list.remove(3)


pop(index): Removes and returns the element at a specific index (default is the last). Example: my_list.pop(2)
del keyword: Deletes an element by index or slices. Example: del my_list[1]
clear(): Removes all elements, leaving an empty list. Example: my_list.clear()

Q20. Discuss the purpose and functionality of the append() method in Python.
The append() method adds a single element to the end of a list. It modifies the list in place and does not return any value.
Example:
my_list = [1, 2]
my_list.append(3)
print(my_list) # Output: [1, 2, 3]
Purpose: Useful for adding individual elements without creating a new list or changing the structure.

Q21. Describe how the extend() method works in Python for list manipulation.
The extend() method adds all elements from an iterable (e.g., another list, tuple, or string) to the end of t he current list. It
modifies the original list in place.
Example:
my_list = [1, 2]
my_list.extend([3, 4])
print(my_list) # Output: [1, 2, 3, 4]
Purpose: Useful for concatenating lists or adding multiple elements at once. It differs from append(), which adds the iterabl e
itself as a single element.

Python Class work Practical Questions: Page( 162-163)


#Ans-1
age=int(input("Enter your age: "))
if age>=0 and age<=12:
print("Child")
elif age>12 and age <=17:
print("Teen")
elif age>17 :
print("Adult")
else:
print("Invalid input")
###################################################
#Ans-2
n=int(input("Enter any number: "))
if n%2==0:
print("Even Number")
else:
print("Odd Number")
###############################################
Ans3:
n=int(input("Enter any number: "))
for i in range(1,11):
print(n,"x",i,"=",n*i)
###############################################
Ans 4:
m=int(input("Enter your marks out of 100: "))
if m>=90 and m<=100:
print("A")
elif m>=80 and m<90:
print("B")
elif m>=70 and m<80:
print("C")
elif m>=60 and m<70:
print("D")
elif m>=50 and m<60:
print("E")
elif m<50:
print("Fail")
else:
print("Invalid Input")
###############################################
Ans 5:
n=int(input("Enter the number of rows for the triangle: "))
for i in range(n):
print()
for j in range(i+1):
print("*",end=" ")
################################################
#Ans 6:
#6-1
col=["Pink","Yellow","Green","Black","Red"]
#6-2
print(col[2])
#6-3
print(col[-1])
#6-4
col.append("Brown")
#6-5
col[1]="Blue"
#6-6
col.pop(3)
#6-7
col2=["White","light Blue","light Green","Lemon Yellow"]
#6-8
col.extend(col2)
#6-9
len(col)

You might also like