Python Tutorial

Week - 8:

  1. Import numpy, Plotpy and Scipy and explore their functionalities.

Answer:

import numpy as np

a = np.array([0, 10, 20, 30, 40])

a

a[:]

a[1:3]

a[1] = 15

a

b = np.arange(-5, 5, 0.5)

b

b**2

1/b

1/b[10]

Output:

 

 

  1. a) Install NumPy package with pip and explore it.

Answer:

 

 How to Install NumPy in python on Windows Operating System?

 

Numpy is an open source object oriented - python programming. While installing one of the important strong python packages like pip and conda on many different operating systems like Linux, MacOS, and Windows. Python has a large number of additional packages that may be installed and used based on your needs.

 

Following Steps for Installing NumPy

 

Step:1. To run the NumPy program, first Numpy needs to be installed.

Step:2. Python should be installed before installing NumPy.

Step:3. Download and Install Python.

Step:4. After installing Python , Numpy is installed from www.python.org  using pip and conda by running different commands on different operating systems. Many important packages are automatically installed within the Numpy library. 

 

 Installing NumPy on Windows Operating System

 

 Step:1. Command pip install numpy

 

Step:2. verify weather Numpy

Here pip show numpy

 

 

 

Step: 3. Python         

 

 

 

Step:4. Import Numpy as np

 

 

 

Step:5. Upgrade Numpy

Here, pip install –upgrade numpy

 

 

 

  1. Write a program to implement Digital Logic Gates – AND, OR, NOT, EX-OR

Answer:

def OR(A, B):                       #define the OR function

    return A | B                    

def AND(A,B):                    #define the AND function

    return A&B

#perform operation and display the result

print("Output of 0 OR 0 is", OR(0,0))

print("Output of 0 OR 1 is", OR(0,1))

print("Output of 1 OR 0 is", OR(1,0))

print("Output of 1 OR 1 is", OR(1,1))

print("Output of 0 AND 0 is", AND(0,0))

print("Output of 0 AND 1 is", AND(0,1))

print("Output of 1 AND 0 is", AND(1,0))

print("Output of 1 AND 1 is", AND(1,1))

Output:

Output of 0 OR 0 is 0

Output of 0 OR 1 is 1

Output of 1 OR 0 is 1

Output of 1 OR 1 is 1

Output of 0 AND 0 is 0

Output of 0 AND 1 is 0

Output of 1 AND 0 is 0

Output of 1 AND 1 is 1

 

Explanation:

 

  1. This program is used to perform OR and AND operations and display the result.
  2. OR (A,B) is a function used to perform or operation .
  3. AND (A,B) is a function to define and operation.
  4. The output for the bits 0’s and 1’s are calculated by calling OR(A,B) and AND(A,B) functions.
  5. The result is displayed by print statements.

 

  1. Write a program to implement Half Adder, Full Adder, and Parallel Adder

Answer:

def getResult(A,B):                       #defining getResult function

 

# Calculating value of sum

    Sum =A^ B

# Calculating value of Carry

    Carry=A& B

#printing the values

    print("Sum ", Sum)

    print("Carry",Carry)

 

#Drivercode

A =0

B=1

# passing two inputs of halfadder as arguments to get result function

getResult(A,B)

Output:

Sum  1

Carry 0

Explanation:

 

  1. This program is used to implement half adder.
  2. A and B are variables that holds binary digits.
  3. getResult(A,B) is a function used to perform the operation and display sum and carry values.
  4. A^B is used to perform XOR operation between A and B bits and the result will be stored in Sum variable.
  5. A&B is used to perform AND operation between A and B bits and the result is stored in Carry variable.
  6. The result of Sum and Carry are displayed by using print function.

 

  1. Write a GUI program to create a window wizard having two text labels, two text fields and two buttons as Submit and Reset.

Answer:

 

from tkinter import *

win=Tk() #creating the main window and storing the window object in 'win'

 

Label(win, text='Name',fg='red',bd=5).grid(row=0)

Label(win, text='Email',font=("time new roman",18)).grid(row=1)

ent1= Entry(win)

ent2 = Entry(win)

ent1.grid(row=0, column=1)

ent2.grid(row=1, column=1)

win.geometry('300x100') #setting the size of the window

win.mainloop()

 

Output:

 

Explanation:

 

  1. This program is used to create a window and place objects on the window.
  2. tkinter is a module used to implement GUI(Graphics User Interface) in python.
  3. This framework provides Python users with a simple way to create GUI elements using the widgets found in the Tk toolkit.
  4. Win is an object of tkinter used to create window and store objects in the window.
  5. Label is a widget that is used to display boxes where you can place text or images.
  6. bd is used to set the size of the border .
  7. fg is used for coloring bitmap labels or text . 
  8. grid() will determine the row and column place where the object has to be placed.
  9. Text=’’ will determine the name of the object.
  10. font=("time new roman",18) will determine the type of the font and the size of the text.
  11. ent1 and ent2 are the objects that are to be placed on win object.
  12. By using geometry() we can set the size of the window.
  13. By using mainloop() we can update the main window when ever we add objects to the window.