Pages

Tuesday, August 13, 2019

Write simple programs to convert U.S. dollars to Indian rupees

dollar = float(input("Please enter number of dollars:"))
rupee = dollar * 71.12
print(" Rupees:", rupee)

#It display following output
Please enter number of dollars:1 Rupees: 71.12

write a python program to calculate the area and perimeter of the square

# write a python program to calculate the area and perimeter of the square

l=int(input("Enter the Side of Square: "))
square_area=l*l
square_perimeter=4*l
print("Area of Square : ",square_area)
print("Perimeter of Square : ",square_perimeter)


# It display following output
Enter the Side of Square: 4
Area of Square :  16
Perimeter of Square :  16

Numpy in Python

Numpy

  • NumPy is nothing but the library of Linear Algebra  for Python, the main reason is that it is more important in Data Science using Python.
  • Numpy is mathematical function mainly use the array method instead of lists, to perform the faster operation.
First we will study the basics of Numpy, to get it first started with its install it
If you have installed Anaconda already then simply type following command in your Anaconda prompt:
conda install numpy
After installation of Numpy the first step is to import the Numpy library 
using following command:
import numpy as np

Numpy Arrays

In NumPy the arrays are the main data type in these library. Numpy arrays mainly divided into     in two types: 
1. Vectors: Vectors are nothing but the 1-d arrays. 
2. Matrices: Matrices are nothing but the 2-d arrays.

Creating Numpy Arrays

From a Python List

Ex.1:
my_list1 = [1,2,3]
np.array(my_list1)
It displays following output:
array([1, 2, 3])
Ex.2:
my_matrix1 = [[1,2,3],[4,5,6],[7,8,9]]
np.array(my_matrix1)
It displays following output:
array([[1, 2, 3],[4, 5, 6],[7, 8, 9]])

Built-in Methods

There are many built in types to generate Arrays.

1. arange

In this method, it return evenly spaced values within a given interval.
import numpy as np
np.arange(0,10)
It displays following output:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.arange(0,11,2)
It displays following output:
array([ 0,  2,  4,  6,  8, 10])

2. zeros and ones

This method is used to generate arrays of zero's or one's.
For example:
import numpy as np
np.zeros((5,5))
It displays following output:
array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]])

3. linspace

It return evenly spaced numbers over a given interval.
For example:
import numpy as np
np.linspace(0,15,4)
It displays following output:
array([ 0.,  5., 10., 15.])

4. eye

It is used to creates an identity matrix.
For example:
import numpy as np
np.eye(3)
It displays following output:
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])

5. Random

In Numpy there are many ways to create a random number arrays:

rand

It is used to create an array of given shape and elaborate it with random samples from a uniform distribution over (0,1).
For example:
import numpy as np
np.random.rand(3,2)
It display following output:
array([[0.23440635, 0.37017193],
       [0.4850963 , 0.45149511],
       [0.94609105, 0.15344959]])

Array Attributes and Methods:

Let us discuss with some important array attributes and methods:

1. Reshape

It returns an array that containing the same data with a new shape.
For example:
import numpy as np
arr = np.arange(20)
arr.reshape(5,4)
It display following output:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19]])

2. min, max, argmin,argmax

In this method, it is used to find min or max values. Also argmin and argmax used to find their index locations.
For example:
import numpy as np
a = np.array([1, 2, 3, 4, 5])
a
It displays following output:
array([1, 2, 3, 4, 5])
a.min()
1
a.max()
5
a.argmin()
0
a.argmax()
4

Monday, August 12, 2019

Function in Python

Functions in Python:

·         A function is a organized and reusable block of code which run when it called. In this you can pass data which is known as parameter into the function.
·         For creating a function use def keyword, for example:
def function_name ( )
            print(“ Hello function1”)
·         For example:
1.      To find the factorial of given number using function in Python.
def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)
n=int(input("Input a number to compute the factorial : "))
print(factorial(n))
                        
                         It display following output:

                         Input a number to compute the factorial : 4
                         24
 
 
2.      To find the Fibonacci series of given number using function in Python.
def fibonacci(n):
    if(n <= 1):
        return n
    else:
        return(fibonacci(n-1) + fibonacci(n-2))
