CH #3 Solved Exercise
CH #3 Solved Exercise
Mobile Apps: Creating and maintaining apps for smartphones and tablets.
2. Write the code to take input a number and print its mathematical table on screen from 1 to 10.
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: "))
5. Differentiate between global and local variable with the help of suitable example
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.
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
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]
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}")
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
# 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))