Explore Python sample programs organized into a learning path. Start with a small input-and-output exercise, then work through the focused tutorials below. Each supporting lesson has its own runnable examples and guided practice notebook.
Run the Python examples, compare expected outputs, modify the code, and complete guided exercises.
View on GitHuba, b, c = 10, 15, 5
print("Sum of three numbers:", a + b + c)
Output:
Sum of three numbers: 30
The original hub begins with the sum of three input numbers. This fixed-value version lets you check the result immediately.
All input() values are text, so convert them to numbers before addition.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
print("Sum:", a + b + c)
Output:
Sum: 30
An elif chain is simpler than deeply nested if/else blocks.
marks = int(input("Enter your marks: "))
if marks >= 90:
result = "First distinction"
elif marks >= 60:
result = "Second distinction"
elif marks >= 35:
result = "Passed"
else:
result = "Failed"
print(result)
Output:
Second distinction
The original hub demonstrates repeated division by two. The built-in bin() provides the usual equivalent.
n = 156
print(bin(n)[2:])
def binary_digits(n):
if n == 0:
return "0"
digits = ""
while n > 0:
digits = str(n % 2) + digits
n //= 2
return digits
print(binary_digits(156))
Output:
10011100
10011100
The original int(x + 0.5) shortcut is only suitable for a narrow range of nonnegative inputs. Python’s round() uses ties-to-even and the examples below show the difference.
for n in (2.4, 2.5, 3.5, -2.5):
print(n, round(n))
Output:
2.4 2
2.5 2
3.5 4
-2.5 -2
A basic conditional program for integer input.
n = -8
if n > 0:
print("Positive")
elif n < 0:
print("Negative")
else:
print("Zero")
Output:
Negative
range(start, stop, step) avoids a separate evenness check for a fixed sequence.
print(list(range(2, 21, 2)))
Output:
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
Validate that the input contains one alphabetic letter.
letter = "E".lower()
if len(letter) != 1 or not letter.isalpha():
print("Enter one letter")
elif letter in "aeiou":
print("Vowel")
else:
print("Consonant")
Output:
Vowel
The built-in max() is concise; a sequence of comparisons can also solve the exercise.
values = (12, 39, 7)
print(max(values))
Output:
39
Use sum() with a generator expression.
sentence = "Learn Python with plus2net"
count = sum(1 for ch in sentence.lower() if ch in "aeiou")
print(count)
Output:
6
Use the standard-library function to handle the full century rule.
import calendar
print(calendar.isleap(2000))
print(calendar.isleap(1900))
Output:
True
False
The arithmetic formula avoids looping from 1 to n.
n = 10
print(n * (n + 1) // 2)
Output:
55
Move from simpler loops and arithmetic to algorithms, recursion and date handling. Each topic links to its full tutorial.
input() returns a string. Convert and validate the value before doing arithmetic.Choose three programs from the hub and predict their outputs before running them. Use the companion practice notebook for runnable examples and self-checks.
Begin with input, conditions, loops and basic numeric operations; follow the learning path on this hub.
No. The numbered examples on this page use Python standard-library features.
No. Each tutorial contains runnable code; the notebooks are optional guided practice.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.