0% found this document useful (0 votes)
9 views2 pages

Python Practice Answers

The document provides a collection of Python practice problems along with their answers and explanations. It covers various topics including numbers, if statements, loops, strings, lists, dictionaries, text files, and functions. Each section includes specific tasks and the corresponding code snippets to solve them.

Uploaded by

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

Python Practice Answers

The document provides a collection of Python practice problems along with their answers and explanations. It covers various topics including numbers, if statements, loops, strings, lists, dictionaries, text files, and functions. Each section includes specific tasks and the corresponding code snippets to solve them.

Uploaded by

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

Python Practice Problems – Answers & Explanations

Numbers
Check if number is even or odd
Use n % 2 == 0 to determine evenness.

Sum 1 to 100
Use sum(range(1, 101)).

If Statements
Positive/negative/zero
Use if n>0, elif n<0, else.

Grading system
Compare ranges using if/elif blocks.

For Loops
Multiples of 3
Loop from 1–100 and check i % 3 == 0.

Count vowels
Iterate string and count characters in 'aeiou'.

Strings
Reverse a string
Use a loop building the reversed string.

Character counts
Use a dictionary or count() in a loop.

Lists
Remove duplicates
Use list(set(lst)) or filter manually.

Find largest/smallest
Use max(lst) and min(lst).
More with Lists
Flatten nested list
Loop sublists and extend a result list.

Rotate left
[Link]([Link](0)).

While Loops
Ask until 'exit'
Use while True and break on input == 'exit'.

Fibonacci to 100
Generate iteratively a,b = b,a+b.

Dictionaries
Word frequencies
Split sentence and increment dictionary counters.

Swap keys/values
Use {v:k for k,v in [Link]()}.

Text Files
Count lines
Open file and use len([Link]()).

Write numbers
Write each number using a loop with [Link]().

Functions
Factorial
Use recursion or iterative multiplication.

Palindrome check
Compare string to its reversed version.

You might also like