Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

python programs--1

The document outlines various Python programming exercises categorized into sections such as Basics, Data Structures, String Manipulation, and Functional Programming. Each section includes multiple tasks ranging from variable manipulation and input/output operations to advanced topics like decorators and generators. It serves as a comprehensive guide for practicing Python programming skills.

Uploaded by

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

python programs--1

The document outlines various Python programming exercises categorized into sections such as Basics, Data Structures, String Manipulation, and Functional Programming. Each section includes multiple tasks ranging from variable manipulation and input/output operations to advanced topics like decorators and generators. It serves as a comprehensive guide for practicing Python programming skills.

Uploaded by

Manish Vardhan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Python Programs

1. Python Basics
- Variables and Data Types:
1. Write a Python program to swap two variables without using a third variable.
2. Create a program to check if a given number is an integer or a float.
3. Write a program to take two float numbers from the user and print their product.
4. How would you convert a boolean value to an integer in Python? Write a program for it.
5. Write a Python program to take a user’s name and age as input, and print a formatted
string displaying the information.

i
- Input/Output:

id
1. Write a program to take a user’s input and print it in reverse.
2. Create a program to prompt the user for two numbers and print their sum.
3. Write a program that takes a sentence as input and prints the number of characters in it.
4. Take a string input from the user and print whether it contains any numbers.
5. Write a program to print the multiplication table of a number given by the user.

gd
- Operators:
1. Write a Python program to perform addition, subtraction, multiplication, and division using
input values.
2. Create a program to find the maximum of three numbers using comparison operators.
3. Write a program to check if a number is divisible by both 5 and 7.
in
4. Create a program that uses bitwise operators to swap two numbers.
5. Write a program to perform logical operations (and, or, not) on two boolean inputs.

- Conditional Statements:
od

1. Write a Python program to check if a number is positive, negative, or zero.


2. Create a program to determine the grade based on a student’s score.
3. Write a program to find the largest of three numbers using if-else statements.
4. Create a program to check whether a given year is a leap year.
5. Write a program that takes an integer and checks if it is odd or even.
C

- Loops:
1. Write a Python program to print the first 10 natural numbers using a loop.
2. Create a program to find the factorial of a number using a while loop.
3. Write a program that uses a for loop to print the Fibonacci sequence up to 100.
4. Create a program to find the sum of all even numbers between 1 and 50.
5. Write a program to reverse a given string using a loop.

- Functions:
1. Write a Python function to find the greatest common divisor (GCD) of two numbers.
2. Create a function that takes a list of numbers and returns the average.
3. Write a function to check if a string is a palindrome.
4. Create a function that takes two numbers and returns their LCM (Least Common Multiple).
5. Write a function that accepts any number of arguments and returns the sum of all
arguments.

- Exception Handling:
1. Write a program that takes two numbers as input and performs division, handling the case
where the divisor is zero.
2. Create a program that handles file opening exceptions when the file does not exist.
3. Write a program that handles input exceptions when the user enters non-integer data.
4. Create a program that catches exceptions when converting strings to integers.
5. Write a program to raise a custom exception if the input is a negative number.

i
---

id
2. Data Structures
- Lists:
1. Write a Python program to find the second largest number in a list.

gd
2. Create a program to remove duplicates from a list.
3. Write a program to reverse a list without using the `reverse()` method.
4. Create a function to find the average of numbers in a list.
5. Write a program to check if a list is empty or not.

- Tuples:
in
1. Write a Python program to find the index of an element in a tuple.
2. Create a program to convert a list into a tuple.
3. Write a program to find the length of a tuple.
4. Create a program to join two tuples together.
5. Write a program to reverse a tuple.
od

- Sets:
1. Write a Python program to find the union of two sets.
2. Create a program to check if a given set is a subset of another set.
3. Write a program to find the intersection of two sets.
4. Create a program to find the difference between two sets.
C

5. Write a program to remove all elements from a set.

- Dictionaries:
1. Write a Python program to merge two dictionaries.
2. Create a program to find the key with the maximum value in a dictionary.
3. Write a program to iterate through a dictionary and print all the keys and values.
4. Create a program to check if a key exists in a dictionary.
5. Write a program to remove a specific key from a dictionary.

- List, Dictionary, and Set Comprehension:


1. Write a program to create a list of squares from 1 to 10 using list comprehension.
2. Create a program to extract even numbers from a list using list comprehension.
3. Write a program to create a dictionary from a list where keys are elements and values are
their squares using dictionary comprehension.
4. Create a program to create a set of unique vowels in a given string using set
comprehension.
5. Write a program to create a dictionary from two lists: one with keys and one with values
using dictionary comprehension.

- Sorting and Searching:


1. Write a Python program to sort a list of numbers in ascending order.
2. Create a program to implement binary search on a sorted list.

i
3. Write a program to find the largest element in a list using a linear search.

id
4. Create a program to sort a dictionary by its values.
5. Write a program to sort a list of tuples by the second element in each tuple.

---

gd
3. String Manipulation
- String Operations:
1. Write a Python program to reverse a string without using slicing.
2. Create a program to count the number of vowels in a given string.
3. Write a program to check if a string is a palindrome.
in
4. Create a program to remove all spaces from a string.
5. Write a program to replace all occurrences of a substring in a string.

- String Methods:
1. Write a program to capitalize the first letter of each word in a string.
od

2. Create a program to count the number of occurrences of a substring in a string.


3. Write a program to convert all lowercase letters in a string to uppercase.
4. Create a program to split a string into a list of words.
5. Write a program to check if a string ends with a certain suffix.

- Regular Expressions:
C

1. Write a Python program to validate an email address using regular expressions.


2. Create a program to find all the words in a string that start with a vowel.
3. Write a program to extract all the numbers from a given string.
4. Create a program to check if a string contains only alphanumeric characters using regular
expressions.
5. Write a program to replace all digits in a string with `#`.

---

4. Functional Programming
- Lambda Functions:
1. Write a lambda function to square a number and use it on a list of numbers.
2. Create a lambda function to return the larger of two numbers.
3. Write a lambda function to filter out even numbers from a list.
4. Create a lambda function to find the cube of a number.
5. Write a program to sort a list of tuples using a lambda function.

- Map, Filter, and Reduce:


1. Write a Python program using `map()` to double each number in a list.
2. Create a program using `filter()` to find all prime numbers in a list.
3. Write a program using `reduce()` to find the product of all elements in a list.
4. Create a program to square each element in a list using `map()` and a lambda function.

i
5. Write a program using `filter()` to remove all negative numbers from a list.

id
- Decorators:
1. Write a decorator to measure the time taken by a function to execute.
2. Create a program to make a function execute twice using a decorator.

gd
3. Write a decorator to print a function’s name every time it is called.
4. Create a program to add authentication before executing a function using a decorator.
5. Write a decorator that capitalizes the output of a function that returns a string.

- Generators and Iterators:


1. Write a generator function to yield the first `n` Fibonacci numbers.
in
2. Create an iterator class to iterate through the characters of a string.
3. Write a generator function to generate the squares of numbers from 1 to

Follow @codingdidi –
od

Instagram https://www.instagram.com/codingdidi/
Youtube https://youtube.com/@codingdidi?si=TkcPMdGsgV8z61lh
Linkedin www.linkedin.com/in/akansha-yadav24
C

You might also like