9 Basic Python Programs You Can’t Miss

basic python programs

Are you in search of basic Python programs? Your search ends here. To execute below programs you can use Online Python IDE.

Basic Python Programs 1: Input two integers and code for arithmetic operator and perform operation accordingly.

a = int(input('Enter first number: '))
b = int(input('Enter second number: '))
c = int(input('Enter code (1: add, 2: sub, 3: mul, 4: div, 5: mod): '))
r = 0

if c == 1:
    r = a + b
elif c == 2:
    r = a - b
elif c == 3:
    r = a * b
elif c == 4:
    r = a / b
elif c == 5:
    r = a % b
else:
    print('Invalid input')

print('Answer is', r)

Output:
Enter first number: 
1
Enter second number: 
2
Enter code (1: add, 2: sub, 3: mul, 4: div, 5: mod): 
1
Answer is 3

Basic Python Programs 2: Print numbers 1…50 and also calculate their sum

# Initialize a variable to store the sum
total_sum = 0

# Loop through numbers from 1 to 50
for i in range(1, 51):
    print(i)
    total_sum += i  # Add the current number to the sum

# Print the total sum
print("The sum of numbers from 1 to 50 is:", total_sum)

Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
The sum of numbers from 1 to 50 is: 1275

Basic Python Programs 3: Print numbers 1…100. Change line after each 10 numbers.

for i in range(1, 101):
    print(i, end=' ')
    if i % 10 == 0:
        print()
        
Output:

1 2 3 4 5 6 7 8 9 10 
11 12 13 14 15 16 17 18 19 20 
21 22 23 24 25 26 27 28 29 30 
31 32 33 34 35 36 37 38 39 40 
41 42 43 44 45 46 47 48 49 50 
51 52 53 54 55 56 57 58 59 60 
61 62 63 64 65 66 67 68 69 70 
71 72 73 74 75 76 77 78 79 80 
81 82 83 84 85 86 87 88 89 90 
91 92 93 94 95 96 97 98 99 100

Basic Python Programs 4: Print rectangular pattern of stars

width = 5  # Specify the width of the rectangle
height = 4  # Specify the height of the rectangle

for i in range(height):
    for j in range(width):
        print("*", end=" ")
    print()  # Move to the next line after each row
    
Output:

* * * * * 
* * * * * 
* * * * * 
* * * * * 

Basic Python Programs 5: Print triangular pattern of stars

height = 5  # Specify the height (number of rows) of the triangle

for i in range(1, height + 1):
    for j in range(i):
        print("*", end=" ")
    print()
    
Output:

* 
* * 
* * * 
* * * * 
* * * * * 

Basic Python Programs 6: Print triangular pattern of stars in decreasing order.

height = 5  # Specify the height (number of rows) of the triangle

for i in range(height, 0, -1):
    for j in range(i):
        print("*", end=" ")
    print()
    
Output:

* * * * * 
* * * * 
* * * 
* * 
*

Basic Python Programs 7: Print n terms of Fibonacci series.

n = int(input("Enter the number of terms in the Fibonacci series: "))

# Initialize the first two terms of the series
a, b = 0, 1

# Check if the number of terms is valid
if n <= 0:
    print("Please enter a positive integer.")
elif n == 1:
    print("Fibonacci Series:")
    print(a)
else:
    print("Fibonacci Series:")
    print(a)
    print(b)

    for _ in range(2, n):
        # Calculate the next term
        next_term = a + b

        # Print the next term
        print(next_term)

        # Update a and b for the next iteration
        a, b = b, next_term
        
Output:

Enter the number of terms in the Fibonacci series: 
15
Fibonacci Series:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

Basic Python Programs 8: Check whether entered number is single digit even number.

num_str = input("Enter a number: ")

# Check if the input is a single character and a digit
if len(num_str) == 1 and num_str.isdigit():
    num = int(num_str)
    
    # Check if it's an even number
    if num % 2 == 0:
        print("The entered number is a single-digit even number.")
    else:
        print("The entered number is not an even number.")
else:
    print("Please enter a single-digit number.")
    
Output:

Enter a number: 
2
The entered number is a single-digit even number.


Enter a number: 
3
The entered number is not an even number.

Basic Python Programs 9: Print alphabets a…z

# Using a for loop
for letter in range(ord('a'), ord('z') + 1):
    print(chr(letter), end=' ')
    
Output:
a b c d e f g h i j k l m n o p q r s t u v w x y z

One thought on “9 Basic Python Programs You Can’t Miss

Leave a Reply

Your email address will not be published. Required fields are marked *