Python Lab
Python Lab
Python
Numpy
11. Python program to demonstrate basic array
characteristics
12. Python program to demonstrate array creation
techniques
13. Python program to demonstrate indexing in numpy
14. Python program to demonstrate basic operations on
single array
15. Python program to demonstrate unary operators in
numpy
Pandas
16. Python code demonstrate to make a Pandas DataFrame
with two-dimensional list
17. Python code demonstrate creating DataFrame from
dictionary of narray and lists
18. Python code demonstrate creating a Pandas dataframe
using list of tuples .
19 Python code demonstrate how to iterate over rows in
Pandas Dataframe
20. Python code demonstrate how to get column names in
Pandas dataframe
PYTHON
1.Write a program to demonstrate different numbers data types in python.
a=5
print(a, "is of type", type(a))
a = 2.0
a = 1+2j
Output:-
5 is of type <class 'int'>
return x + y
return x - y
return x * y
else:
return x / y
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
if choice == '1':
else:
print("Invalid input")
Output:-
Select operation:
1. Add
2. Subtract
3. Multiply
4. Divide
Result: 15.0
num_digits = len(str(number))
# Initialize sum
armstrong_sum = 0
temp = number
digit = temp % 10
temp //= 10
# Check if the given number is Armstrong
if number == armstrong_sum:
return True
else:
return False
if is_armstrong(number):
else:
Output:-
Enter a number: 153
primes = []
if num > 1:
if (num % i) == 0:
break
else:
primes.append(num)
return primes
if prime_numbers:
else:
Output:-
Enter the start of the interval: 10
Prime numbers between 10 and 50 are: [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
if n == 0:
return 1
else:
return n * factorial(n-1)
if number < 0:
else:
result = factorial(number)
Output:-
Enter a number: 5
return s == s[::-1]
if is_palindrome(string):
else:
Output:-
Enter a string: katak
The string is a palindrome
count = 0
count += 1
Output:-
Enter a word: Katak
my_list.append(60)
my_list.remove(30)
removed_element = my_list.pop()
print("Removed element from the list:", removed_element)
Output:-
Initial list: [10, 20, 30, 40, 50]
List after appending element 60: [10, 20, 30, 40, 50, 60]
List after removing element 30: [10, 20, 40, 50, 60]
List after removing the last element: [10, 20, 40, 50]
my_tuple = (1, 2, 3, 4, 5)
# Slicing a tuple
# Length of a tuple
# Concatenating tuples
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
print(item)
tuple_to_list = list(my_tuple)
list_to_tuple = tuple(tuple_to_list)
Output:-
Original Tuple: (1, 2, 3, 4, 5)
First element: 1
Last element: 5
Length of tuple: 5
1
2
my_dict["grape"] = 5
my_dict["banana"] = 6
del my_dict["orange"]
my_dict.clear()
Output:-
Original Dictionary: {'apple': 2, 'banana': 3, 'orange': 4}
Value of 'apple': 2
apple : 2
banana : 6
grape : 5
Numpy
# Create a 1D array
# Create a 2D array
print("\n2D Array:")
print(arr2d)
Output:-
1D Array: [1 2 3 4 5]
2D Array:
[[1 2 3]
[4 5 6]]
Dimensions of 1D Array: 1
Dimensions of 2D Array: 2
Size of 1D Array: 5
Size of 2D Array: 6
arr6 = np.linspace(0, 5, 10) # Start at 0, stop at 5 (inclusive), with 10 equally spaced values
This program showcases various techniques for creating arrays in NumPy, such as creating arrays from
lists, creating arrays filled with zeros or ones, creating empty arrays, creating arrays with a range of
values, and creating arrays with evenly spaced values.
Output:-
Array 2 (Zeros):
[[0. 0. 0.]
[0. 0. 0.]]
Array 3 (Ones):
[[1. 1.]
[1. 1.]
[1. 1.]]
Array 4 (Empty):
[[4.9e-324 9.9e-324]
[1.5e-323 2.0e-323]]
Array 5 (Arange): [0 2 4 6 8]
# Slicing
Output:-
Element at (0,0): 10
[[ 60 70]
[100 110]]
# Basic operations
# Scalar operations
scalar = 2
print("Original array multiplied by", scalar, ":", arr * scalar) # Output: [ 2 4 6 8 10]
Note:- This program achieves basic operations such as sum, minimum, maximum, and mean using
NumPy functions and also demonstrates scalar operations like multiplication and addition.
Output:-
Original array: [1 2 3 4 5]
Sum of elements: 15
Minimum element: 1
Maximum element: 5
# Unary operators
Output:-
Original array: [1 2 3 4 5]
Pandas
# Two-dimensional list
# Create DataFrame
df = pd.DataFrame(data, columns=columns)
print(df)
Note:- This code creates a Pandas DataFrame from the two-dimensional list data and specifies the
column names using the columns parameter. Finally, it prints the DataFrame.
Output:-
ID Name Age
0 1 Alice 25
1 2 Bob 30
2 3 Charlie 35
3 4 David 40
17. Python code demonstrate creating DataFrame from dictionary of narray and
lists.
import pandas as pd
import numpy as np
# Create DataFrame
df = pd.DataFrame(data)
print(df)
Note:-This code creates a Pandas DataFrame from the dictionary data, where the keys represent column
names and the values are NumPy arrays or lists. It then prints the resulting DataFrame.
Output:-
A B C
0 1 5 a
1 2 6 b
2 3 7 c
3 4 8 d
18. Python code demonstrate creating a Pandas dataframe using list of tuples .
import pandas as pd
# List of tuples
# Create DataFrame
df = pd.DataFrame(data, columns=columns)
print(df)
Note:- This code creates a Pandas DataFrame from the list of tuples data and specifies the column
names using the columns parameter. Finally, it prints the DataFrame.
It displays a Pandas DataFrame with columns 'ID', 'Name', and 'Age', populated with the corresponding
data from the list of tuples.
Output:-
ID Name Age
0 1 Alice 25
1 2 Bob 30
2 3 Charlie 35
3 4 David 40
df = pd.DataFrame(data)
print("Name:", row['Name'])
print("Age:", row['Age'])
print()
Note:- This code creates a simple Pandas DataFrame and then iterates over its rows using the iterrows()
function. Inside the loop, it prints the index of the row and the values of each column for that row.
It displays the index, name, and age of each row in the Pandas DataFrame.
Output:-
Index: 0
Name: Alice
Age: 25
Index: 1
Name: Bob
Age: 30
Index: 2
Name: Charlie
Age: 35
Index: 3
Name: David
Age: 40
20. Python code demonstrate how to get column names in Pandas dataframe.
import pandas as pd
df = pd.DataFrame(data)
column_names = df.columns
print("Column names:")
print(column)
Note:- This code creates a simple Pandas DataFrame and then retrieves the column names using the
columns attribute. It then iterates over the column names and prints each one.
It displays the column names of the Pandas DataFrame: "Name" and "Age".
Output:-
Column names:
Name
Age