Example python programs: Functions basics

Simple python programs to practice:

1) Write a Python function Factorial(n) which returns the factorial of the given number(n).

2) Write a Python function, Square(num) which returns the square of the given number(num).

3) Write a Python function, Sum(n) which returns the sum of first n numbers.

4) Write a Python function, Palindrome(num) that accepts an integer num as input and returns true/false based on condition.

    Ex: num = 12321 ---> output = 12321 ----> then given number is palindrome.

5) Write a Python function, check_strong_number(num) that accepts a positive as input, and return true/false based on condition.
  • A number is said to be a Strong number if the sum of the factorial of each digit is equal to the given number.
   Ex: 145 is strong number as (1! =1) + (4! = 24) + (5! = 120) = 145.

Lists in Python

What is List?
  • List is one of the most common collection datatypes in Python which is ordered and mutable (changeable).
  • It is used to store a group of an element together in a sequence.
  • List can store both homogenous and heterogeneous elements i.e., a single list may contain data types like integers, floats, strings as well as objects.
    • Individual value in a list is called as elements.
    • These elements will be stored in a contiguous memory location.
        For Ex:  Number1 = 10
                      Number2 = 20
                      Number3 = 30,  
                      Number4 = 40 
    • These individual variables can be represented in a list as
                      Number_list = [ 10, 20, 30, 40 ]