Python_Practice_Programs (1)
Python_Practice_Programs (1)
1. Introduction to Python
• Python variables
• Python basic operators
• Understanding Python blocks
• Python data types
o Declaring and using numeric data types: int, float, etc.
S.no. Program
1 Declare a variable age and assign the value 25. Print the variable and its data type.
2 Swap the values of two variables x = 10 and y = 20 without using a third variable.
3 Declare a variable var and assign the value 10 to it. Later change the value of var to 10.5 and
then to "Hello". Print the variable and its type after each assignment.
4 Assign the same value 5 to three variables a, b, and c. Print them.
5 Assign the values 1, 2, 3 to variables x, y, and z in a single line and print their sum.
6 Write a program to input two numbers and print their sum, difference, product, and division.
7 If x = 20, y = 3, compute and print x // y, x % y, and x ** y.
8 Write a program that checks if a number is both greater than 10 and less than 20 using logical
operators.
9 Given x = 5 and y = 10, use the and, or, and not operators to compare their values and print
the results.
10 Write a Python program that takes input from the user for their name, age, and weight (as
float). Then, print a formatted string that says, "Your name is namenamename, age is
ageageage and weight is weightweightweight kg."
S.no. Program
1 Write a program to check if a number is positive, negative, or zero. Use proper indentation to
define blocks for if, elif, and else.
2 Input the user's age and print whether they are eligible to vote (18 or older) or not.
3 Write a program to input a student's score and print the grade based on the following rules:
• 90 and above: A
• 80-89: B
• 70-79: C
• 60-69: D
• Below 60: F
4 Print the first 5 multiples of a given number using a for loop.
5 Write a program to print the squares of numbers from 1 to 10 using a for loop with range().
6 Given a string name = "Computer Science and Engineering", count and print the number of
vowels in the string using a for loop.
7 Write a program to keep asking the user for a number and find the sum of all numbers entered.
Stop when the user inputs 0.
8 Write a program that iterates through numbers 1 to 10 using a for loop and prints each
number, but use the pass statement for numbers divisible by 3.
9 Modify the program to print numbers between 1 and 20 that are not multiples of 3, using
continue.
10 Write a Python program where the user has to guess a randomly generated number between 1
and 100. Give feedback on whether the guess is too high or too low, and let the user keep
guessing until they find the correct number.
4. Python Functions
• Organizing Python codes using functions
S.no. Program
1 Write a Python function fibonacci(n) that returns the nth Fibonacci number using recursion.
Test the function with different values of n.
2 Write a lambda function that takes two numbers as input and returns their sum.
3 Write a Python program that demonstrates the difference between a local and a global
variable. Declare a global variable x = 10, then modify it locally inside a function and print
the results before and after the function call.
4 Write a Python program to create a simple calculator that can add, subtract, multiply, and
divide using functions. Each operation should be a separate function.
5 Write a function is_prime(n) that checks if a number is prime. Use this function inside a
program to check if a given number is prime or not.
6 Write a function is_palindrome(s) that checks if a given string s is a palindrome (reads the
same forward and backward). Use this function inside a program to check different strings.
7 Write a Python function find_largest(lst) that takes a list of numbers as input and returns the
largest number.
8 Write a function celsius_to_fahrenheit(celsius) that converts Celsius to Fahrenheit and another
function fahrenheit_to_celsius(fahrenheit) that converts Fahrenheit to Celsius. Use these
functions in a program to perform the conversions.
9 Write a function count_words(sentence) that takes a sentence as input and returns the number
of words in it.
10 Write a function validate_password(password) that checks if a password meets the following
criteria:
• At least 8 characters long
• Contains both uppercase and lowercase characters
• Contains at least one number Use this function inside a program to validate user-
entered passwords.
6. Python Packages
• Using built-in functions of packages:
o matplotlib
o numpy
o pandas
S.no. Program
1 Plot a graph of y = x^2 for x values from 0 to 10. Customize the graph with the following
features:
• Set the title as "Quadratic Function"
• Label the x-axis as "x" and the y-axis as "y"
• Change the line color to red and line style to dashed.
2 Given the list of students' names ["Alice", "Bob", "Charlie", "David"] and their corresponding
scores [85, 92, 78, 90], plot a bar chart to represent the scores of each student.
3 Write a Python program that generates random x and y data points using numpy and creates a
scatter plot using matplotlib.
4 Write a Python program that multiplies two 3x3 matrices using numpy’s dot() function.
5 Create a numpy array with numbers from 1 to 20 and slice the array to print:
• The first 5 elements
• The last 5 elements
• Elements at even indices
6 Create two numpy arrays a = np.array([1, 2, 3]) and b = np.array([4, 5, 6]). Perform element-
wise addition, subtraction, multiplication, and division of these arrays and print the results.
7 Given a numpy array data = np.array([10, 15, 7, 22, 17]), calculate and print the following
statistics:
• Mean
• Standard deviation
• Median
• Sum of all elements
8 Create a DataFrame with the following data:
7. GUI Programming
• Tkinter introduction
• Tkinter and Python programming
• Tk widgets and examples
S.no. Program
1 Write a Python program to create a basic Tkinter window with the following properties:
• The window title should be "Welcome Window".
• The window should have a fixed size of 500x400 pixels.
• Add a label that says "Hello, Tkinter!" in the center of the window.
Objective: Understand how to create a basic Tkinter window, set window properties, and add
a label widget.
2 Create a Tkinter window with a button labeled "Press Me". When the button is clicked, it
should display a pop-up message box with the text "Button was clicked!".
Objective: Learn how to create buttons and handle events (button clicks) with the command
option and message boxes.
3 Build a simple calculator using Tkinter. The calculator should have buttons for numbers 0-9,
and basic arithmetic operators (+, -, *, /). It should display the result when the user clicks the
"=" button.
Objective: Combine multiple widgets (buttons, labels, and entry boxes) to create a simple
application, and learn about handling input and calculations in Tkinter.
4 Create a login form with two input fields for username and password. Add a "Login" button
that validates the inputs:
• If the username is "admin" and the password is "password123", display "Login
successful" in a label below the button.
• If the credentials are incorrect, display "Invalid username or password".
Objective: Learn about entry widgets, buttons, and basic form validation using Python logic
within a Tkinter application.
5 Create a Tkinter application to convert temperatures between Celsius and Fahrenheit. It
should have:
• Two entry fields: one for Celsius and one for Fahrenheit.
• Buttons to convert from Celsius to Fahrenheit and vice versa.
• Display the converted temperature in the appropriate entry field.
Objective: Practice handling user input and output, as well as using Tkinter's entry boxes and
buttons to build a useful tool.