Output :
Enter elements of the list separated by space: 3 5 7 9 2 4 6 Sorted array: [2, 3, 4, 5, 6, 7, 9]
Output :
Enter elements of the list separated by space: 3 5 7 9 2 4 6 Sorted array: [2, 3, 4, 5, 6, 7, 9]
Output :
1. Length of the string: 382. Convert the string to lowercase: hello, world! this is a sample string.3. Convert the string to uppercase: HELLO, WORLD! THIS IS A SAMPLE STRING.4. Count occurrences of 'is' in the string: 25. Check if the string starts with 'Hello': True6. Check if the string ends with 'string.': True7. Find the index of 'World' in the string: 78. Replace occurrences of 'sample' with 'modified': Hello, World! This is a modified string.9. Split the string into substrings based on ' ': ['Hello,', 'World!', 'This', 'is', 'a', 'sample', 'string.']10. Join substrings into a single string using '_': Hello,_World!_This_is_a_sample_string.
Output :
Select operation: 1. Add 2. Subtract 3. Multiply 4. Divide Enter choice (1/2/3/4): 1 Enter first number: 23 Enter second number: 67 Result: 90.0 Do you want to perform another calculation? (yes/no): yes
Enter choice (1/2/3/4):
Output :
Select operation: 1. Add 2. Subtract 3. Multiply 4. Divide Enter choice (1/2/3/4): 5 Invalid input Do you want to perform another calculation? (yes/no): no
Output :
Enter elements of the list separated by space: 1 3 45 6 8 90 Enter the element to search for: 3 Target 3 found at index 1.
Output 2:
Enter elements of the list separated by space: 3 5 7 89 -1 0 34 Enter the element to search for: 8 Target 8 not found in the list.
def is_prime(number):
Function Definition:
This line defines a function named is_prime that takes a parameter number. The function will return True if the number is prime and False otherwise.
Check for Numbers Less than 2:
This block of code checks if the input number is less than 2. If so, it immediately returns False since prime numbers are defined as greater than 1.
Check for Factors:
This loop checks for factors of the number from 2 up to the square root of the number. If any factor is found, the function returns False since the number is not prime.
Return True for Prime Numbers:
If the loop completes without finding any factors, the function returns True, indicating that the number is prime.
User Input:
This line prompts the user to enter a number for prime checking.
Function Call and Output:
This block of code calls the is_prime function with the user-provided number and prints the result based on whether the number is prime or not.
def multiplication_table(number, limit):
Function Definition:
This line defines a function named multiplication_table that takes two parameters: number (the base number for the multiplication table) and limit (the maximum value up to which the table will be displayed).
Display Header:
This line prints a header indicating that the multiplication table is for the specified number and will be displayed up to the specified limit.
Multiplication Table Loop:
This loop iterates from 1 to limit (inclusive) and calculates the result of multiplying the number by the loop variable i. It then prints each multiplication equation in the format "number * i = result".
User Input:
These lines prompt the user to enter the number for which they want to see the multiplication table and the limit up to which the table should be displayed. The inputs are converted to integers using int().
Function Call:
This line calls the multiplication_table function with the user-provided values of number and limit. The function then displays the multiplication table accordingly.
When you run this program, it will prompt you to enter a number and a limit. After entering the values, the program will display the multiplication table for the specified number up to the given limit.
def sum_of_natural_numbers(n):
Function Definition:
This line defines a function named sum_of_natural_numbers that takes a parameter n. This function will calculate the sum of the first n natural numbers.
Input Validation:
This block of code checks if the input n is a positive integer. If n is not a positive integer or not an integer at all, it prints an error message and exits the function using return.
Sum Calculation:
This line calculates the sum of the first n natural numbers using the formula sum = n * (n + 1) / 2. The // operator is used for integer division to ensure that the result is an integer.
Output:
This line prints the result, indicating the sum of the first n natural numbers.
User Input:
This line prompts the user to enter a positive integer value for n using the input function. The input is then converted to an integer using int().
Function Call:
This line calls the sum_of_natural_numbers function with the user-provided value of n. The function performs the necessary calculations and prints the result.