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

CH #3 Solved Exercise

Uploaded by

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

CH #3 Solved Exercise

Uploaded by

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

FAZAIA INTER COLLEGE KALLAR KAHAR

Unit #3 Solved Exercise

Give short answer of the following Short Response Questions (SRQs).

1. What are the applications of computer programming in daily life?

Mobile Apps: Creating and maintaining apps for smartphones and tablets.

 Websites: Developing and managing online content and services.


 Automation: Streamlining repetitive tasks and processes.
 Gaming: Designing and building video games.
 Communication: Enabling email, messaging, and video calls.
 Finance: Managing online banking, investments, and budgeting.
 Healthcare: Supporting medical records, diagnostics, and treatment tools.
 Education: Creating e-learning platforms and educational software.
 Transportation: Enhancing navigation systems and ride-sharing apps.
 Entertainment: Powering streaming services and digital media players.
 Weather Forecasting: Analyzing data for accurate weather predictions.
 Smart Devices: Controlling and automating home appliances.

2. Write the code to take input a number and print its mathematical table on screen from 1 to 10.

n = int(input("Enter a number: ")) # Get input from the user

for i in range(1, 11): # Print the multiplication table from 1 to 10


print("{} x {} = {}".format(number, i, number * i))

3. Take odd number as input, check if it is odd, otherwise ask the user to re-enter an odd number?
while True:
number = int(input("Enter an odd number: "))

if (number % 2 != 0): # Check if the number is odd


print("You entered an odd number:", number)
break
else:
print("That's not an odd number. Please enter an odd number.")

Understanding while True Loop


 while True: This statement creates an infinite loop. The while True loop is used because we don’t
know how many times the user will enter an even number before entering an odd one. It keeps the
program running until the user meets the condition (entering an odd number).
 Breaking the Loop: Inside this loop, you can use break to exit the loop when a certain condition is
met. In this case, when the user enters an odd number, the loop stops.
4. Write down the main examples of python base applications.
Web Development: Python is used with frameworks like Django and Flask to build dynamic websites
and web applications.
Data Analysis: Python helps in processing and analyzing large datasets using libraries such as Pandas
and NumPy.
Machine Learning: Python facilitates the development and training of machine learning models with
libraries like TensorFlow and scikit-learn.
Desktop Applications: Python creates desktop applications with graphical user interfaces (GUIs) using
frameworks like Tkinter and PyQt.
Game Development: Python is used in game development for creating 2D games and simulations
with libraries like Pygame.
Web Scraping: Python extracts data from websites using libraries like BeautifulSoup and Scrapy.
Education: Python is used to teach programming concepts, computer science, and data analysis due
to its simplicity and readability.

5. Differentiate between global and local variable with the help of suitable example

Local Variable global Variable

1.Local variables are declared within a 1.Global variable is declared outside any
function. function.
2.It can be used only in function in which 2.It can be used in all functions.
they are declared.
3.It is created when the program starts
3.It is created when the control enters the execution.
function in which it is declared.

4.It destroyed when the control leaves the 4.It destroyed when the program
function. terminates.
5.It is used(scope) when the values are to 5.It is used (scope) when values are to be
be used within a function. shared among different functions.

Example of local and global variable


global_var = 10 # Global variable

def abc():
local_var = 5 # Local variable
print("Inside function:")
print("Global variable:", global_var) # Accessing global variable
print("Local variable:", local_var) # Accessing local variable

abc ()
print("Outside function:")
print("Global variable:", global_var) # Accessible outside the function

Give long answer of the following Extended Response Questions (ERQs).

1. Explain the application of python in different business and technical domains.


1. Business Domains

 Finance and Trading:


o Application: Python is used for quantitative analysis, algorithmic trading, risk
management, and financial forecasting.
o Tools: Libraries like Pandas for data analysis, NumPy for numerical computations, and
libraries like QuantLib for quantitative finance.
 E-commerce:
o Application: Python helps in developing e-commerce platforms, managing inventory,
processing payments, and analyzing customer data.
o Tools: Django and Flask for web development, and libraries for data analysis and machine
learning.
 Customer Relationship Management (CRM):
o Application: Python automates tasks such as managing customer data, sending
personalized communications, and analyzing customer behavior.
o Tools: CRM platforms can integrate Python scripts for automating workflows and data
processing.
 Marketing and Analytics:
o Application: Python is used for analyzing marketing data, tracking campaign performance,
and customer segmentation.
o Tools: Libraries like Matplotlib and Seaborn for data visualization, and Scikit-learn for
predictive modeling.

2. Technical Domains

 Web Development:
o Application: Python is used to build dynamic websites and web applications with features
like user authentication, data management, and API integration.
o Tools: Frameworks like Django and Flask.
 Data Science and Analytics:
o Application: Python is central to data analysis, data visualization, and predictive modeling.
o Tools: Libraries such as Pandas for data manipulation, NumPy for numerical computations,
Matplotlib and Seaborn for visualization, and Scikit-learn for machine learning.
 Machine Learning and Artificial Intelligence:
o Application: Python is widely used for developing machine learning models, deep learning
algorithms, and AI applications.
o Tools: Libraries like TensorFlow, Keras, and PyTorch for building and training models.
 Automation and Scripting:
o Application: Python automates repetitive tasks such as file manipulation, data extraction,
and process automation.
o Tools: Libraries like Selenium for web automation, and modules like os and shutil for file
operations.
 Scientific Computing:
