0% found this document useful (0 votes)
4 views40 pages

Python Basics All Practice Questions

This document provides a comprehensive set of 280 practice questions for Python basics, ranging from simple tasks like printing messages to more complex operations involving data structures. Each question includes a clear description, sample input, and expected output, allowing users to gradually build their skills from basic to medium difficulty. The topics covered include variables, data types, loops, and various data structures such as strings, lists, tuples, dictionaries, and sets.

Uploaded by

simhify.my
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views40 pages

Python Basics All Practice Questions

This document provides a comprehensive set of 280 practice questions for Python basics, ranging from simple tasks like printing messages to more complex operations involving data structures. Each question includes a clear description, sample input, and expected output, allowing users to gradually build their skills from basic to medium difficulty. The topics covered include variables, data types, loops, and various data structures such as strings, lists, tuples, dictionaries, and sets.

Uploaded by

simhify.my
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PYTHON BASICS – PRACTICE QUESTIONS

WITH SAMPLE I/O (280 Problems)


This document contains detailed practice questions for Python Basics. Each question clearly
describes the task, followed by a sample input and a sample output. The questions start
from basic level and gradually increase to medium difficulty.

1. Introduction to Python
1. Write a Python program to print 'Hello World'.

Sample Input:

No input

Sample Output:

Hello World

2. Write a program to display your name using Python.

Sample Input:

Ravi

Sample Output:

Ravi

3. Write a program to print any message on the screen.

Sample Input:

Welcome

Sample Output:

Welcome

4. Write a program to add two numbers and print the result.

Sample Input:

53

Sample Output:
8

5. Write a program to multiply two numbers.

Sample Input:

46

Sample Output:

24

6. Write a program to subtract two numbers.

Sample Input:

10 3

Sample Output:

7. Write a program to divide two numbers.

Sample Input:

10 2

Sample Output:

5.0

8. Write a program to find the square of a number.

Sample Input:

Sample Output:

25

9. Write a program to find the cube of a number.

Sample Input:

Sample Output:

27
10. Write a program to print a sentence using Python.

Sample Input:

Python is easy

Sample Output:

Python is easy

2. print() Function
1. Print a single line using print().

Sample Input:

Hello

Sample Output:

Hello

2. Print two values using print().

Sample Input:

5 10

Sample Output:

5 10

3. Print values using comma separation.

Sample Input:

Python Easy

Sample Output:

Python Easy

4. Use print() with sep parameter.

Sample Input:

AB

Sample Output:

A-B
5. Use print() with end parameter.

Sample Input:

Hello World

Sample Output:

HelloWorld

6. Print numbers 1 to 3 in one line.

Sample Input:

123

Sample Output:

123

7. Print text and number together.

Sample Input:

Age 20

Sample Output:

Age 20

8. Print a message twice.

Sample Input:

Hi

Sample Output:

Hi
Hi

9. Print different data types together.

Sample Input:

Python 3.10

Sample Output:

Python 3.1

10. Print your city name.


Sample Input:

Delhi

Sample Output:

Delhi

3. Variables
1. Store a number in a variable and print it.

Sample Input:

10

Sample Output:

10

2. Store your name in a variable and print it.

Sample Input:

Ravi

Sample Output:

Ravi

3. Store two numbers and print their sum.

Sample Input:

46

Sample Output:

10

4. Swap two variables.

Sample Input:

37

Sample Output:

73

5. Store a float value and print it.


Sample Input:

10.5

Sample Output:

10.5

6. Store a boolean value and print it.

Sample Input:

True

Sample Output:

True

7. Assign same value to three variables.

Sample Input:

100

Sample Output:

100 100 100

8. Store city name and print its length.

Sample Input:

Delhi

Sample Output:

9. Store marks and print average.

Sample Input:

70 80 90

Sample Output:

80

10. Store age and check eligibility to vote.

Sample Input:
20

Sample Output:

Eligible

4. input() Function
1. Read a number from user and print it.

Sample Input:

Sample Output:

2. Read two numbers and print sum.

Sample Input:

34

