Python 100 Programming Examples and Solutions
This article presents a comprehensive collection of 100 Python programming exercises covering topics such as number combinations, tax calculations, perfect squares, date handling, sorting algorithms, Fibonacci sequences, matrix operations, and more, each accompanied by problem descriptions, analysis, and complete code solutions.
This document provides a catalog of 100 Python programming exercises, each with a problem statement, brief analysis, and a full code implementation. The examples range from basic arithmetic and control structures to more complex algorithms and data manipulations.
Example 001: Number Combination
Problem: Given the digits 1, 2, 3, 4, generate all distinct three‑digit numbers without repeating digits.
Analysis: Iterate through all possible triples and filter out those with duplicate digits.
<code>total=0
for i in range(1,5):
for j in range(1,5):
for k in range(1,5):
if ((i!=j)and(j!=k)and(k!=i)):
print(i,j,k)
total+=1
print(total)</code>A more concise solution uses itertools.permutations :
<code>import itertools
sum2=0
a=[1,2,3,4]
for i in itertools.permutations(a,3):
print(i)
sum2+=1
print(sum2)</code>Example 002: Tax Bonus Calculation
Problem: Compute a bonus based on profit tiers with decreasing commission rates.
Analysis: Apply piecewise calculations for each profit interval.
<code>profit=int(input('Show me the money: '))
bonus=0
thresholds=[100000,100000,200000,200000,400000]
rates=[0.1,0.075,0.05,0.03,0.015,0.01]
for i in range(len(thresholds)):
if profit<=thresholds[i]:
bonus+=profit*rates[i]
profit=0
break
else:
bonus+=thresholds[i]*rates[i]
profit-=thresholds[i]
bonus+=profit*rates[-1]
print(bonus)</code>Example 003: Perfect Square Problem
Problem: Find an integer that becomes a perfect square after adding 100 and again after adding 168.
Analysis: Brute‑force search up to the point where the gap between successive squares exceeds 168.
<code>n=0
while (n+1)**2-n*n<=168:
n+=1
print(n+1)</code>Example 004: Day of Year
Problem: Determine the day number within a year for a given date, accounting for leap years.
<code>def isLeapYear(y):
return (y%400==0 or (y%4==0 and y%100!=0))
DofM=[0,31,28,31,30,31,30,31,31,30,31,30]
res=0
year=int(input('Year:'))
month=int(input('Month:'))
day=int(input('day:'))
if isLeapYear(year):
DofM[2]+=1
for i in range(month):
res+=DofM[i]
print(res+day)</code>Example 005: Sorting Three Numbers
Problem: Input three integers and output them in ascending order.
<code>raw=[]
for i in range(3):
x=int(input('int%d: '%(i)))
raw.append(x)
for i in range(len(raw)):
for j in range(i,len(raw)):
if raw[i]>raw[j]:
raw[i],raw[j]=raw[j],raw[i]
print(raw)
# Using built‑in sort
raw2=[]
for i in range(3):
x=int(input('int%d: '%(i)))
raw2.append(x)
print(sorted(raw2))</code>Example 006: Fibonacci Sequence
Problem: Generate Fibonacci numbers using recursion or iteration.
<code>def Fib(n):
return 1 if n<=2 else Fib(n-1)+Fib(n-2)
print(Fib(int(input())))
# Iterative version
target=int(input())
res=0
a,b=1,1
for i in range(target-1):
a,b=b,a+b
print(a)</code>... (similar sections continue for examples 007 through 050, each presenting the problem, brief analysis, and the corresponding Python code wrapped in tags) ...
The collection concludes with references to the original repository.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.