Fibonacci series up to 50

Python Practice › Basic Programs › Program 25

Fibonacci series up to 50

EasyBasic Programs · 25 of 40

Write a Python program to print the Fibonacci series between 0 and 50. Each number is the sum of the two before it.

Output
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Approach

  1. Start with a = 0 and b = 1.
  2. Print a, then move forward with a, b = b, a + b until a passes 50.

Solution

Python
a, b = 0, 1series = []while a <= 50:    series.append(a)    a, b = b, a + bprint(series)
Output
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Line by line

Line 1

The series starts with 0 and 1.

Line 3

The loop stops as soon as the next number would be above 50.

Line 5

Both variables update at once: the new a is the old b, and the new b is the sum of the old pair.

Learn the concept

New to this topic? Read the lesson, then come back.

More basic programs

← All 40 Basic Programs

Reverse a number

Python Practice › Basic Programs › Program 24

Reverse a number

EasyBasic Programs · 24 of 40

Write a Python program to reverse the digits of a number.

Input
12345
Output
54321

Approach

  1. Take the last digit with % 10 and remove it with // 10.
  2. Build the reversed number digit by digit.

Solution 1: arithmetic

Python
n = 12345reversed_n = 0while n > 0:    digit = n % 10    reversed_n = reversed_n * 10 + digit    n //= 10print(reversed_n)
Output
54321

Line by line

Line 4

n % 10 is the last digit (5 first).

Line 5

Shifting the result one place left (* 10) and adding the digit appends it.

Line 6

//= 10 removes the last digit; the loop ends when n reaches 0.

Solution 2: string slicing

Python
n = 12345print(int(str(n)[::-1]))
Output
54321

Line by line

Line 2

Convert to a string, reverse it with [::-1] and convert back to int.

Learn the concept

New to this topic? Read the lesson, then come back.

More basic programs

← All 40 Basic Programs

Factorial of a number

Python Practice › Basic Programs › Program 23

Factorial of a number

EasyBasic Programs · 23 of 40

Write a Python program to find the factorial of a number. n! = 1 × 2 × … × n, and 0! = 1.

Input
n = 6
Output
6! = 720

Approach

  1. Start with 1 and multiply by every number from 2 to n.

Solution 1: loop

Python
n = 6factorial = 1for i in range(2, n + 1):    factorial *= iprint(f"{n}! = {factorial}")
Output
6! = 720

Line by line

Line 2

Start at 1, the identity for multiplication (and the value of 0!).

Line 3

range(2, n + 1) gives 2, 3, 4, 5, 6; the end value is excluded, so add 1.

Line 4

*= multiplies the running result by each number.

Solution 2: math.factorial

Python
import mathprint(math.factorial(6), math.factorial(0))
Output
720 1

Line by line

Line 2

The standard library already has an efficient factorial function.

Learn the concept

New to this topic? Read the lesson, then come back.

More basic programs

← All 40 Basic Programs

Days between two dates

Python Practice › Basic Programs › Program 22

Days between two dates

EasyBasic Programs · 22 of 40

Write a Python program to calculate the number of days between two dates.

Input
(2023, 1, 5) and (2023, 1, 22)
Output
17 days

Approach

  1. Create two date objects.
  2. Subtract them to get a timedelta and read its .days.

Solution

Python
from datetime import date start = date(2023, 1, 5)end = date(2023, 1, 22)gap = end - startprint(gap.days, "days")
Output
17 days

Line by line

Line 5

Subtracting dates gives a timedelta object.

Line 6

.days is the whole number of days between them.

Learn the concept

New to this topic? Read the lesson, then come back.

More basic programs

← All 40 Basic Programs

Print the current date in a format

Python Practice › Basic Programs › Program 21

Print the current date in a format

EasyBasic Programs · 21 of 40

Write a Python program to print the current date in the format “2023 Jan 05” (year, short month name, day) using the datetime module.

Output
2026 Nov 02

Approach

  1. Get today’s date with date.today().
  2. Format it with strftime("%Y %b %d").

Solution

Python
from datetime import date today = date.today()print(today.strftime("%Y %b %d"))
Output
2026 Nov 02

Line by line

Line 3

date.today() reads the date from your computer’s clock.

Line 4

%Y is the 4-digit year, %b the short month name and %d the 2-digit day.

Note

The output shows the date on which this page was built. Press Try it and Run to see today’s date.

Learn the concept

New to this topic? Read the lesson, then come back.

More basic programs

← All 40 Basic Programs

Simple interest

Python Practice › Basic Programs › Program 20

Simple interest

EasyBasic Programs · 20 of 40

Write a Python program to calculate simple interest. Simple interest = P × R × T / 100, where P is the principal, R the yearly rate in percent and T the time in years.

Input
P = 10000, R = 6.5, T = 3
Output
Simple interest: 1950.0
Total amount: 11950.0

Approach

  1. Apply the formula.
  2. Add the interest to the principal for the total amount.

Solution

Python
principal = 10000rate = 6.5years = 3 interest = principal * rate * years / 100amount = principal + interestprint("Simple interest:", interest)print("Total amount:", amount)
Output
Simple interest: 1950.0
Total amount: 11950.0

Line by line

Line 5

Dividing by 100 converts the percentage rate into a fraction.

Line 6

The amount repaid is the principal plus the interest.

Note

The original version of this exercise listed the formula as P + (P/r)*t, which is not the simple interest formula. The correct formula is P × R × T / 100.

Learn the concept

New to this topic? Read the lesson, then come back.

More basic programs

← All 40 Basic Programs

Check an Armstrong number

Python Practice › Basic Programs › Program 19

Check an Armstrong number

MediumBasic Programs · 19 of 40

Write a Python program to check whether a number is an Armstrong number: the sum of its digits, each raised to the power of the number of digits, equals the number (153 = 1³ + 5³ + 3³).

Input
153, 370, 123, 9474
Output
153 True
370 True
123 False
9474 True

Approach

  1. Count the digits.
  2. Raise each digit to that power and add them up.
  3. Compare the sum with the original number.

Solution

Python
def is_armstrong(n):    digits = str(n)    power = len(digits)    total = sum(int(d) ** power for d in digits)    return total == n for n in [153, 370, 123, 9474]:    print(n, is_armstrong(n))
Output
153 True
370 True
123 False
9474 True

Line by line

Line 2

Turning the number into a string makes it easy to loop over its digits.

Line 3

The power is the number of digits: 3 for 153, 4 for 9474.

Line 4

The generator expression converts each digit back to int, raises it to power and sum() adds them.

Line 5

The function returns True only when the sum equals the number.

Learn the concept

New to this topic? Read the lesson, then come back.

More basic programs

← All 40 Basic Programs

Surface area of a cylinder

Python Practice › Basic Programs › Program 18

Surface area of a cylinder

EasyBasic Programs · 18 of 40

Write a Python program to calculate the surface area of a cylinder. Area = 2πrh + 2πr².

Input
r = 3, h = 10
Output
Surface area: 245.04

Approach

  1. Curved surface = 2πrh; the two circular ends = 2πr².
  2. Add them.

Solution

Python
import math r = 3h = 10curved = 2 * math.pi * r * hends = 2 * math.pi * r ** 2print("Surface area:", round(curved + ends, 2))
Output
Surface area: 245.04

Line by line

Lines 5-6

Splitting the formula into named parts makes each step easy to check.

Line 7

The total is rounded to two decimals.

Learn the concept

New to this topic? Read the lesson, then come back.

More basic programs

← All 40 Basic Programs

Surface area of a cube

Python Practice › Basic Programs › Program 17

Surface area of a cube

EasyBasic Programs · 17 of 40

Write a Python program to calculate the surface area of a cube. Surface area = 6 × a².

Input
a = 4
Output
Surface area of the cube: 96

Approach

  1. A cube has 6 square faces, each of area a².

Solution

Python
a = 4surface_area = 6 * a ** 2print("Surface area of the cube:", surface_area)
Output
Surface area of the cube: 96

Line by line

Line 2

Six faces times the area of one face, a ** 2.

Learn the concept

New to this topic? Read the lesson, then come back.

More basic programs

← All 40 Basic Programs

Area of a circle

Python Practice › Basic Programs › Program 16

Area of a circle

EasyBasic Programs · 16 of 40

Write a Python program to calculate the area of a circle. Area = π × r².

Input
r = 7
Output
Area: 153.94

Approach

  1. Use math.pi for an accurate value of π.
  2. Multiply π by the radius squared.

Solution

Python
import math r = 7area = math.pi * r ** 2print("Area:", round(area, 2))
Output
Area: 153.94

Line by line

Line 4

math.pi is π to 15 decimal places, more accurate than 3.14.

Line 5

round(area, 2) keeps two decimals for display.

Learn the concept

New to this topic? Read the lesson, then come back.

More basic programs

← All 40 Basic Programs