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

python

Uploaded by

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

python

Uploaded by

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

1.

Try-Except: Handling Input Errors

Write a program that prompts the user to input a number. Use a try-except block to
catch:

ValueError if the user inputs something that is not a number.


ZeroDivisionError if the user attempts to divide a number by zero.
Print appropriate error messages for each case.
---------------------
2. Try-Except: Safe List Indexing

Create a list of 5 numbers. Write a program that asks the user for an index and
tries to access the element at that index. Use try-except to handle:

IndexError if the user enters an index out of range.


Print an error message if the index is invalid, or print the element at that
index if it's valid.
---------------------------
3. List Functions: Find Maximum and Minimum

Write a program that creates a list of integers. Use the max() and min() functions
to find and print the largest and smallest numbers in the list.
----------------------------------
4. List Functions: Remove Duplicates

Write a program that takes a list with duplicate elements (e.g., [1, 2, 2, 3, 3, 3,
4]) and removes duplicates using the set() function. Print the list without
duplicates.
-----------------------------------------
5. List Functions: Reverse a List

Write a program that creates a list of strings. Use the reverse() function to
reverse the list and print the reversed list.
------------------------------------------------
6. List Functions: Insert at a Specific Position
---------------------------------------------------------------
Write a program that creates a list of numbers. Insert the number 99 at the third
position using the insert() function and print the modified list.

7. Function: Greet User


--------------------------------------------------------------------------
Write a function greet(name) that takes a name as an argument and prints a greeting
message like: "Hello, [name]!". Call the function with your name.

8. Function: Calculate Square


----------------------------------------------------------------------------------
Write a function square(num) that takes a number as an argument and returns the
square of that number. Call the function with different numbers and print the
results
-----------------------------------------------------------------------------------
-------
9. Function: Multiply Two Numbers

Write a function multiply(a, b) that takes two numbers as input and returns their
product. Test the function with different values for a and b.

You might also like