0% found this document useful (0 votes)
14 views5 pages

L and T Lab Assainments - Tejoprakash

The document contains a series of Python programming assignments designed for a lab setting, covering various topics such as user input, arithmetic operations, list and tuple manipulations, and basic NumPy operations. Each assignment includes code snippets that demonstrate how to implement specific tasks, such as adding numbers, calculating percentages, and performing array operations. The document serves as a practical guide for students to practice and enhance their programming skills using Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

L and T Lab Assainments - Tejoprakash

The document contains a series of Python programming assignments designed for a lab setting, covering various topics such as user input, arithmetic operations, list and tuple manipulations, and basic NumPy operations. Each assignment includes code snippets that demonstrate how to implement specific tasks, such as adding numbers, calculating percentages, and performing array operations. The document serves as a practical guide for students to practice and enhance their programming skills using Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

4/14/25, 10:41 AM L and T Lab assainments - Colab

keyboard_arrow_down 1. Ask for the user’s first name and display the output message
first_name = input("Enter your first name: ")
print(f"Hello, {first_name}!")

Enter your first name: Tejo prakash


Hello, Tejo prakash!

keyboard_arrow_down 2. Ask for the user’s first name and surname and display the output message
# 2. Ask for the user’s first name and surname and display the output message
first_name = input("Enter your first name: ")
surname = input("Enter your surname: ")
print(f"Hello, {first_name} {surname}!")

Enter your first name: Tejoprakash


Enter your surname: Kandula
Hello, Tejoprakash Kandula!

keyboard_arrow_down 3. Add two numbers and display the result


num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
print(f"The total is {num1 + num2}")

Enter first number: 2


Enter second number: 2
The total is 4.0

keyboard_arrow_down 4. Add two numbers and multiply by a third


a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))
print(f"The result is {(a + b) * c}")

Enter first number: 2


Enter second number: 2
Enter third number: 2
The result is 8.0

keyboard_arrow_down 5. Pizza slice calculation


total_slices = int(input("How many slices did you start with? "))
eaten_slices = int(input("How many slices have you eaten? "))
print(f"You have {total_slices - eaten_slices} slices left.")

How many slices did you start with? 3


How many slices have you eaten? 2
You have 1 slices left.

keyboard_arrow_down 6. Name and age increment


name = input("Enter your name: ")
age = int(input("Enter your age: "))
print(f"{name}, next year you will be {age + 1} years old.")

Enter your name: Tejoprakash


Enter your age: 20
Tejoprakash, next year you will be 21 years old.

https://colab.research.google.com/drive/18v_vm_JoTYIE1nHFVwe6TB0R1wvuVUXG?hl=en#scrollTo=Kqbd32Prkfy1&printMode=true 1/5
4/14/25, 10:41 AM L and T Lab assainments - Colab

keyboard_arrow_down 7. Split the bill


total_bill = float(input("Enter total bill amount: "))
diners = int(input("Enter number of diners: "))
print(f"Each person must pay: {total_bill / diners:.2f}")

Enter total bill amount: 200


Enter number of diners: 2
Each person must pay: 100.00

keyboard_arrow_down 8. Convert days to hours, minutes, and seconds


days = int(input("Enter number of days: "))
print(f"{days} days = {days*24} hours, {days*24*60} minutes, and {days*24*60*60} seconds.")

Enter number of days: 365


365 days = 8760 hours, 525600 minutes, and 31536000 seconds.

keyboard_arrow_down 9. Result classification


percentage = float(input("Enter your end term percentage: "))
if percentage > 85:
print("Passed with distinction")
elif percentage > 40:
print("Passed")
else:
print("Failed")

Enter your end term percentage: 89


Passed with distinction

keyboard_arrow_down 10. Number between 10 and 20


number = int(input("Enter a number between 10 and 20: "))
if 10 <= number <= 20:
print("Thank you")
else:
print("Incorrect answer")

Enter a number between 10 and 20: 15


Thank you

keyboard_arrow_down 11. Name three times


name = input("Enter your name: ")
print((name + "\n") * 3)

Enter your name: Tejoprakash


Tejoprakash
Tejoprakash
Tejoprakash

keyboard_arrow_down 12. Name repeat based on number


name = input("Enter your name: ")
times = int(input("Enter a number: "))
if times < 10:
print((name + "\n") * times)

Enter your name: Tejoprakash


Enter a number: 3
Tejoprakash
Tejoprakash
Tejoprakash

https://colab.research.google.com/drive/18v_vm_JoTYIE1nHFVwe6TB0R1wvuVUXG?hl=en#scrollTo=Kqbd32Prkfy1&printMode=true 2/5
4/14/25, 10:41 AM L and T Lab assainments - Colab

keyboard_arrow_down 13. List operations


my_list = [1, "apple", 3.14, "banana", 7, 2.71, "carrot", 9, "dog", 5]
my_list.append("new_item")
my_list.remove("apple")
my_list[0] = 42
print("Sliced list:", my_list[2:6])
print("Length of list:", len(my_list))
sorted_list = sorted([x for x in my_list if isinstance(x, (int, float))])
print("Sorted numbers ascending:", sorted_list)
print("Sorted descending:", sorted_list[::-1])
print("'banana' is in list?", "banana" in my_list)
print("Index of 'banana':", my_list.index("banana") if "banana" in my_list else "Not found")

Sliced list: ['banana', 7, 2.71, 'carrot']