n = int(input("Enter number of terms:"))
print("Fibonacci sequence:")
for i in range(n):
    print(fibonacci(i))
                              It display following output:
                              
                               Enter number of terms: 5
                               Fibonacci sequence:
0
1
1
2

3

Decision Making and Loops in Python


Decision Making:
Decision making evaluates the multiple conditions that will produce TRUE or FALSE as an output. Following figure shows the typical structure of decision making:





Python programming language having following types of decision making statements,
  1.      If statement
  2.    If … else statement
  3.    Nested if statement

  1.        If statement :
It is used for decision making and run the body of the code when if statement is true.
Syntax:
            If expression:
                        statement(s)
Example:
            a=10
            b=8
if a>b:
            print(“a is greater than b”)


 2.      If … else statement:

In this type else statement is combined with an if statement. It is mainly used to execute Boolean conditions like if for true and else for false.

Syntax:
if expression:
            statement(s)
else:
            statement(s)

Example:
mark = int(input("Enter the mark of student:"))
if mark >= 40:
    print("Student is passed")
else:
    print("Student is failed")

Above code produce following output:

Enter the marks of student: 38

Student is failed


3.      Nested if statement:
When there are more than two conditions are presents, in that situation nested if statements are used.
Syntax:
if expression1:
    statement(s)
    if expression2:
      statement(s)
    elif expression3:
      statement(s)
    else
      statement(s)
elif expression4:
    statement(s)
else:
    statement(s)

Example:
if 1 = = 2:
    print('first')
elif 3 = = 3:
    print('middle')
else:
    print('Last')

Above code produce following output:
middle


Loops in Python:
In Python there are following types of loops are mainly used:
  1.       For loop
  2. While loop

 1. For loop: It executes a sequence of statements multiple times up to the given condition satisfied.
For example:
seq = [1,2,3,4,5]
for item in seq:
    print(item)

It displays following output:

1
2
3
4
5


2. While loop: It repeats a given one or more statements while a given condition becomes true.
Syntax:
while expression:
statement(s)
Example:
i = 1
while i < 4:
    print('i is: {}'.format(i))
    i = i+1

It displays following output:
                    i is: 1
                    i is: 2
                    i is: 3


Operators in Python


Operators in Python

In python, following operators are generally used:
1.      Comparison Operator
2.      Logical Operator

1.      Comparison Operator:
In Python, there are following types of comparison operators:

                    i.            Less than (< ) Operator
                  ii.            Greater than (>) Operator
                iii.            Less than or Equal to (<= ) Operator
                iv.            Greater than (>=) Operator

i.                    Less than (< ) Operator:
·         It checks left value is less than that of right value. If it is less then output is true otherwise output is false.
·         For example:
>>> 1 < 2
      Output is true, because 1 is less than 2.

      Suppose consider another example:
>>> 4 < 2
     Output if false, because 4 is not less than 2.

ii.                  Greater than (>) Operator:
·         It checks left value is greater than that of right value. If it is greater then output is true otherwise output is false.
·         For example:
>>> 2 > 1
      Output is true, because 2 is greater than 1.

      Suppose consider another example:
>>> 2 > 4
     Output if false, because 2 is not greater than 4.

iii.                Less than (<= ) Operator:
·         It checks left value is less than or equal to that of right value. If it is less or equal then output is true otherwise output is false.
·         For example:
>>> 1 < = 4
      Output is true, because 1 is less than 4.

      Suppose consider another example:
>>> 2 <= 2
     Output if true, because 2 is equal to 2.

iv.                Greater than (>= ) Operator:
·         It checks left value is greater than or equal to that of right value. If it is greater or equal then output is true otherwise output is false.
·         For example:
>>> 4 > = 1
      Output is true, because 4 is greater than 1.

      Suppose consider another example:
>>> 2 >= 3
     Output if false, because 2 is not greater or equal to 3.


2.      Logical Operator:
Following table describes the Logical Operators in Python:

Sr. No.
Operator
Meaning
Example
1
AND
If both the operands are true then condition
becomes true otherwise false.
(x and y) is
False.
2
OR
If any of the two operands are non-zero then condition becomes true otherwise false.
(x or y) is
True.
3
NOT
Used to reverse the logical state of its operand.
Not(x and y)
is True.

Consider the following output for Logical Operator in Jupyter tool: