Friday, 13 January 2017

How to find the largest number from a list of numbers in Python?

Below script will take the list of integers and find and print the largest number. Here we taking the input as a space separated integers and then using split() method to convert them to a list of integers.

Code:


def findLargest(numList):
 max = numList[0]
 for num in numList:
  if num > max:
   max = num
 return max

print 'Enter the list of numbers Seperated by Space:'
userInput = raw_input()

numlist = [int(n) for n in userInput.split()]

print 'Largest Number : {0}'.format(findLargest(numlist))

Execution: 

Thursday, 12 January 2017

How to calculate Factorial of number in python using Recursion, Iteration and Memoization

This script defines three methods to calculate the factorial of a number. First method uses Recursion , second one uses the Iteration using a for loop and third one uses Memoization(Dynamic Programming).

Code:

def FactorialRecursive(num):
 if num < 0:
  print 'Invalid Input'
  return -1
 if num== 0:
  return 1
 return num*FactorialRecursive(num-1)


def FactorialIterative(num):
 if num >= 0:  
  fact = 1
  for numbers in range(1,num+1):
   fact = fact* numbers
  return fact
 else:
  print 'Invalid Input'
  return -1

def FactorialDP(num):
 if num < 0:
  print 'Invalid Input'
  return -1

 result = [1]*20
 for numbers in range(1,num+1):
  result[numbers] = numbers * result[numbers-1]

 return result[num]
 
#Take user input and call the Factorial function
userNum = int(raw_input('Enter the Number : '))

print 'Factorial using Recursive Method for {0} : {1}'.format(userNum, FactorialRecursive(userNum))
print 'Factorial using Iterative Method for {0} : {1}'.format(userNum, FactorialIterative(userNum))
print 'Factorial using Dynamic Programming Method for {0} : {1}'.format(userNum, FactorialDP(userNum))

Execution:


How to Reverse a number in Python using a simpe function

Here you will look at how to define a very basic function which takes a input from the user - a number and print the reverse of the input.

Code:

def ReverseNum(num):
 reverse = 0
 while num >0:
  reminder = num % 10;
  reverse = (reverse * 10) + reminder
  num = num/10;

 return reverse 


print 'Enter the number'
num = int(raw_input())
print ReverseNum(num)

Execution:

Wednesday, 11 January 2017

How to use if-elif-else in Python using the FizzBuzz problem

What is fizzbuzz problem?

It is one of the popular and very simple interview questions for developers. FizzBuzz  problem is to write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".

Code:


print 'Showing fizzBuzz numbers from 1 to 20'
for num in range(1,20):
 if num%3== 0 and num%5 == 0:
  print 'fizzbuzz'
 elif num%3 ==0:
  print 'fizz'
 elif num%5 ==0:
  print 'buzz'
 else:
  print num 

Execution:

 

Monday, 9 January 2017

How to Sum two numbers in python


Why Learn Python


print 'hello world!'

Yes, that's all it takes to write your first program in python. True, it is a simple yet powerful language. All those things which you did with batch files, cmdlets, shell scripts, perl scripts can be done in python in a easy way.  And a lot more! Not only scripting, its already proved itself as a server side development language for websites. eg. Big part of Instagram(Django), Quora(Pylons) like sites are written in python. It has already become a hot language for Data science and analysis as it has good modules like scipy\ Pandas etc. And, a heaven for machine learning beginners. It's good to learn python in 2017. It will be there in 2018 and many years to come. So, Happy learning.


Objectives
This is the first Post. I will keep it simple. After reading this post, you should be able to create a very basic python script which takes two numbers and displays their sum.


Code

print 'Enter the first number'
num1 = input()

print 'Enter the second number'
num2 = input()

sum = int(num1) + int(num2)

print 'Sum is {0}'.format(sum)

Open SubLimeText or NotePad++ editor and Save this little code as first.py( any filename).

Then, go to the folder containing the file. Open console and type-
python first.py

This will execute your program. It will ask for two numbers and show the sum.

What have you learned here?

a) Printing a text on screen.
b) Taking input from user using input().
c) Converting a string to a number using int().
d) Showing the formatted output using format().

 Same program can be written in one line as well( You will find people doing this in many places so..)



print 'sum is {0}'.format(int(input('Enter the first number')) + int(input('Enter the second number')))