Length of list: 10
Sorted numbers ascending: [2.71, 3.14, 5, 7, 9, 42]
Sorted descending: [42, 9, 7, 5, 3.14, 2.71]
'banana' is in list? True
Index of 'banana': 2

keyboard_arrow_down 14. Tuple operations


my_tuple = (1, "text", [2, 3])
print("First element:", my_tuple[0])
print("Slice:", my_tuple[1:])

First element: 1
Slice: ('text', [2, 3])

keyboard_arrow_down 16. Numpy Operations Using Google Colab or Jupyter


Create and manipulate NumPy arrays. • Create 1D, 2D, and 3D arrays using numpy.array().

• Check the shape, size, and data type of the arrays.

• Modify the shape of an array using reshape().

import numpy as np

# 1D Array
arr_1d = np.array([1, 2, 3])
print("1D Array:", arr_1d)

# 2D Array
arr_2d = np.array([[1, 2], [3, 4]])
print("2D Array:\n", arr_2d)

# 3D Array
arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print("3D Array:\n", arr_3d)

# Shape, size, data type


print("Shape:", arr_3d.shape)
print("Size:", arr_3d.size)
print("Data Type:", arr_3d.dtype)

# Reshape
reshaped = np.reshape(arr_1d, (3, 1))
print("Reshaped 1D to 2D:\n", reshaped)

1D Array: [1 2 3]
2D Array:
[[1 2]
[3 4]]
3D Array:
[[[1 2]
[3 4]]

[[5 6]
[7 8]]]
Shape: (2, 2, 2)
Size: 8
Data Type: int64
Reshaped 1D to 2D:
[[1]

https://colab.research.google.com/drive/18v_vm_JoTYIE1nHFVwe6TB0R1wvuVUXG?hl=en#scrollTo=Kqbd32Prkfy1&printMode=true 3/5
4/14/25, 10:41 AM L and T Lab assainments - Colab
[2]
[3]]

keyboard_arrow_down 17. Array Indexing and Slicing using Numpy


• Create a 2D array of shape (3, 4).

• Access specific rows, columns, and elements.

• Use slicing to extract a subarray.

• Experiment with boolean indexing to filter elements.

arr = np.array([[10, 20, 30, 40],


[50, 60, 70, 80],
[90, 100, 110, 120]])

# Access specific row, column, element


print("First row:", arr[0])
print("Second column:", arr[:, 1])
print("Element at (2,3):", arr[2, 3])

# Slicing
print("Subarray (first 2 rows and columns):\n", arr[:2, :2])

# Boolean indexing
print("Elements > 50:\n", arr[arr > 50])

First row: [10 20 30 40]


Second column: [ 20 60 100]
Element at (2,3): 120
Subarray (first 2 rows and columns):
[[10 20]
[50 60]]
Elements > 50:
[ 60 70 80 90 100 110 120]

keyboard_arrow_down 18. Explore different ways to create arrays using Numpy.


• Create arrays using zeros(), ones(), and full().

• Generate random arrays using random.random() and random.randint().

• Create evenly spaced arrays using arange()

# Zeros, ones, full


zeros = np.zeros((2, 3))
ones = np.ones((2, 3))
full = np.full((2, 3), 7)

print("Zeros:\n", zeros)
print("Ones:\n", ones)
print("Full (7s):\n", full)

# Random arrays
random_floats = np.random.random((2, 3))
random_ints = np.random.randint(1, 10, (2, 3))

print("Random floats:\n", random_floats)


print("Random integers:\n", random_ints)

# arange
evenly_spaced = np.arange(0, 10, 2)
print("Evenly spaced (0 to 10 step 2):", evenly_spaced)

Zeros:
[[0. 0. 0.]
[0. 0. 0.]]
Ones:
[[1. 1. 1.]
[1. 1. 1.]]
Full (7s):
[[7 7 7]
[7 7 7]]
Random floats:
[[0.23365107 0.15422949 0.11361008]
[0.29595246 0.74685921 0.09367996]]
Random integers:
[[2 8 7]

https://colab.research.google.com/drive/18v_vm_JoTYIE1nHFVwe6TB0R1wvuVUXG?hl=en#scrollTo=Kqbd32Prkfy1&printMode=true 4/5
4/14/25, 10:41 AM L and T Lab assainments - Colab
[3 4 5]]
Evenly spaced (0 to 10 step 2): [0 2 4 6 8]

keyboard_arrow_down 19. Perform element-wise operations on arrays using Numpy.


• Create two arrays of the same shape.

• Perform addition, subtraction, multiplication, and division.

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

print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)

Addition: [5 7 9]
Subtraction: [-3 -3 -3]
Multiplication: [ 4 10 18]
Division: [0.25 0.4 0.5 ]

keyboard_arrow_down 20. Apply NumPy mathematical functions to arrays using Numpy.


Use numpy.sum(), numpy.mean(), numpy.median(), numpy.std(), and numpy.var().

data = np.array([5, 10, 15, 20, 25])

print("Sum:", np.sum(data))
print("Mean:", np.mean(data))
print("Median:", np.median(data))
print("Standard Deviation:", np.std(data))
print("Variance:", np.var(data))

Sum: 75
Mean: 15.0
Median: 15.0
Standard Deviation: 7.0710678118654755
Variance: 50.0

https://colab.research.google.com/drive/18v_vm_JoTYIE1nHFVwe6TB0R1wvuVUXG?hl=en#scrollTo=Kqbd32Prkfy1&printMode=true 5/5

You might also like