python-lab-Content
python-lab-Content
Sign
Readability: Python uses a simple, clear, and easy-to-understand syntax, making it great
for beginners.
Interpreted Language: Python code is executed line-by-line, which means it doesn’t
need to be compiled before running.
Dynamically Typed: Variables don’t need to be declared with a type. Python determines
the type at runtime.
Extensive Libraries: Python has a vast collection of libraries and frameworks for tasks
such as web development (Django, Flask), data science (NumPy, pandas, matplotlib), and
AI/ML (TensorFlow, PyTorch).
Cross-Platform: Python can run on various platforms, including Windows, macOS, and
Linux.
Python is ideal for beginners due to its simplicity and wide application in various fields like web
development, scientific computing, automation, and more.
Python Installation
Python is available for all major operating systems (Windows, macOS, and Linux). Below is the
process for installing Python on different platforms.
1. Download Python:
o Go to the official Python website: https://www.python.org/downloads/
o Download the latest stable version for Windows.
2. Run the Installer:
o Open the downloaded .exe installer.
o Important: Check the box that says "Add Python to PATH". This ensures Python can be
run from the command line.
o Click on "Install Now" to begin the installation process.
o Wait until the installation is complete.
3. Verify Installation:
o Open the Command Prompt (search for "cmd" in the Start menu).
o Type the following command to check the Python version:
bash
Copy code
python --version
o If Python is installed correctly, you should see the Python version printed on the screen.
4. IDLE (Optional):
o Python comes with its own built-in Integrated Development and Learning Environment
(IDLE). You can open IDLE from the Start menu and begin writing Python code
immediately.
After installing Python, you may want to set up an environment to write and run Python code.
This can be done using a code editor or an IDE (Integrated Development Environment).
Visual Studio Code (VS Code): A lightweight, feature-rich code editor with Python support via
extensions.
PyCharm: A full-fledged IDE specifically designed for Python development.
Sublime Text: A fast, customizable code editor.
Jupyter Notebooks: Ideal for data science, machine learning, and interactive programming.
IDLE: Comes bundled with Python and is great for beginners
Conclusion
Python is an excellent language for beginners and experienced developers alike. It's easy to learn,
powerful, and versatile, making it a great choice for a wide range of applications. The installation
process is straightforward on all major operating systems, and once installed, you can start
coding and experimenting with Python right away!
TOPIC 2 - Write a program that check if a person is eligible to vote
Explanation:
1. Function check_voting_eligibility(age):
o This function takes an age as input and checks whether it is 18 or greater.
o If the age is 18 or older, the function returns a message saying the person is eligible to
vote.
o If the age is less than 18, the function returns a message saying the person is not
eligible.
2. Input:
o The program prompts the user to enter their age using input(), and the input is
converted to an integer with int().
3. Output:
o The program calls the check_voting_eligibility() function and prints the result
based on the returned message.
TOPIC – 3 Write a program that takes a numeric grade (0-100) and prints the corresponding
letter grade
Explanation:
1. Function convert_to_letter_grade(grade):
o This function takes a numeric grade as input and converts it into a letter grade.
o The grading scale is as follows:
90–100 → A
80–89 → B
70–79 → C
60–69 → D
0–59 → F
o If the grade is outside the valid range (0–100), the function will return an error message.
2. Input:
o The program prompts the user to input a numeric grade using input(), and the input is
then converted to a floating-point number using float().
3. Output:
o The program calls convert_to_letter_grade() to determine the letter grade and
prints the result.
TOPIC – 4 Write a program that takes two numbers and prints which one is larger or if they are
equal.
Explanation:
Explanation:
1. Menu Display:
o The program displays a menu offering two options:
1 for Celsius to Fahrenheit conversion.
2 for Fahrenheit to Celsius conversion.
2. User Input:
o The program then prompts the user to choose one of the options.
o Based on the user's choice, it asks for the corresponding temperature value.
3. Conversion Logic:
o If the user selects 1 (Celsius to Fahrenheit):
The program takes a Celsius temperature, applies the conversion formula, and
prints the result.
The conversion formula: Fahrenheit=(Celsius×95)+32\text{Fahrenheit} = \left(
\text{Celsius} \times \frac{9}{5} \right) + 32Fahrenheit=(Celsius×59)+32
o If the user selects 2 (Fahrenheit to Celsius):
The program takes a Fahrenheit temperature, applies the conversion formula,
and prints the result.
The conversion formula: Celsius=(Fahrenheit−32)×59\text{Celsius} = \left(
\text{Fahrenheit} - 32 \right) \times \frac{5}{9}Celsius=(Fahrenheit−32)×95
4. Error Handling:
o If the user enters anything other than 1 or 2, the program prints an error message:
"Invalid choice. Please select either 1 or 2."
TOPIC – 6 Write a Python program to find GCD of two numbers
Explanation:
Euclidean Algorithm:
This algorithm works on the principle that the GCD of two numbers a and b (where a > b) is the same
as gcd(b, a % b). The process continues until b becomes zero, and a will hold the GCD
TOPIC – 7 write a Python Program to find the maximum from a list of numbers.
Explanation:
Predefined List: The list numbers = [10, 20, 50, 30, 40] is hardcoded, meaning it is
predefined within the program.
Finding the Maximum: The program assumes the first number in the list (numbers[0]) is the
maximum and then iterates through the rest of the list to check if there is a larger number.
Output: The program will print the largest number in the list.
Topic- 8 write a Python Program to perform Linear Search
The linear search algorithm is defined as a sequential search algorithm that starts at one
end and goes through each element of a list until the desired element is found;
otherwise, the search continues till the end of the dataset
TOPIC – 9 write a Python Program to perform binary search
Binary search is a search algorithm used to find the position of a target value within
a sorted array. It works by repeatedly dividing the search interval in half until the target value is
found or the interval is empty.
TOPIC – 10 write a Python Program to perform selection sort
TOPIC 11 - write a Python program to find first n prime numbers
TOPIC 12 - write a Python Program to perform Merge sort
TOPIC – 13
Write a function to find the first negative number in a list of integers. Use break to stop
the search as soon as you find a negative number.
Explanation:
1. Function first_negative_number(numbers):
o The function iterates over each number in the list numbers.
o If a negative number is found (num < 0), the function returns that number and stops
further searching using the return statement (this is equivalent to break).
o If the loop completes without finding a negative number, the function returns None to
indicate no negative number was found.
2. Test the Function:
o In the example list [10, 20, 30, -5, 40, -10, 50], the function will return -5
because it's the first negative number in the list.
3. Output:
o If a negative number is found, it is printed. If no negative number is found, it prints a
message saying there is no negative number in the list.
TOPIC 14 - To write a Python program to multiply matrices
1. Check Matrix Dimensions: Ensure that the number of columns in the first matrix is
equal to the number of rows in the second matrix. If this condition is met, matrix
multiplication is possible.
2. Matrix Multiplication: For each element in the result matrix, compute the dot product of
the corresponding row from the first matrix and column from the second matrix.
TOPIC 15 - Print the Factorial of a Number
Explanation:
1. Iterative Approach:
o We initialize result to 1, then multiply result by each number from 1 to n using a
loop.
o This is a straightforward approach for calculating the factorial.
2. Recursive Approach:
o The recursive version of the factorial function calls itself with n-1 until it reaches the
base case (n == 0 or n == 1), where it returns 1.
o Recursion is often a more elegant solution, but can lead to stack overflow if the number
is too large (due to too many recursive calls).