The document is a Python script that performs various operations including user input for integers, floats, and strings, and demonstrates arithmetic operations, string manipulation, and list handling. It also includes examples of slicing strings and lists, as well as converting between tuples and lists. The script showcases interactive features allowing users to input data and see the results of their operations.
The document is a Python script that performs various operations including user input for integers, floats, and strings, and demonstrates arithmetic operations, string manipulation, and list handling. It also includes examples of slicing strings and lists, as well as converting between tuples and lists. The script showcases interactive features allowing users to input data and see the results of their operations.
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: "))
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)