Sample Output:

3. Read a name and print it.

Sample Input:

Anu

Sample Output:

Anu

4. Read age and print it.

Sample Input:

21

Sample Output:

21

5. Read two numbers and print product.

Sample Input:
45

Sample Output:

20

6. Read a number and print its square.

Sample Input:

Sample Output:

36

7. Read marks and print result.

Sample Input:

75

Sample Output:

Pass

8. Read temperature and print it.

Sample Input:

30

Sample Output:

30

9. Read price and discount and print final price.

Sample Input:

100 10

Sample Output:

90

10. Read string and print length.

Sample Input:

Python
Sample Output:

5. Data Types
1. Print data type of integer.

Sample Input:

10

Sample Output:

int

2. Print data type of float.

Sample Input:

10.5

Sample Output:

float

3. Print data type of string.

Sample Input:

Hello

Sample Output:

str

4. Print data type of boolean.

Sample Input:

True

Sample Output:

bool

5. Print data type of None.

Sample Input:

None
Sample Output:

NoneType

6. Store list and print its type.

Sample Input:

[1,2,3]

Sample Output:

list

7. Store tuple and print its type.

Sample Input:

(1,2)

Sample Output:

tuple

8. Store set and print its type.

Sample Input:

{1,2}

Sample Output:

set

9. Store dictionary and print its type.

Sample Input:

{'a':1}

Sample Output:

dict

10. Check type of input() value.

Sample Input:

10

Sample Output:
str

5. Data Structures – Strings


1. Write a program to read a string and print it.

Sample Input:

Hello

Sample Output:

Hello

2. Write a program to find the length of a given string.

Sample Input:

Python

Sample Output:

3. Write a program to print the first character of a string.

Sample Input:

Python

Sample Output:

4. Write a program to print the last character of a string.

Sample Input:

Python

Sample Output:

5. Write a program to convert a string to uppercase.

Sample Input:

python

Sample Output:
PYTHON

6. Write a program to reverse a string.

Sample Input:

Python

Sample Output:

nohtyP

7. Write a program to check whether a string is a palindrome.

Sample Input:

madam

Sample Output:

Palindrome

8. Write a program to count vowels in a string.

Sample Input:

hello

Sample Output:

9. Write a program to replace spaces in a string with underscore.

Sample Input:

python basics

Sample Output:

python_basics

10. Write a program to check whether a substring exists in a string.

Sample Input:

on Python

Sample Output:

True
11. Write a program to count the occurrences of a character.

Sample Input:

banana a

Sample Output:

12. Write a program to split a sentence into words.

Sample Input:

Python is fun

Sample Output:

['Python', 'is', 'fun']

13. Write a program to join words using hyphen.

Sample Input:

Python is fun

Sample Output:

Python-is-fun

14. Write a program to find the longest word in a sentence.

Sample Input:

Python is very easy

Sample Output:

Python

15. Write a program to remove duplicate characters from a string.

Sample Input:

programming

Sample Output:

progamin
6. Data Structures – Lists
1. Write a program to create a list and print it.

Sample Input:

[1, 2, 3]

Sample Output:

[1, 2, 3]

2. Write a program to find the length of a list.

Sample Input:

[1,2,3,4]

Sample Output:

3. Write a program to access the first element of a list.

Sample Input:

[10,20,30]

Sample Output:

10

4. Write a program to access the last element of a list.

Sample Input:

[10,20,30]

Sample Output:

30

5. Write a program to append an element to a list.

Sample Input:

[1,2,3] and 4

Sample Output:

[1,2,3,4]
6. Write a program to insert an element at a given position.

Sample Input:

[1,2,3], insert 99 at index 1

Sample Output:

[1,99,2,3]

7. Write a program to remove an element from a list.

Sample Input:

[1,2,3], remove 2

Sample Output:

[1,3]

8. Write a program to find the sum of list elements.

Sample Input:

[1,2,3,4]

Sample Output:

10

9. Write a program to find the maximum element in a list.

Sample Input:

[5,9,2]

Sample Output:

