Skip to main content
Python Beginner Interview Code Examples

Python Beginner Interview Code Examples

This tutorial help to provide some python script questions which are asked in python fresher interview. We’ll provide factorial, Fibonacci and sum number python code with examples.

These are common python code examples that expect to interview the candidates to understand coding skills:

  • Sum of two number
  • Factorial of a number
  • NumPy Factorial
  • Fibonacci series

Write a python code two sum of two number

This example helps to understand, are you have basic syntax knowledge of python:

def sum(a, b):
    return (a + b)

a = int(input('Enter 1st number: '))
b = int(input('Enter 2nd number: '))

print(f'Sum of {a} and {b} is {sum(a, b)}')

Output:

3
Enter 2nd number: 
4
Sum of 3 and 4 is 7


** Process exited - Return Code: 0 **
Press Enter to exit terminal

Factorial of a number in python

A factor is a positive integer. The answer to your factorial question is the sum of all positive integers that are less than or equal to that number. An exclamation point (!) is used to indicate it.

These are very common questions for a fresher developer. Normally, this question is asked in any programming language:

The Example:

n! = n* (n-1) * (n-2) *……..1
4! = 4x3x2x1 = 24

Let’s write a python code to calculate the factorial of the entered number.

n = int(input("Enter a number: "))    

def fact(num):
 if num == 0:
  return 1
 elif num < 0:    
   print(" Factorial does not exist for negative numbers")
   quit()
 else:
  return num * fact(num-1)

print("The factorial of",n,"is",fact(n))    

Input value: -2

Enter a number: 
-2
 Factorial does not exist for negative numbers


** Process exited - Return Code: 0 **
Press Enter to exit terminal

Enter value: 0

Enter a number: 
0
The factorial of 0 is 1


** Process exited - Return Code: 0 **
Press Enter to exit terminal

Input value: 4

Enter a number: 
4
The factorial of 4 is 24

** Process exited - Return Code: 0 **
Press Enter to exit terminal

NumPy Factorial

Numpy.math.factorial() is a mathematical function in python that is used to compute the factorial of a given positive number.

Syntax:

The syntax of the Numpy factorial() function is as follows :

numpy.math.factorial(n)

The parameters are as follows :

n: This is the input integer/number for which the factorial has to be calculated.

Return: The function returns an integer of factorial.

Examples of NumPy Factorial

import numpy as np

n = int(input("Enter a number: "))
factorial = np.math.factorial(n)
print("Factorial of", n, "is :", factorial)

Output:

Enter a number: 
6
Factorial of 6 is : 720


** Process exited - Return Code: 0 **
Press Enter to exit terminal

Fibonacci series in python

This is another common question for a fresher developer, to write a python code that’ll generate n number Fibonancy series.

The Fibonacci sequence was first discovered by Leonardo Fibonacci, who is an Italian mathematician, around A.D. 1170.

In the Fibonacci sequence, each number is the sum of two numbers that precede it. For example:

Example of Fibonacci Series

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……

Let’s write a python code to generate the n number Fibonacci series:

n = int(input("Enter a number: "))

def fib(n):
 a=0
 b=1
 if n == 1:
  print(a)
 else:
  print(a)
  print(b)
  for i in range(2,n):
   c=a+b
   a=b
   b=c
   print(c)

print("The Fibonancy of",n,"is:")
fib(n)

Input value : 10

Enter a number: 
10
The Fibonancy of 10 is:
0
1
1
2
3
5
8
13
21
34


** Process exited - Return Code: 0 **
Press Enter to exit terminal

Leave a Reply

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