QB Solved
QB Solved
Features in Python
In this section we will see what are the features of Python programming language:
2. Easy to code
Python is a high-level programming language. Python is very easy to learn the language as
compared to other languages like C, C#, Javascript, Java, etc. It is very easy to code in the
Python language and anybody can learn Python basics in a few hours or days. It is also a
developer-friendly language.
3. Easy to Read
As you will see, learning Python is quite simple. As was already established, Python’s syntax is
really straightforward. The code block is defined by the indentations rather than by semicolons
or brackets.
4. Object-Oriented Language
One of the key features of Python is Object-Oriented programming. Python supports
object-oriented language and concepts of classes, object encapsulation, etc.
6. High-Level Language
Python is a high-level language. When we write programs in Python, we do not need to
remember the system architecture, nor do we need to manage the memory.
7. Large Community Support
Python has gained popularity over the years. Our questions are constantly answered by the
enormous StackOverflow community. These websites have already provided answers to many
questions about Python, so Python users can consult them as needed.
8. Easy to Debug
Excellent information for mistake tracing. You will be able to quickly identify and correct the
majority of your program’s issues once you understand how to interpret Python’s error traces.
Simply by glancing at the code, you can determine what it is designed to perform.
******************************************************************************
****
Q.2What is problem ? List down steps in problem solving with suitable example
A problem is a situation or challenge that requires a solution. It arises when there is a gap
between the current state and the desired goal, and there is uncertainty about how to bridge that
gap.
Steps in Problem Solving
The problem-solving process involves systematic steps to analyze and resolve an issue
effectively. The main steps are:
******************************************************************************
*******
Q.What is modularization ? Explain top down design approach.
Modularization
Modularization is the process of dividing a large program into smaller, manageable, and
independent modules or functions. Each module performs a specific task and can be developed,
tested, and debugged independently before being integrated into the main program. This
approach improves code readability, reusability, maintainability, and efficiency.
******************************************************************************
*******
Q.Define Programming Paradigm?List programming Paradigm?Explain one in detail.
○ Procedural Programming
○ Object-Oriented Programming (OOP)
○ Parallel Programming
2. Declarative Paradigm
○ Functional Programming
○ Logic Programming
○ Database Query Programming (SQL)
3. Event-Driven Paradigm
# Swapping
temp = a
a=b
b = temp
print(f"After swapping: a = {a}, b = {b}")
******************************************************************************
*******
Q.Program to enter a number and then calculate the sum of its digits
# Initialize sum to 0
sum_digits = 0
Example of a Dictionary:
student = {
"name": "Alice",
"age": 22,
"course": "Computer Science"
}
print(student)
Output:
Output:
Output:
{'name': 'Alice', 'course': 'Computer Science', 'grade': 'A', 'city': 'New York', 'ID': 101}
removed_value = student.pop("course")
print(student)
print("Removed Value:", removed_value)
Output:
{'name': 'Alice', 'grade': 'A', 'city': 'New York', 'ID': 101}
Removed Value: Computer Science
******************************************************************************
******
Q.Explain Selection /Condition Statements in python
1. if Statement
The if statement checks a condition and executes a block of code only if the condition is True.
Syntax:
if condition:
# Code to execute if the condition is True
Example:
python
CopyEdit
age = 18
if age >= 18:
print("You are eligible to vote.")
Output:
Syntax:
if condition:
# Code to execute if condition is True
else:
# Code to execute if condition is False
Example:
age = 16
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
Output:
Syntax:
if condition1:
# Code if condition1 is True
elif condition2:
# Code if condition2 is True
else:
# Code if none of the conditions are True
Example:
marks = 85
Output:
Grade: B
******************************************************************************
*******
Q.Write a program to find whether entered no is Armstrong or not
if sum_of_digits == num:
print(f"{num} is an Armstrong number")
else:
print(f"{num} is not an Armstrong number")
******************************************************************************
*******
QWrite a program to find whether entered character is vowel or not
char = input("Enter a character: ") # Taking input from the user
1) break
The break statement is used to exit a loop prematurely when a certain condition is met. It stops
the loop entirely.
Example:
for num in range(1, 6):
if num == 3:
break # Exits the loop when num is 3
print(num)
Output:
1
2
2) continue
The continue statement is used to skip the rest of the current iteration and move to the next
iteration of the loop.
Example:
for num in range(1, 6):
if num == 3:
continue # Skips printing 3 and moves to the next iteration
print(num)
Output:
1
2
4
5
3) pass
The pass statement is a placeholder that does nothing. It is used when a statement is syntactically
required but no action is needed.
Example:
for num in range(1, 6):
if num == 3:
pass # Does nothing, loop continues normally
print(num)
Output:
1
2
3
4
5
4) range()
The range() function generates a sequence of numbers. It is commonly used in loops.
Output:
0
1
2
3
4
Output:
2
3
4
5
6
*****************************************************************************
Q.Explain following operations of list
1)creating a list 2)Display list 3)Appending 4)Accessing an elements
1) Creating a List
A list in Python can be created using square brackets [], with elements separated by commas.
Example:
2) Displaying a List
You can display a list using the print() function.
Example:
fruits = ["apple", "banana", "cherry"]
print(fruits) # Output: ['apple', 'banana', 'cherry']
3) Appending an Element
To add an element at the end of a list, use the append() method.
Example:
fruits.append("orange")
print(fruits) # Output: ['apple', 'banana', 'cherry', 'orange']
4) Accessing Elements
Elements in a list can be accessed using their index. Python uses zero-based indexing (i.e., the
first element is at index 0).
Example:
******************************************************************************
******
Q.Write a program to find if number is divisible by 7
# Taking user input
num = int(input("Enter a number: "))
# Checking divisibility
if num % 7 == 0:
print(f"{num} is divisible by 7.")
else:
print(f"{num} is not divisible by 7.")