10. Write a program to find the minimum element in a list.

Sample Input:

[5,9,2]

Sample Output:

11. Write a program to reverse a list.


Sample Input:

[1,2,3]

Sample Output:

[3,2,1]

12. Write a program to remove duplicate elements from a list.

Sample Input:

[1,2,2,3]

Sample Output:

[1,2,3]

13. Write a program to count even numbers in a list.

Sample Input:

[1,2,3,4]

Sample Output:

14. Write a program to sort a list in ascending order.

Sample Input:

[3,1,2]

Sample Output:

[1,2,3]

15. Write a program to find the second largest element in a list.

Sample Input:

[10,20,30]

Sample Output:

20

7. Data Structures – Tuples


1. Create a tuple with three numbers and print it.
Sample Input:

(1,2,3)

Sample Output:

(1,2,3)

2. Find the length of a tuple.

Sample Input:

(10,20,30)

Sample Output:

3. Access the first element of a tuple.

Sample Input:

(5,6,7)

Sample Output:

4. Access the last element of a tuple.

Sample Input:

(5,6,7)

Sample Output:

5. Convert a tuple into a list.

Sample Input:

(1,2)

Sample Output:

[1,2]

6. Convert a list into a tuple.

Sample Input:
[3,4]

Sample Output:

(3,4)

7. Count how many times an element occurs in a tuple.

Sample Input:

(1,2,2,3)

Sample Output:

8. Find the index of an element in a tuple.

Sample Input:

(10,20,30), 20

Sample Output:

9. Check whether an element exists in a tuple.

Sample Input:

5 in (1,2,3)

Sample Output:

False

10. Unpack a tuple into two variables and print them.

Sample Input:

(10,20)

Sample Output:

10 20

11. Find the sum of tuple elements.

Sample Input:

(1,2,3)
Sample Output:

12. Find the maximum element in a tuple.

Sample Input:

(5,1,9)

Sample Output:

13. Find the minimum element in a tuple.

Sample Input:

(5,1,9)

Sample Output:

14. Reverse a tuple.

Sample Input:

(1,2,3)

Sample Output:

(3,2,1)

15. Access elements from a nested tuple.

Sample Input:

(1,(2,3))

Sample Output:

23

8. Data Structures – Dictionaries


1. Create a dictionary and print it.

Sample Input:

{'a':1,'b':2}
Sample Output:

{'a':1,'b':2}

2. Access a value using a key.

Sample Input:

{'name':'Ravi'} name

Sample Output:

Ravi

3. Add a new key-value pair to a dictionary.

Sample Input:

{'a':1}, add b:2

Sample Output:

{'a':1,'b':2}

4. Update the value of an existing key.

Sample Input:

{'a':1}, update a to 10

Sample Output:

{'a':10}

5. Remove a key from a dictionary.

Sample Input:

{'a':1,'b':2}, remove b

Sample Output:

{'a':1}

6. Print all keys of a dictionary.

Sample Input:

{'x':1,'y':2}

Sample Output:
dict_keys(['x','y'])

7. Print all values of a dictionary.

Sample Input:

{'x':1,'y':2}

Sample Output:

dict_values([1,2])

8. Loop through a dictionary and print keys and values.

Sample Input:

{'a':1,'b':2}

Sample Output:

a 1, b 2

9. Check if a key exists in a dictionary.

Sample Input:

a in {'a':1}

Sample Output:

True

10. Count frequency of characters in a string using dictionary.

Sample Input:

hello

Sample Output:

{'h':1,'e':1,'l':2,'o':1}

11. Merge two dictionaries.

Sample Input:

{'a':1} and {'b':2}

Sample Output:

{'a':1,'b':2}
12. Find the key with maximum value.

Sample Input:

{'a':5,'b':9,'c':3}

Sample Output:

13. Create a dictionary of numbers and their squares.

Sample Input:

1 to 5

Sample Output:

{1:1,2:4,3:9,4:16,5:25}

14. Remove all items from a dictionary.

Sample Input:

{'a':1}

Sample Output:

{}

15. Convert two lists into a dictionary.

Sample Input:

['a','b'], [1,2]

Sample Output:

{'a':1,'b':2}

9. Data Structures – Sets


1. Create a set and print it.

Sample Input:

{1,2,3}

Sample Output:

{1,2,3}
2. Remove duplicate elements from a list using set.

Sample Input:

[1,2,2,3]

Sample Output:

[1,2,3]

3. Add an element to a set.

Sample Input:

{1,2}, add 3

Sample Output:

{1,2,3}

4. Remove an element from a set.

Sample Input:

{1,2,3}, remove 2

Sample Output:

{1,3}

5. Find union of two sets.

Sample Input:

{1,2} {2,3}

Sample Output:

{1,2,3}

6. Find intersection of two sets.

Sample Input:

{1,2} {2,3}

Sample Output:

{2}

7. Find difference of two sets.


Sample Input:

{1,2} {2,3}

Sample Output:

{1}

8. Find symmetric difference.

Sample Input:

{1,2} {2,3}

Sample Output:

{1,3}

9. Check if set is subset.

Sample Input:

{1,2} ⊆ {1,2,3}

Sample Output:

True

10. Check if two sets are disjoint.

Sample Input:

{1,2} {3,4}

Sample Output:

True

11. Count unique characters in a string.

Sample Input:

program

Sample Output:

{'p','r','o','g','a','m'}

12. Convert set to list.

Sample Input:
{1,2,3}

Sample Output:

[1,2,3]

13. Remove all elements from set.

Sample Input:

{1,2}

Sample Output:

set()

14. Check membership in set.

Sample Input:

2 in {1,2,3}

Sample Output:

True

15. Create set of even numbers up to 10.

Sample Input:

1 to 10

Sample Output:

{2,4,6,8,10}

10. Loops
1. Print numbers from 1 to 5 using a loop.

Sample Input:

Sample Output:

12345

2. Print even numbers from 1 to 10.

Sample Input:
-

Sample Output:

2 4 6 8 10

3. Print multiplication table of a number.

Sample Input:

Sample Output:

5 10 15 20 25

4. Find factorial of a number.

Sample Input:

Sample Output:

120

5. Find sum of numbers from 1 to n.

Sample Input:

Sample Output:

15

6. Count digits in a number.

Sample Input:

1234

Sample Output:

7. Reverse a number.

Sample Input:

123
Sample Output:

321

8. Check if number is prime.

Sample Input:

Sample Output:

Prime

9. Generate Fibonacci series.

Sample Input:

Sample Output:

01123

10. Print pattern of stars.

Sample Input:

Sample Output:

* ** ***

11. Find sum of digits.

Sample Input:

123

Sample Output:

12. Find largest number in a list using loop.

Sample Input:

[3,7,2]

Sample Output:
7

13. Count vowels in a string using loop.

Sample Input:

hello

Sample Output:

14. Print numbers using while loop.

Sample Input:

1 to 5

Sample Output:

12345

15. Use break to stop loop at a condition.

Sample Input:

stop at 3

Sample Output:

12

11. Conditional Statements


1. Check if a number is positive or negative.

Sample Input:

-5

Sample Output:

Negative

2. Check if number is even or odd.

Sample Input:

Sample Output:
Even

3. Find greatest of two numbers.

Sample Input:

59

Sample Output:

4. Find greatest of three numbers.

Sample Input:

375

Sample Output:

5. Check leap year.

Sample Input:

2024

Sample Output:

Leap Year

6. Grade student based on marks.

Sample Input:

85

Sample Output:

7. Check if character is vowel.

Sample Input:

Sample Output:

Vowel
8. Check divisibility by 5 and 11.

Sample Input:

55

Sample Output:

Divisible

9. Simple calculator.

Sample Input:

10 + 5

Sample Output:

15

10. Check valid triangle.

Sample Input:

60 60 60

Sample Output:

Valid

11. Check voting eligibility.

Sample Input:

18

Sample Output:

Eligible

12. Login validation.

Sample Input:

correct user & pass

Sample Output:

Login Successful

