NAME: MOHAMMED ZAID
ROLL NO.: 234130055
ASSIGNMENT NO. 7
Q.1] Create a python list
Input:
ages = [55, 56, 57]
print(ages)
Output:
[55, 56, 57]
Q.2] Access list element
Input:
languages = ['Python', 'Zaid', 'C++']
print(languages[0])
print(languages[1])
Output:
Python
Zaid
Q.3] Add elements to python list
Input:
fruits = ['apple', 'banana', 'orange']
print('Original List:', fruits)
fruits.append('cherry')
print('Updated List:', fruits)
Output:
Original List: ['apple', 'banana', 'orange']
Updated List: ['apple', 'banana', 'orange', 'cherry']
Q.4] Change list items
Input:
colors = ['Red', 'Black', 'Green']
print('Original List:', colors)
colors[2] = 'Blue'
print('Updated List:', colors)
Output:
Original List: ['Red', 'Black', 'Green']
Updated List: ['Red', 'Black', 'Blue']
Q.5] Remove an Item from the List
Input:
numbers = [2,4,7,9]
numbers.remove(4)
print(numbers)
Output:
[2, 7, 9]
Q.6] Python List length
Input:
cars = ['BMW', 'Mercedes', 'Tesla']
print('Total Elements: ', len(cars))
Output:
Total Elements: 3
Q.7] Iterating through a list
Input:
fruits = ['apple', 'banana', 'orange']
for fruit in fruits:
print(fruit)
Output:
apple
banana
orange
ASSIGNMENT NO. 8
Q.1] Create a python tuple
Input:
numbers = (1, 2, 55)
print(numbers)
Output:
1, 2, 55
Q.2] Access items using index
Input:
languages = ('Python', 'Swift', ‘Zaid')
print(languages[0])
print(languages[2])
Output:
Python
Zaid
Q.3] Tuple cannot be modified
Input:
cars = ('BMW', 'Tesla', 'Ford', 'Toyota')
cars[0] = 'Nissan'
print(cars)
Output:
ERROR!
Q.4] Python Tuple length
Input:
cars = ('BMW', 'Tesla', 'Ford', 'Toyota')
print('Total Items:', len(cars))
Output:
Total Items: 4
Q.5] Iterate through a tuple
Input:
fruits = ('apple','banana','orange')
for fruit in fruits:
print(fruit)
Output:
apple
banana
orange
ASSIGNMENT NO. 9
Q.1] Create a dictionary
Input:
country_capitals = {
"Germany": "Berlin",
"Canada": "Ottawa",
"England": "London"
}
print(country_capitals)
Output:
{'Germany': 'Berlin', 'Canada': 'Ottawa', 'England': 'London'}
Q.2] Access Dictionary Items
Output:
country_capitals = {
"Germany": "Berlin",
"Canada": "Ottawa",
"England": "London"
}
print(country_capitals["Germany"])
print(country_capitals["England"])
Output:
Berlin
London
Q.3] Add items to dictionary
Input:
country_capitals = {
"Germany": "Berlin",
"Canada": "Ottawa",
}
country_capitals["Italy"] = "Rome"
print(country_capitals)
Output:
{'Germany': 'Berlin', 'Canada': 'Ottawa', 'Italy': 'Rome'}
Q.4] Remove dictionary items
Input:
country_capitals = {
"Germany": "Berlin",
"Canada": "Ottawa",
}
del country_capitals["Germany"]
print(country_capitals)
Output:
{'Canada': 'Ottawa'}
Q.5] Clear dictionary items
Input:
country_capitals = {
"Germany": "Berlin",
"Canada": "Ottawa",
}
country_capitals.clear()
print(country_capitals)
Output:
{}
Q.6] Change dictionary items
Input:
country_capitals = {
"Germany": "Berlin",
"Italy": "Naples",
"England": "London"
}
country_capitals["Italy"] = "Rome"
print(country_capitals)
Output:
{'Germany': 'Berlin', 'Italy': 'Rome', 'England': 'London'}
Q.7] Iterate through a dictionary
Input:
country_capitals = {
"United States": "Washington D.C.",
"Italy": "Rome"
}
for country in country_capitals:
print(country)
print()
for country in country_capitals:
capital = country_capitals[country]
print(capital)
Output:
United States
Italy
Washington D.C.
Rome
Q.8] Find dictionary length
Input:
country_capitals = {"England": "London", "Italy": "Rome"}
print(len(country_capitals))
numbers = {10: "ten", 20: "twenty", 30: "thirty"}
print(len(numbers))
countries = {}
print(len(countries))
Output:
2
3
0
ASSIGNMENT NO. 10
Q.1] Classes and Objects
Input:
class person :
def _init_(self,name,age):
self.name = name
self.age = age
p1 = person("zaid" , 18)
print(p1.name)
print(p1.age)
Output:
zaid
18
ASSIGNMENT NO. 11
Q.1] Inheritance of classes
Input:
class Person():
def __init__(self, name, age):
self.name = name
self.age = age
def display(self):
print(self.name, self.age)
class Student(Person):
def __init__(self, name, age):
self.sName = name
self.sAge = age
super().__init__(“Zaid", age)
def displayInfo(self):
print(self.sName, self.sAge)
obj = Student("Yogesh", 17)
obj.display()
obj.displayInfo()
Output:
Zaid 17
Yogesh 17
ASSIGNMENT NO. 12
Q.1] Create a numpy array
Input:
import numpy as np
array1 = np.array([2, 4, 6, 8])
print(array1)
Output:
[2 4 6 8]
Q.2] Create an array using np.zeros
Input:
import numpy as np
array1 = np.zeros(4)
print(array1)
Output:
[0. 0. 0. 0.]
Q.3] Create an array with np.arrange Input:
import numpy as np
array1 = np.arange(5)
print("Using np.arange(5):", array1)
array2 = np.arange(1, 9, 2)
print("Using np.arange(1, 9, 2):",array2)
Output:
Using np.arrange(5): [0 1 2 3 4]
Using np.arrange(1, 9, 2): [1 3 5 7]
Q.4] Create an array with np.random.rand
Input:
import numpy as np
array1 = np.random.rand(5)
print(array1)
Output:
[0.12976068 0.5978689 0.69336894 0.62606041 0.74989593]
Q.5] Create an empty numpy array
Input:
import numpy as np
array1 = np.empty(4)
print(array1)
Input:
[4.96319325e-310 0.00000000e+000 0.00000000e+000
0.00000000e+000]
Q.6] Create a 2D numpy array
Input:
import numpy as np
array1 = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
print(array1)
Output:
[[1 2 3 4]
[5 6 7 8]]
Q.7] Create a 3D numpy array
Input:
import numpy as np
array1 = np.array([[[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]],
[[55, 56, 57, 58],
[59, 60, 61, 62],
[63, 64, 65, 66]]])
print(array1)
Output:
[[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
[[55, 56, 57, 58],
[59, 60, 61, 62],
[63, 64, 65, 66]]])
ASSIGNMENT NO. 13
Q.1] Create a Panda series
Input:
import pandas as pd
data = [10, 20, 30, 40, 50]
my_series = pd.Series(data)
print(my_series)
Output
0 10
1 20
2 30
3 40
4 50
dtype: int64
Q.2] Display value from panda series using labels
Input:
import pandas as pd
data = [10, 20, 30, 40, 50]
my_series = pd.Series(data)
print(my_series[2])
Output:
30
Q.3] Specify labels using panda series
Input:
import pandas as pd
a = [1, 3, 5
my_series = pd.Series(a, index = ["x", "y", "z"])
print(my_series)
Output:
x 1
y 3
z 5
dtype: int64
Q.4] To access elements from panda series
Input:
import pandas as pd
a = [1, 3, 5]ls
my_series = pd.Series(a, index = ["x", "y", "z"])
print(my_series["y"])
Output:
x 1
y 3
z 5
dtype: int64
Q.5] Create Panda dataframe using dictionary
Input:
import pandas as pd
data = {'Name': ['John', 'Alice', 'Bob'],
'Age': [25, 30, 35],
'City': ['New York', 'London', 'Paris']}
df = pd.DataFrame(data)
print(df)
Output:
Name Age City
0 John 25 New York
1 Alice 30 London
2 Bob 35 Paris
Q.6] Create Panda Dataframe using lists
Input:
import pandas as pd
data = [['John', 25, 'New York'],
['Alice', 30, 'London'],
['Bob', 35, 'Paris']]
df = pd.DataFrame(data, columns=['Name', 'Age', 'City'])
print(df)
Output:
Name Age City
0 John 25 New York
1 Alice 30 London
2 Bob 35 Paris
Q.7] Create an empty data frame
Input:
import pandas as pd
df = pd.DataFrame()
print(df)
Output:
Empty DataFrame
Columns: []
Index: []
ASSIGNMENT NO. 14
Q.1] Plot x and y points with a line using matplot
Input:
import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([1, 8])
ypoints = np.array([3, 10])
plt.plot(xpoints, ypoints)
plt.show()
Output:
Q.2] Plot x and y points without a line using matplot
Input:
import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([1, 8])
ypoints = np.array([3, 10])
plt.plot(xpoints, ypoints, 'o')
plt.show()
Output:
Q.3] Plot multiple points with a line using matplot
Input:
import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([1, 2, 6, 8])
ypoints = np.array([3, 8, 1, 10])
plt.plot(xpoints, ypoints)
plt.show()
Output:
Q.4] Plot points using default X points
Input:
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10, 5, 7])
plt.plot(ypoints)
plt.show()
Output: