Download Python Programming: Mathematical Operations, Strings, Lists, Tuples, and Sets and more Lab Reports Computer Science in PDF only on Docsity!
Python Programming code: 1) SQUARE PATTERN : side = int(input("Please Enter any Side of a Square : ")) input is converted to an integer using int() and stored in the variable side. i = 0 Variable I initialized by 0 print("Square Star Pattern") while(i < side): j = 0 while(j < side): j = j + 1 print('', end = ' ') i = i + 1 print('') OUTPUT:* Please Enter any Side of a Square : 12 Square Star Pattern
>>>
2) Mathematical Operations Program:
# Math Department Program
def add(a, b):
return a + b
def subtract(a, b):
return a – b
def multiply(a, b):
return a * b
def divide(a, b):
if b != 0:
return a / b
else:
return "Error: Cannot divide by zero."
elif choice == "2":
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
result = subtract(num1, num2)
print("Result: ", result)
elif choice == "3":
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
result = multiply(num1, num2)
print("Result: ", result)
elif choice == "4":
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
result = divide(num1, num2)
print("Result: ", result)
elif choice == "5":
print("Exiting the program...")
OUTPUT:
- Addition
- Subtraction
- Multiplication
- Division
- Exit Enter your choice (1-5): 1 Enter the first number: 5 Enter the second number: 3 Result: 8. Math Department Program
- Addition
- Subtraction
- Multiplication
- Division
- Exit Enter your choice (1-5): 3 Enter the first number: 4 Enter the second number: 2 Result: 8.
3) a) Assign integer number and calculate using arithmetic operators
# Define integers
a = 5
b = 10
# Perform arithmetic operations with integers
sum_result = a + b
difference_result = b - a
product_result = a * b
quotient_result = b / a
# Display the results
print("Sum:", sum_result)
print("Difference:", difference_result)
print("Product:", product_result)
print("Quotient:", quotient_result)
**4) FLOAT Program: my_float = 3.14 Define a float variable another_float = 2.0 arithmetic operations with float variables result = my_float + another_float print(result)
- A) Float program using the syntax: str_value = "3.14" Convert a string to a float float_value = float(str_value) print(float_value) int_value = 42 Convert an integer to a float float_value = float(int_value) print(float_value)**
- Complex Program:
Create a complex number with specified real and imaginary
parts complex_number1 = complex(3.0, 4.0) # 3.0 + 4.0j print(complex_number1)
Create a complex number with only the real part (imaginary
part defaults to 0.0) complex_number2 = complex(2.0) # 2.0 + 0.0j print(complex_number2)
Create a complex number with only the imaginary part (real
part defaults to 0.0) complex_number3 = complex(imag=1.5) # 0.0 + 1.5j print(complex_number3)
# String length
string_length = len(my_string)
print("String length:", string_length)
# String methods
uppercase_string = my_string.upper()
Convert to uppercase
lowercase_string = my_string.lower()
Convert to lowercase
print(uppercase_string)
print(lowercase_string)
7) Print characters from a string that are present at an even index number
Accept a string from the user
user_input = input("Enter a string: ")
Calculate the length of the string
length_of_string = len(user_input) print("Length of the string:", length_of_string)
Iterate over even indices using a for loop and range
print("Characters at even indices:") for i in range(0, length_of_string, 2): print(user_input[i])
Accept input string from a user
word = input('Enter a word: ') print("Original String:", word) 7)a) Print characters using slicing
Convert string to list
Pick only even index chars
characters_at_even_indices = list(word[0::2])
Print characters at even indices
print("Characters at even indices:") for char in characters_at_even_indices: print(char)
Add new item to list after a specified item
def add_item_after(lst, item_to_find, new_item): index = lst.index(item_to_find) lst.insert(index + 1, new_item) return lst
Extend nested list by adding the sublist
def extend_nested_list(nested_list, sublist): nested_list.extend(sublist) return nested_list
Replace list’s item with a new value if found
def replace_item(lst, old_value, new_value): for i in range(len(lst)): if lst[i] == old_value: lst[i] = new_value return lst
Remove all occurrences of a specific item from a list
def remove_all_occurrences(lst, item_to_remove): return [x for x in lst if x != item_to_remove]
usage:
my_list = [1, 2, 3, 4, 5] other_list = [6, 7, 8, 9, 10] nested_list = [[11, 12], [13, 14]] print("1:", reverse_list(my_list)) print(“ 2:", concatenate_index_wise(my_list, other_list)) print(“ 3:", square_elements(my_list)) print(“ 4:", concatenate_order(my_list, other_list)) print(“ 5:") iterate_simultaneously(my_list, other_list) print(" 6:", remove_empty_strings(["", "hello", "", "world", ""])) print(“ 7:", add_item_after(my_list, 3, 100)) print(“8:", extend_nested_list(nested_list, [15, 16])) print(“9:", replace_item(my_list, 3, 300)) print(“ 10:", remove_all_occurrences(my_list + other_list, 5))