o Application: Python is used for complex scientific computations, simulations, and
modeling.
o Tools: Libraries like SciPy for scientific calculations and SymPy for symbolic mathematics.
 Network Programming:
o Application: Python facilitates the creation of network applications, such as servers and
clients, and handling network protocols.
o Tools: The socket library for network communication, and frameworks like Twisted for
asynchronous networking.
 Game Development:
o Application: Python is used to develop 2D games and prototypes.
o Tools: Libraries like Pygame for game development.

2. What are the basic functions that list provides. Elaborate each of them of them with examples.
Lists in Python are one of the most commonly used data structures, and they come with a variety of
built-in- functions that make them powerful and flexible. Below are some of the basic functions
(methods) that lists provide, along with explanations and examples for each.
1. append(x)
 Description: Adds an item x to the end of the list.
 Example:

my_list = [1, 2, 3]
my_list.append(4)
print(my_list) # Output: [1, 2, 3, 4

2. extend(iterable)
 Description: Extends the list by appending all the items from the iterable (e.g., another list).
 Example:

my_list = [1, 2, 3]
my_list.extend([4, 5])
print(my_list) # Output: [1, 2, 3, 4, 5]

3. remove(x)
 Description: Removes the first occurrence of the item x. Raises a ValueError if x is not found.
 Example:
my_list = [1, 2, 3, 2]
my_list.remove(2) # Removes the first occurrence of 2
print(my_list) # Output: [1, 3, 2]

4. pop([index])
 Description: Removes and returns the item at the given index. If no index is specified,
removes and returns the last item.
 Example:
my_list = [1, 2, 3]
last_item = my_list.pop() # Removes and returns the last item
print(last_item) # Output: 3
print(my_list) # Output: [1, 2]

5. count(x)
 Description: Returns the number of occurrences of the item x in the list.
 Example:
my_list = [1, 2, 3, 2, 2]
count_of_twos = my_list.count(2) # Counts occurrences of 2
print(count_of_twos) # Output: 3
6 . copy()
 Description: Returns a shallow copy of the list.
 Example:
my_list = [1, 2, 3]
new_list = my_list.copy()
print(new_list) # Output: [1, 2, 3]
7. insert(index, x)
 Description: Inserts an item x at the specified index.
 Example:
my_list = [1, 2, 3]
my_list .insert(1, 'a') # Inserts 'a' at index 1
print(my_list) # Output: [1, 'a', 2, 3]
8. sort([key=None, reverse=False])
 Description: Sorts the items of the list in ascending order by default. Can sort in descending
order if reverse=True is specified.
 Example:
my_list = [3, 1, 2]
my_list.sort() # Sorts in ascending order
print(my_list) # Output: [1, 2, 3]

my_list.sort(reverse=True) # Sorts in descending order


print(my_list) # Output: [3, 2, 1]

4. How to locate and select Debugger in IDEL? Write steps by taking an example into consideration.
In IDLE (Integrated Development and Learning Environment) for Python, you can use the debugger to
step through code, inspect variables, and troubleshoot issues. Here’s how to locate and use the
debugger in IDLE with a step-by-step.
example:
def additon(a, b):
result = a + b
return result
sum = addition(5, 3)
print(f"The sum of two numbers = {sum}")

Steps to Locate and Use the Debugger in IDLE

1. Open IDLE:
o Open IDLE and create a new file. Copy and paste the above code into the file and save it
with a .py extension.
2. Set a Breakpoint:
o In IDLE, right-click on the line sum = addition ( 5 , 3 ) and select "Set Breakpoint". A red
dot should appear next to the line number, indicating the breakpoint is set.
3. Start the Debugger:
o Go to the "Debug" menu at the top and select "Debugger". This will open the Debug
Control window.
o Now, run the program by pressing F5 or selecting "Run Module" from the "Run" menu.
4. Observe the Breakpoint:
o The program will start executing and then pause at the breakpoint you set on the
sum= addition ( 5 , 3 ) line. At this point, the Debug Control window will show the current
execution state.
5. Step Through the Code:
o In the Debug Control window, click the "Step" button to move to the next line of code.
The program will now step into the addition function.
o Continue stepping through the code using the "Step" button to see how the addition
function work.
6. Inspect Variables:
o While stepping through the code, you can hover over variables like sum , result in the
code editor to see their current values.
o Alternatively, you can see the variable values in the Debug Control window under the
"Locals" section.
7. Continue Execution:
o After you understand how the code is executing, you can click "Go" in the Debug Control
window to let the program run to completion.
8. Result:
o Once the program finishes running, the result will be printed in the Python Shell window:
The sum of two numbers = 8.

5.Write a code to print the multiplication of first 10 odd numbers and first 10 even numbers and
find the difference of the two.[ Hints: use function]
def multi_odd_num(n):
product = 1
count = 0
num = 1
while count < n:
product = product * num
count += 1
num += 2 # Move to the next odd number
return product

def multi_even_num(n):
product = 1
count = 0
num = 2
while count < n:
product *= num
count += 1
num += 2 # Move to the next even number
return product

n = 10 # Number of odd and even numbers to multiply


product_odds = multiply_odd_numbers(n) # function call statement
product_evens = multiply_even_numbers(n) # function call statement

# Find the difference


difference = product_odds - product_evens

# Print results
print ("Product of first { } odd numbers: { }".format(n, product_odds))
print ("Product of first { } even numbers: { }".format(n, product_evens))
print ("Difference between the two products: { }".format(difference))

You might also like