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

lab2_Python

The document contains a Python script that performs various operations including input handling for integers, floats, and strings, as well as arithmetic operations and string manipulations. It also demonstrates slicing of strings and lists, along with user interaction for creating and modifying tuples and lists. The script showcases fundamental programming concepts such as data types, user input, and basic list operations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

lab2_Python

The document contains a Python script that performs various operations including input handling for integers, floats, and strings, as well as arithmetic operations and string manipulations. It also demonstrates slicing of strings and lists, along with user interaction for creating and modifying tuples and lists. The script showcases fundamental programming concepts such as data types, user input, and basic list operations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

var_int , var_float , var_str = int(input("Enter the integer: ")) , float(input("Enter the

float: ")) , input("Enter the string: ")


print("Initial values: \n Integer: ", var_int ,"\n Float: ", var_float , "\n String: " , var_str)
addition = var_int + var_float
subtraction = var_float - var_int
Multiplication = var_int * var_float
division = var_float / var_int if var_int != 0 else "Division by zero error! "
print("The result if addition is : " , addition , "\n The result of subtraction is : " ,
subtraction , "\n The result of Multiplication is : " , Multiplication , "\n The result of
Division is :" , division)
string_int_concat = var_str + " " + str(var_int)
string_float_concat = f"{var_str} {var_float}"

print("String combination: \n String + Integer: " , string_int_concat)


print("String + Float :" , string_float_concat)

var_int , var_float = var_int * 2 , var_float / 2


var_str = var_str.upper()

print("\n Integer after modification" , var_int , "\n Float after modification" , var_float)
print("String after modification" , var_str)
# Step 1: Initial string slicing example
sample_str = "Advanced Python"

# Slicing the word "Python" from the string


slice_python = sample_str[9:] # Extracting 'Python'

# Slicing the word "Advanced"


slice_advanced = sample_str[:8] # Extracting 'Advanced'

# Combining slices creatively


combined_sentence = f"{slice_python} is more {slice_advanced} than you think!"
print("Combined sentence:", combined_sentence)

# Step 2: Extending task with user input


user_input = input("\n Please enter a sentence: ")

# Asking user for slicing indices


start1 = int(input("Enter the starting index for the first slice: "))
end1 = int(input("Enter the ending index for the first slice: "))
start2 = int(input("Enter the starting index for the second slice: "))
end2 = int(input("Enter the ending index for the second slice: "))

# Slicing based on user input


slice1 = user_input[start1:end1]
slice2 = user_input[start2:end2]

# Creating a unique sentence by combining slices


new_sentence = f"First slice: '{slice1}', Second slice: '{slice2}'"
print("\n Your combined sentence is:", new_sentence)
# Ask user for a number (can be float or integer)
user_input = input("Please enter a number (integer or float): ")
# Convert input to float for flexible operations
number = float(user_input)
# Perform various operations with built-in functions
rounded_value = round(number) # Rounds the number
power_of_two = pow(number, 2) # Exponentiation (number squared)
absolute_value = abs(number) # Gets the absolute value
integer_conversion = int(number) # Converts to integer (truncates decimal part)
# Print the results of each operation
print("Original number: ",number)
print("Rounded value: ", rounded_value)
print("Squared value (number^2): ", power_of_two)
print("Absolute value: ", absolute_value)
print("Integer conversion: ",integer_conversion)
# Ask the user to input a list of numbers
user_input = input("Enter a list of numbers separated by spaces: ")
# Convert the input string into a list of integers
number_list = list(map(int, user_input.split()))
# Ask the user for start and end indices for slicing
start_index = int(input("Enter the starting index for slicing: "))
end_index = int(input("Enter the ending index for slicing: "))
# Perform slicing and display the sliced list
sliced_list = number_list[start_index:end_index]
print("Sliced list (from index ",start_index, "to" , end_index ,":", sliced_list)
# Ask the user for a value to append to the list
append_value = int(input("Enter a value to append to the list: "))
number_list.append(append_value)
print("List after appending " , append_value , ":", number_list)
# Ask the user for a value and position to insert into the list
insert_value = int(input("Enter a value to insert into the list: "))
insert_position = int(input("Enter the position to insert the value: "))
number_list.insert(insert_position, insert_value)
print("List after inserting", insert_value , "at position ", "insert_position ", number_list)
# Ask the user for a value to remove from the list
remove_value = int(input("Enter a value to remove from the list: "))
if remove_value in number_list:
number_list.remove(remove_value)
print("List after removing " , remove_value ,":", number_list)
else:
print("Value ", remove_value , " is not in the list.")
#Let the user input a tuple of values (mixed types allowed)
user_input = input("Enter values separated by spaces to create a tuple: ")
#Split the input and create a tuple (input as strings)
user_tuple = tuple(user_input.split())
#Access elements in the tuple (demonstrating first and last element access)
print("Original tuple: " , user_tuple)
print("First element: " , user_tuple[0])
print("Last element: " , user_tuple[-1])
#Convert the tuple to a list
tuple_to_list = list(user_tuple)
print("Tuple converted to list: " , tuple_to_list)
#Modify the list
append_value = input("Enter a value to append to the list: ")
tuple_to_list.append(append_value)
print("List after appending " , append_value , ":" ,tuple_to_list)
#Convert the list back to a tuple
list_to_tuple = tuple(tuple_to_list)
print("List converted back to tuple: " , list_to_tuple)
#Create another tuple and combine it with the modified tuple
additional_tuple = ("Extra", "Tuple", "Values")
combined_tuple = list_to_tuple + additional_tuple
print("Combined tuple: " , combined_tuple)

You might also like