Python Sample Programs for Beginners: Loops, Numbers and Dates

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.

Interactive Companion Jupyter Notebook

Run the Python examples, compare expected outputs, modify the code, and complete guided exercises.

View on GitHub Open in Google Colab
On this page — Show page sections— Hide page sections

First working example

a, 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.

Sum three numbers entered by the user

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

Grade using elif

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

Convert decimal to binary

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

Round positive and negative values

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

Check positive, negative or zero

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]

Vowel or consonant checker

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

Largest of three values

The built-in max() is concise; a sequence of comparisons can also solve the exercise.

values = (12, 39, 7)
print(max(values))

Output:

39

Count vowels in a sentence

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

Check a leap year

Use the standard-library function to handle the full century rule.

import calendar
print(calendar.isleap(2000))
print(calendar.isleap(1900))

Output:

True
False

Sum the first n natural numbers

The arithmetic formula avoids looping from 1 to n.

n = 10
print(n * (n + 1) // 2)

Output:

55

Python sample-code learning path

Move from simpler loops and arithmetic to algorithms, recursion and date handling. Each topic links to its full tutorial.

  1. Sum of Digits — practice program 1 of 12
  2. Factors of a Number — practice program 2 of 12
  3. Prime Numbers — practice program 3 of 12
  4. Factorial — practice program 4 of 12
  5. Strong Numbers — practice program 5 of 12
  6. Sum of Digit Cubes — practice program 6 of 12
  7. Armstrong Numbers — practice program 7 of 12
  8. GCD and LCM — practice program 8 of 12
  9. Multiplication Tables — practice program 9 of 12
  10. Recursive Functions — practice program 10 of 12
  11. Fibonacci Series — practice program 11 of 12
  12. Date Exercises — practice program 12 of 12

Common mistakes and troubleshooting

  • Run each example in Python 3.9 or newer. Indentation is significant in loops and functions.
  • Remember that input() returns a string. Convert and validate the value before doing arithmetic.
  • Check boundary values such as zero, negative numbers, small inputs and empty ranges.
  • When comparing outputs, note whether a code sample uses fixed values or user input.

Practice exercises

Choose three programs from the hub and predict their outputs before running them. Use the companion practice notebook for runnable examples and self-checks.

Frequently asked questions

Where should beginners start?

Begin with input, conditions, loops and basic numeric operations; follow the learning path on this hub.

Do I need to install extra packages?

No. The numbered examples on this page use Python standard-library features.

Are notebooks required to read the tutorials?

No. Each tutorial contains runnable code; the notebooks are optional guided practice.

Plus2Net Python Basics podcast playlist

Other related resources

Back to top ↑




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer