Adi's PWP PT-1 Solution
Adi's PWP PT-1 Solution
• Script Mode
o Runs a complete Python program saved in a .py file.
o Used for developing full applications.
o Example:
o Run With:
B. Using enumerate()
enumerate returns both the index and value, making iteration easier.
Example:
Note:-Does not modify the original list but creates a new one.
3. List Repetition(* operator )
Repetition duplicates a list multiple times.
Example:
4. Finding Length(len())
The len() function returns the number of elements in a list.
2. or (Logical OR)
• Returns True if at least one of the conditions is True, otherwise returns False.
• Example:-
== Equal to 5 == 5 True
• Examples:-
• continue Statement:
o The continue statement skips the current iteration and moves to the next iteration of
the loop.
o Example: Using continue in a for Loop Output:
• Difference (- or difference())
o Returns elements in A but not in B.
12. List data types in Python. Explain any two with example
1. None Types:
• Null
2. Numeric Types:
• int (Integer)
3. Sequence Types:
• str (String)
• list (List)
• tuple (Tuple)
• range
4. Set Types:
• set (Mutable Set)
• frozenset (Immutable Set)
5. Mapping Type:
• dict (Dictionary)
6. Boolean Type:
• bool (Boolean: True or False)
• Example: Output:
• Example: Output:
13. Write a Python Program to accept values from user in a list and find the largest number
and smallest number in a list.
Code:- Output:-
14. Explain Membership and Assignment operators in python
• Python provides Membership and Assignment operators for checking values in sequences and
assigning values to variables, respectively.
• Membership Operators (in, not in) :-
Used to check if a value exists in a sequence (list, tuple, string, etc.).
Operators:
Example:
# a. Create a set
my_set = {1, 2, 3, 4, 5}
print("Created Set:", my_set)
# Output: Created Set: {1, 2, 3, 4, 5}
# b. Access set elements (using a loop, as sets are unordered)
print("Accessing Set Elements:")
for item in my_set:
print(item)
# Output (Order may vary):
#1
#2
#3
#4
#5
Example: Output:
• if-elif-else Statement
The if-elif-else statement is used when multiple conditions need to be checked. It works like
multiple if conditions, but only the first True condition is executed.
Syntax:
Example: Output:
17. Explain building blocks of Python
Python's building blocks are the fundamental components that define the language's structure and
functionality. Some key building blocks include:
1. Identifiers – Names given to variables, functions, and classes.
• Must start with a letter (A-Z or a-z) or an underscore (_), followed by letters, digits (0-9), or
underscores.
• Example: my_variable = 10
2. Keywords – Reserved words in Python that cannot be used as variable names.
• Examples: if, else, for, while, def, return, class, import, True, False.
3. Indentation – Python uses indentation instead of curly braces {} to define code blocks, making the
code more readable.
• Example:
5. Comments – Used to explain code and make it more understandable. They are ignored by the Python
interpreter.
• Single-line comment: # This is a comment
• Multi-line comment:
20. Write a Python program to find the factorial of a number provided by the user