Category: Python

Simple Calculator Program in Python

Here we are going to implement simple calculator program in Python. We are going to implement Addition, Substraction, Multiplication and Division.

Simple Calculator Program in Python

# Simple Calculator for Add, Sub, Mul and Div

def Addition (num1,num2):
return num1+num2

def Substraction (num1,num2):
return num1-num2

def Multiplication (num1,num2):
return num1*num2

def Division (num1,num2):
return num1/num2

print("Enter two number for calculation")

num1 = int(input("Enter a number1: "))
num2 = int(input("Enter a number2: "))

print("Enter your choice \n 1. Addition \n2. Substraction \n3. Multiplication \n4.Division")
choice = int(input())

if choice == 1:
elif choice ==2:
elif choice ==3:
elif choice == 4:
Filed under: Python

Python Program for Factorial Using Loop and Recursive Function

Python Program for Factorial Using Loop and Recursive Function

Factorial of n is the product of all positive descending integers. Factorial of n is denoted by n!.

Here, 4! is pronounced as “4 factorial”, it is also called “4 bang” or “4 shriek”.

The factorial is normally used in Combinations and Permutations (mathematics).

There are many ways to write the factorial program in Python language. Let’s see the 2 ways to write the factorial program in Python.

  • Factorial Program using loop
  • Factorial Program using recursion

Factorial Using Loop in Python

num = int(input("Enter a number: "))
fact = 1

for i in range(1,num+1):
fact = fact * i

print("Factorial of ",num, "is :",fact)

Factorial Using Recursive Function in Python

# Factorial using Recursion 

def fact_recur(n):
if n==0:
return 1

num = 4
print("Factorial of",num,"is",fact_recur(num))
Filed under: Python

Python Program to Display Fibonacci Sequence Using Recursive Function and Loop

Fibonacci Series using Loop and Recursion

In Fibonacci Series, next number use the sum of the previous two numbers. For example: 0, 1, 1, 2, 3, 5 , 8, 13, 21, 34, 55 and so on. Fibonacci series starts from 0 and 1.

Fibonacci application are in Stock Market, Trading, Banking Application, Data structure, Graphs and much more.

The sequence Fn of Fibonacci numbers is defined by the recurrence relation:
Fn = Fn-1 + Fn-2

There are two ways to write the Fibonacci Series program in Python:

  1. Fibonacci Series using Loop
  2. Fibonacci Series using recursion

Fibonacci Series using Loop

nNum = 10

num = 0
num1 = 0
num2 = 1

count = 0

while (count<nNum):
    num = num1 +num2
    num1 = num2
    num2 = num
    count +=1


Fibonacci Series using Recursion (Recursive Function)

# Recurrence Relation
# Fn = Fn-1 + Fn-2

def recur_fibo(n):
    if n <= 1:
        return n

nNum = 10
for i in range(nNum):
Filed under: Python

Python – Try…Except…Finally

Python provides two very important features to handle any unexpected error in your Python programs and to add debugging capabilities in them.

The try block lets you test a block of code for errors.

The except block lets you handle the error.

The finally block lets you execute code, regardless of the result of the try- and except blocks.

Exception Handling In Python

When an error, Python program will stop and generate error. These errors can be handled using try except Statements

  print("An exception occurred")

We can raise a message using error names. Example – NameError

except NameError:
print("Variable x not defined")
print("Something went wrong!!!")

Else in Try Except

You can use the else keyword to define a block of code to be executed if no errors were raised:

  print("Something went wrong")
  print("Nothing went wrong")

Finally in Try Except

The finally block, will be executed no matter what happens to try except block:

except NameError:
print("Variable x not defined")
print("Something went wrong!!!")
print("The try-except is finished")
Filed under: Python

Python – Modules

Python Module can be thought as package in Java.  A module allows you to logically organize your Python code. Consider a module to be the same as a code library. We can store Functions, Variables, Classes in Module.

Built-In Modules

Python have built-in module which we can use according to our needs. For example – 

import platform

a = platform.system()

Create a Module

To create a module just save the code you want in a file with the file extension

def greeting():
print("Hello from module")

person = {"name":"Huzaif","age":22,"country":"India"}

Above we have created a function and dictionary variable. Save the file in the same folder.

Import Statement

The import statement help to import or use the module which we have created. Use module file name. Example : mymodule

import mymodule

mymodule.greeting("name") # Accessing Function from
print(mymodule.person["age"]) # Accessing Variable from

Renaming Module name

You can rename module name by using as keyword

import mymodule as mm
a = mm.person1["age"]

Accessing Specific Part from Module

For Accessing Specific Part from Module we use from keyword

from mymodule import person

print(person["age"]) # we have accessed only person variable
Filed under: Python