You are on page 1of 3

Q1. Explain use of format () method with example.

Ans - The format() method formats the specified value(s) and insert them inside the
string's placeholder.

The placeholder is defined using curly brackets: {}.

The format() method returns the formatted string.

Syntax
string.format(value1, value2..
.)
The placeholders can be identified using named indexes {price}, numbered indexes {0},
or even empty placeholders {}.

Example
Using different placeholder values:

txt1 = "My name is {fname}, I'm {age}".format(fname = "John", age = 36)


txt2 = "My name is {0}, I'm {1}".format("John",36)
txt3 = "My name is {}, I'm {}".format("John",36)

Q2. Explain package NumPy with example.


Ans -

Example –
import numpy as np

# Create a NumPy array


array = np.array([1, 2, 3, 4, 5])

# Print the array


print(array)

# Get the shape of the array


print(array.shape)

# Get the data type of the array


print(array.dtype)

# Add two arrays


array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])

array3 = array1 + array2

print(array3)

# Multiply two arrays


array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])
array3 = array1 * array2

print(array3)

# Dot product of two arrays


array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])

array3 = np.dot(array1, array2)

print(array3)

You might also like