0% found this document useful (0 votes)
18 views

Python Out Put

The document contains Python code snippets and output for problems involving lists, strings, and NumPy arrays. It includes examples of using map, filter, and NumPy functions to manipulate and calculate properties of data structures.

Uploaded by

Shivam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Python Out Put

The document contains Python code snippets and output for problems involving lists, strings, and NumPy arrays. It includes examples of using map, filter, and NumPy functions to manipulate and calculate properties of data structures.

Uploaded by

Shivam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

NAME: SHIVAM GORLA COURSE:BCA-G1 SEMESTER:G1

ROLLNO:2221964(67) SUBJECT:PYTHON STUDENTID:220111410

21.wap in python to convert uppercase and vice versa


Code:
def swap_case(input_string):
result = ''
for char in input_string:
if char.isupper():
result += char.lower()
elif char.islower():
result += char.upper()
else:
result += char
return result

output:
NAME: SHIVAM GORLA COURSE:BCA-G1 SEMESTER:G1
ROLLNO:2221964(67) SUBJECT:PYTHON STUDENTID:220111410

22. wap in python to calculate ethe square of each element in list using map()
Code:
def square(x):
return x * x
if __name__ == "__main__":
numbers = [1, 2, 3, 4, 5]
squared_numbers = list(map(square, numbers))
print("Original list:", numbers)
print("Squared list:", squared_numbers)

output:
NAME: SHIVAM GORLA COURSE:BCA-G1 SEMESTER:G1
ROLLNO:2221964(67) SUBJECT:PYTHON STUDENTID:220111410

23.wap in python to filter out all the negative numbers from the list
Code:
def is_positive(x):
return x >= 0
if __name__ == "__main__":
numbers = [10, -3, 4, -7, 0, 15, -1, 2]
positive_numbers = list(filter(is_positive, numbers))
print("Original list:", numbers)
print("Filtered list (non-negative numbers):", positive_numbers)
output:
NAME: SHIVAM GORLA COURSE:BCA-G1 SEMESTER:G1
ROLLNO:2221964(67) SUBJECT:PYTHON STUDENTID:220111410

24.wap in python to calculate the sum of the odd numbers from 1 to 10 number list using
filter function
Code:
def is_odd(x):
return x % 2 != 0
if __name__ == "__main__":
numbers = list(range(1, 11))
odd_numbers = list(filter(is_odd, numbers))
sum_of_odd_numbers = sum(odd_numbers)
print("Odd numbers:", odd_numbers)
print("Sum of odd numbers:", sum_of_odd_numbers)

output:
NAME: SHIVAM GORLA COURSE:BCA-G1 SEMESTER:G1
ROLLNO:2221964(67) SUBJECT:PYTHON STUDENTID:220111410

25.wap in python to display sum of all elements of a 2D numpy


Code:
import numpy as np
if __name__ == "__main__":
array_2d = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
sum_of_elements = np.sum(array_2d)
print("2D Array:")
print(array_2d)
print("Sum of all elements:", sum_of_elements)

output:
NAME: SHIVAM GORLA COURSE:BCA-G1 SEMESTER:G1
ROLLNO:2221964(67) SUBJECT:PYTHON STUDENTID:220111410

26. wap in python to find the position where elements of two numpy arrays x and y are same?
Code:
import numpy as np
if __name__ == "__main__":
x = np.array([1, 2, 3, 4, 5, 6])
y = np.array([0, 2, 3, 1, 5, 7])
same_positions = np.where(x == y)[0]
print("Array x:", x)
print("Array y:", y)
print("Positions where elements are the same:", same_positions)

output:

You might also like