×

BASIC PYTHON PROGRAM FOR BEGINNERS

Introduction

To begin with, Python is the most in-demand programming language which is easiest to learn for beginners. This basic python program article is framed to help beginners to understand the basic concepts of python with programs and solutions. Python certification course in Chennai at Credo Systemz is a practical oriented training course which starts from the basic with the help of professional expert trainers.

Basic python programs

  • Python program to perform basic arithmetic operations
  • Python program to find the factorial of a given number
  • Python program to find the simple and compound interest
  • Python program to check whether the given number is prime or composite
  • Python program to find the biggest of three numbers
  • Python Program to display the multiplication Table
  • Python Program to Check Armstrong Number
  • Python Program to Check Whether the given String is Palindrome or Not

Python program to perform basic arithmetic operations

Basic Arithmetic operations

  • Addition
  • Subtraction
  • Multiplication
  • Division
Program

#Getting input from user
number1 = input("Enter the First number: ")
number2 = input("Enter the Second number: ")
 # Addition
sum = number1 + number2
 # To display the sum
print("Addition:" ,sum)
# Subtraction
sub = number1 -number2
# To display the subtraction
print("Subtraction:" ,sub)
# Multiplication
multiply = number1 *number2
# To display the multiplication
print("Multiplication:" ,multiply)
# Division
divide = number1 + number2
# To display the sum
print("Division:" ,divide)
Output

Enter the First number: 4

Enter the Second number: 2

Addition: 6

Subtraction: 2

Multiplication: 8

Division: 2

Python program to find the factorial of a given number

A factorial of a given number calculated by findingthe product of all the numbers from 1 to the user specified number.

Using traditional iterationmethod


# factorial of given number
def factorial(num):
    ifnum< 0:
        return 0
    elifnum == 0 or num == 1:
        return 1
    else:
        fact = 1
        while(num> 1):
            fact= fact * num
            num=num - 1
        return fact
#Getting input from user
num = input("Enter the Given number: ")
print("Factorial:",factorial(num))

Output Enter the Given number: 3

Factorial: 6

Using in-built function


import math
#Getting input from user
num = input("Enter the Given number: ")
#in-built function
fact=math.factorial(num)
print("Factorial of given number:",fact)
 
Output Enter the Given number: 4

Factorial of given number: 24

Python program to find the simple and compound interest


P = principle amount
T = time period
R = rate of interest
A =amount 
T =time span
Simple interest formula: 
(P x T x R)/100
Compound interest formula: 
A = P(1 + R/100) t 
Compound Interest = A – P 
#Getting input from user
p = input("Enter the Principle amount: ")
t = input("Enter the time period: ")
r = input("Enter the rate of interest: ")
#to calculate simple interest
SI = (p * t * r)/100
print('Simple Interest:', SI)
#to calculate compound interest
Amount = p * (pow((1 + r/ 100), t))
CI = Amount - principle
print("Compound Interest:", CI)
Output Enter the Principle amount: 5000

Enter the time period: 2

Enter the rate of interest: 5

Simple Interest: 500

Compound Interest: 5500

Python program to check whether the given number is prime or composite

The prime number is a whole number with two integral divisors i.e., divisible by 1 and itself. The composite number is a whole number with more than two integral divisors.

#Getting input from user
num = int(input("Enter a number : "))
#to check prime or composite
ifnum == 0 or 1:
print(num, "is a neither prime NOR composite number")
elifnum> 1:
fori in range(2, num):
if (num % i) == 0:
print(num, "is NOT a prime number")
break
else:
print(num, "is a PRIME number")
else:
print(num, "is a COMPOSITE number")
Output Enter a number: 3

3 is a PRIME number

Python program to find the biggest of three numbers


# Getting input from user
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
num3 = int(input("Enter third number: "))
#to find the biggest number
if (num1 >= num2) and (num1 >= num3):
big = num1
elif (num2 >= num1) and (num2 >= num3):
big = num2
else:
big = num3
print("The largest of three numbers is",big)
Output Enter first number: 12

Enter second number: 17

Enter third number: 25

The largest number is 25

Python Program to Display the multiplication Table

To displays the multiplication table of variable num (from 1 to 10).

# To take input from the user
number = int(input("Enter the number for multiplication table : "))
# Iterate 10 times from i = 1 to 10
fori in range(1, 11):
print(number, 'x', i, '=', number*i)
Output Enter the number for multiplication table: 2

2 x 1 = 2

2 x 2 = 24

2 x 3 = 36

2 x 4 = 48

2 x 5 = 60

2 x 6 = 72

2 x 7 = 84

2 x 8 = 96

2 x 9 = 108

2 x 10 = 120

Python Program to Check Armstrong Number

Armstrong number is a number equal to the sum of the cubes of its own digits. For example, 370 is an Armstrong number since 153 = 1*1*1 + 5*5*5 + 3*3*3.

# Getinput from the user
num = int(input("Enter a number: "))
sum = 0
# Tofind the sum of the cube of each digit
tem = num
while tem> 0:
digit = tem % 10
sum += digit ** 3
tem //= 10
ifnum == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
Output

Enter a number: 370

370 is an Armstrong number

Python Program to Check Whether a Given String is Palindrome or Not

________________________________________A palindrome is a string that is the same when read forward and backward as well. For example: mom, madam,pop,radar

# Getting input from user
string=input(("Enter a string:"))
if(string==string[::-1]):
      print("The string is a palindrome")
else:
      print("Not a palindrome")
			
Output

The string is a palindrome.

Conclusion

These basic python programs helps to understand about how to write simple python programs, data types, operations and to learn, explore more.

Related Article