Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Python Programming: Mathematical Operations, Strings, Lists, Tuples, and Sets, Lab Reports of Computer Science

Python programs for mathematical operations, string manipulation, list manipulation, tuple manipulation, and set operations. Includes arithmetic operations with integers and conversion from binary, hexadecimal, and octal. Demonstrates creating, concatenating, indexing, slicing, and manipulating strings. Reverses, concatenates index-wise, squares items, removes empty strings, extends nested lists, replaces items, removes occurrences, and more in lists. Covers reversing, accessing values, creating single-item tuples, unpacking, swapping, copying, modifying, sorting, counting occurrences, and checking equality in tuples. Gets unique items, checks for common elements, updates sets, and removes non-common items. Demonstrates conditional statements.

Typology: Lab Reports

2023/2024

Uploaded on 03/25/2024

varshini-anbalagan-1
varshini-anbalagan-1 🇮🇳

3 documents

1 / 27

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
>>>
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

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:

  1. Addition
  2. Subtraction
  3. Multiplication
  4. Division
  5. Exit Enter your choice (1-5): 1 Enter the first number: 5 Enter the second number: 3 Result: 8. Math Department Program
  6. Addition
  7. Subtraction
  8. Multiplication
  9. Division
  10. 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)

  1. 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)**
  1. 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))