13. Check number range.


Sample Input:

15

Sample Output:

Between 10 and 20

14. Check uppercase or lowercase.

Sample Input:

Sample Output:

Uppercase

15. Check year is century year.

Sample Input:

2000

Sample Output:

Century Year

12. Functions
1. Create a function to add two numbers.

Sample Input:

53

Sample Output:

2. Create a function to find square of number.

Sample Input:

Sample Output:

16

3. Create a function to check prime.


Sample Input:

Sample Output:

Prime

4. Create a function to find factorial.

Sample Input:

Sample Output:

120

5. Create a function to reverse a string.

Sample Input:

Python

Sample Output:

nohtyP

6. Create a function to count vowels.

Sample Input:

hello

Sample Output:

7. Create a function to find max of three numbers.

Sample Input:

395

Sample Output:

8. Create a function to check palindrome.

Sample Input:
madam

Sample Output:

Palindrome

9. Create a function using default arguments.

Sample Input:

name=Guest

Sample Output:

Hello Guest

10. Create a function using keyword arguments.

Sample Input:

age=20

Sample Output:

20

11. Create a function using *args.

Sample Input:

123

Sample Output:

12. Create a function using **kwargs.

Sample Input:

name=Ravi

Sample Output:

Ravi

13. Create a recursive function.

Sample Input:

5
Sample Output:

120

14. Create function to return list sum.

Sample Input:

[1,2,3]

Sample Output:

15. Create function to check even or odd.

Sample Input:

Sample Output:

Even

13. Exception Handling


1. Handle division by zero.

Sample Input:

10 / 0

Sample Output:

Cannot divide by zero

2. Handle invalid integer input.

Sample Input:

abc

Sample Output:

Invalid input

3. Handle file not found error.

Sample Input:

[Link]
Sample Output:

File not found

4. Handle index error.

Sample Input:

[1,2][5]

Sample Output:

Index error

5. Handle key error.

Sample Input:

{'a':1}['b']

Sample Output:

Key error

6. Use finally block.

Sample Input:

Sample Output:

Executed

7. Raise exception for negative age.

Sample Input:

-5

Sample Output:

Invalid age

8. Handle multiple exceptions.

Sample Input:

Sample Output:
Error occurred

9. Convert string to int safely.

Sample Input:

abc

Sample Output:

Conversion failed

10. Handle value error.

Sample Input:

int('a')

Sample Output:

Value error

11. Handle type error.

Sample Input:

5 + 'a'

Sample Output:

Type error

12. Custom exception message.

Sample Input:

Sample Output:

Custom error

13. Exception in function.

Sample Input:

Sample Output:

Handled
14. Use else block.

Sample Input:

10

Sample Output:

No exception

15. Handle overflow error.

Sample Input:

Sample Output:

Overflow

14. File Handling


1. Open a file in read mode.

Sample Input:

[Link]

Sample Output:

File opened

2. Write text to a file.

Sample Input:

Hello

Sample Output:

Written

3. Read content from a file.

Sample Input:

[Link]

Sample Output:

Hello
4. Append data to file.

Sample Input:

World

Sample Output:

Appended

5. Close a file.

Sample Input:

Sample Output:

Closed

6. Read file line by line.

Sample Input:

[Link]

Sample Output:

Lines printed

7. Count number of lines in file.

Sample Input:

[Link]

Sample Output:

8. Count words in file.

Sample Input:

[Link]

Sample Output:

10

9. Check file existence.


Sample Input:

[Link]

Sample Output:

Exists

10. Handle file not found error.

Sample Input:

[Link]

Sample Output:

File not found

11. Use with statement.

Sample Input:

[Link]

Sample Output:

Closed automatically

12. Copy file content.

Sample Input:

[Link] to [Link]

Sample Output:

Copied

13. Read binary file.

Sample Input:

[Link]

Sample Output:

Read

14. Write multiple lines.

Sample Input:
Hello\nWorld

Sample Output:

Written

15. Delete a file.

Sample Input:

[Link]

Sample Output:

Deleted

You might also like