Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

Name aashutosh Bhardwaj

Section:E2

Course:B.C.A

Roll no:01

def replace_space_with_tab(input_string):
return input_string.replace(' ', '\t')

# Example string long_string = "This is a long string with


spaces between words"

# Replace spaces with tabs modified_string =


replace_space_with_tab(long_string)
print(modified_string)

Output:
Name aashutosh Bhardwaj

Section:E2

Course:B.C.A

Roll no:01

# Define a dictionary
my_dict = {'a': 1, 'b': 2, 'c': 3}

# 1. Print the dictionary


print("1. Print this dictionary:")
print(my_dict)

# 2. Print only its values print("\


n2. Print only its values:")
print(list(my_dict.values()))

# 3. Empty the dictionary


my_dict.clear()
# Print the empty dictionary
print("\n3. Now first empty that dictionary than again print it in python:")
print(my_dict)

Output:
# Define a list with duplicate values
my_list = [1, 2, 3, 4, 2, 3, 5]
unique_list = list(set(my_list))

print("Original list:", my_list) print("List with


duplicates removed:", unique_list)

Output:
def process_tuple(input_tuple):
new_tuple = () for value in input_tuple:
if isinstance(value, int): new_tuple +=
(value,) elif isinstance(value, float):
continue elif isinstance(value, str) and
len(value) == 3: new_tuple += (value,)
return new_tuple

# Example tuple input_tuple = (10, 3.14,


"abc", 5, 6.7, "def")

# Process the tuple new_tuple =


process_tuple(input_tuple)

print("Original Tuple:", input_tuple)


print("Modified Tuple:", new_tuple)

Output:
# Create the dictionary with keys as numbers and values as cubes
cube_dict = {num: num**3 for num in range(1, 11)}

# 1. Print the dictionary


print("1. Dictionary:")
print(cube_dict)

# 2. Print only the values


print("\n2. Values:")
print(list(cube_dict.values()))

# Empty the dictionary


cube_dict.clear()

# 3. Print the empty dictionary print("\


n3. Empty Dictionary:")
print(cube_dict)

Output:
def count_vowels(string):
vowels = 'aeiouAEIOU'
count = 0 for char in
string: if char in
vowels: count +=
1 return count

def main(): user_input = input("Enter a string:


") total_vowels = count_vowels(user_input)
print("Total number of vowels:", total_vowels)

if __name__ == "__main__":

main() Output:

def calculate_total_cost(quantity, price_per_unit):


total_cost = quantity * price_per_unit if
total_cost > 1000: discount = 0.1 * total_cost
total_cost -= discount return total_cost

def main(): quantity = int(input("Enter the quantity purchased: "))


price_per_unit = float(input("Enter the price per unit in rupees: "))
total_cost = calculate_total_cost(quantity, price_per_unit)
print("Total cost:", total_cost, "rupees")

if __name__ == "__main__":

main() Output:

def calculate_bonus(salary, years_of_service):


if years_of_service > 5: bonus = 0.05 *
salary else:
bonus = 0
return bonus

def main(): salary = float(input("Enter the employee's


salary: ")) years_of_service = int(input("Enter the years of
service: ")) bonus = calculate_bonus(salary,
years_of_service) print("Net bonus amount:", bonus,
"rupees")

if __name__ == "__main__":
main() Output:
# Define the lambda function for multiplication
multiply = lambda x, y: x * y

def print_table(number):
print("Table of", number, ":")
for i in range(1, 11):
result = multiply(number, i)
print(number, "x", i, "=", result)

def main(): number = int(input("Enter a number to print


its table: ")) print_table(number)

if __name__ == "__main__":
main() Output:
# Define lambda functions for squaring and
cubing square = lambda x: x ** 2 cube = lambda
x: x ** 3

def square_and_cube(numbers):
squared_numbers = list(map(square, numbers))
cubed_numbers = list(map(cube, numbers))
return squared_numbers, cubed_numbers

def main():
numbers = [int(x) for x in input("Enter a list of integers separated by spaces: ").split()]
squared_numbers, cubed_numbers = square_and_cube(numbers)
print("Original numbers:", numbers)
print("Squared numbers:", squared_numbers)
print("Cubed numbers:", cubed_numbers)

if __name__ == "__main__":

main() Output:
import numpy as np

def main(): # Create a 2D


numpy array array_2d =
np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])

# Calculate the sum of all elements


total_sum = np.sum(array_2d)

print("2D Array:") print(array_2d)


print("Sum of all elements:", total_sum)

if __name__ == "__main__":
main()
import numpy as np

def find_matching_positions(x, y):


matching_positions = np.where(x == y)
return matching_positions

x = np.array([1, 2, 3, 4, 5])
y = np.array([1, 0, 3, 4, 6])

positions = find_matching_positions(x, y)
print("Positions where elements are the same:", positions)
import numpy as np

# Create a 9x3 numpy array from a range between 13 to 40 with a difference of 1


arr = np.arange(13, 40+1, 1).reshape(9, 3)

# Split the array into three equal-sized subarrays


subarrays = np.split(arr, 3)

# Display the original array and subarrays


print("Original Array:") print(arr) print("\
nSubarrays:") for i, subarray in
enumerate(subarrays): print(f"Subarray
{i+1}:") print(subarray) print()
import pandas as pd
import numpy as np

# Create a pandas DataFrame


data = {
'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]
} df =
pd.DataFrame(data)

# Convert DataFrame to numpy array


array = df.values
# Display the numpy array
print("Numpy Array:")
print(array)
import pandas as pd

# Create a pandas DataFrame


data = {
'Name': ['John', 'Emma', 'Ryan'],
'Age': [25, 30, 35],
'City': ['New York', 'Los Angeles', 'Chicago']
} df =
pd.DataFrame(data)

# Write DataFrame to CSV file csv_filename = 'output.csv'


df.to_csv(csv_filename, index=False) print(f"DataFrame has been
successfully written to '{csv_filename}'.")

You might also like