Saturday, 4 December 2021

Write a program to demonstrate working with Vectors, Lists in PYTHON

 Lists are used to store multiple items in a single variable.

Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.

The predefined methods in list are more powerful to perform different operations, they are listed below

The following program show working of various above methods.

# Python program to demonstrate
# Creation of List
	
# Creating a List
List1 = []
print("Blank List: ")
print(List1)

# Creating a List of numbers
List2 = [10, 20, 14]
print("\nList2 of numbers: ")
print(List2)

# Creating a List of strings and accessing
# using index
List3 = ["Geeks", "For", "Geeks"]
print("\nList3 Items: ")
print(List3[0])
print(List3[2])

# Creating a Multi-Dimensional List
# (By Nesting a list inside a List)
List4 = [['Geeks', 'For'] , ['Geeks']]
print("\nMulti-Dimensional List4: ")
print(List4)

# list operations

# 1. finding the lenght of string 
print("\n Length of list List1 is: ")
print(len(List1))

print("\n Length of list List2 is: ")
print(len(List2))

print("\n Length of list List3 is: ")
print(len(List3))

print("\n Length of list List4 is: ")
print(len(List4))

#2. Append operations 

List1.append(1)
List1.append(2)
List1.append(4)
print("\nList1 after Addition of Three elements: ")
print(List1)

# Using insert() method
# Addition of Element at
# specific Position
# (using Insert Method)
List1.insert(3, 12)
List1.insert(0, 'Geeks')
print("\nList1 after performing Insert Operation: ")
print(List1)

# Addition of multiple elements
# to the List at the end
# (using Extend Method)
List1.extend(['be', 'Happy', 'Always'])
print("\nList1 after performing Extend Operation: ")
print(List1)


#3. Using remove() method

# Removing elements from List
# using Remove() method
List1.remove(1)
List1.remove(2)
print("\nList1 after Removal of two elements: ")
print(List1)

#Using pop() method

# Removing element from the
# Set using the pop() method
List1.pop()
print("\nList1 after popping an element: ")
print(List1)
 
# Removing element at a
# specific location from the
# Set using the pop() method
List1.pop(1)
print("\nList1 after popping a specific element: ")
print(List1)

The following is the output of above program

Blank List: 
[]

List2 of numbers: 
[10, 20, 14]

List3 Items: 
Geeks
Geeks

Multi-Dimensional List4: 
[['Geeks', 'For'], ['Geeks']]

 Length of list List1 is: 
0

 Length of list List2 is: 
3

 Length of list List3 is: 
3

 Length of list List4 is: 
2

List1 after Addition of Three elements: 
[1, 2, 4]

List1 after performing Insert Operation: 
['Geeks', 1, 2, 4, 12]

List1 after performing Extend Operation: 
['Geeks', 1, 2, 4, 12, 'be', 'Happy', 'Always']

List1 after Removal of two elements: 
['Geeks', 4, 12, 'be', 'Happy', 'Always']

List1 after popping an element: 
['Geeks', 4, 12, 'be', 'Happy']

List1 after popping a specific element: 
['Geeks', 12, 'be', 'Happy']

Procedure to create vectors in Python

A vector in a simple term can be considered as a single-dimensional array. With respect to Python, a vector is a one-dimensional array of lists. It occupies the elements in a similar manner as that of a Python list.

Creation of a Vector in Python

Python NumPy module is used to create a vector. We use numpy.array() method to create a one-dimensional array i.e. a vector.
numpy.array(list)

import numpy as np 
 
lst1 = [10,20,30,40,50] 
 #: Horizontal Vector
vctr1 = np.array(lst1) 

print("Vector created from a list1:") 
print(vctr1) 

#Vertical Vector
lst2 = [[2], 
        [4], 
        [6],
          [10]]  
 
vctr2 = np.array(lst2) 
print("Vector created from a list2:") 
print(vctr2) 
vctr_add = vctr1+vctr2
print("Addition of two vectors:\n ",vctr_add)
vctr_sub = vctr1-vctr2
print("Subtraction of two vectors:\n ",vctr_sub)
vctr_mul = vctr1*vctr2
print("Multiplication of two vectors: \n",vctr_mul)
vctr_div = vctr1/vctr2
print("Division of two vectors:\n ",vctr_div)

The following is the output of the above program

Vector created from a list1:
[10 20 30 40 50]
Vector created from a list2:
[[ 2]
 [ 4]
 [ 6]
 [10]]
Addition of two vectors:
  [[12 22 32 42 52]
 [14 24 34 44 54]
 [16 26 36 46 56]
 [20 30 40 50 60]]
Subtraction of two vectors:
  [[ 8 18 28 38 48]
 [ 6 16 26 36 46]
 [ 4 14 24 34 44]
 [ 0 10 20 30 40]]
Multiplication of two vectors: 
 [[ 20  40  60  80 100]
 [ 40  80 120 160 200]
 [ 60 120 180 240 300]
 [100 200 300 400 500]]
Division of two vectors:
  [[ 5.         10.         15.         20.         25.        ]
 [ 2.5         5.          7.5        10.         12.5       ]
 [ 1.66666667  3.33333333  5.          6.66666667  8.33333333]
 [ 1.          2.          3.          4.          5.        ]]


0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top