Advertisement
Lyuben_Andreev

ModuleStringList

Jun 8th, 2024
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.36 KB | Writing | 0 0
  1. """Task 1
  2. The user types in an arithmetic expression. For example, 23+12.
  3.  
  4. Print its result. In our example, the output is 35.
  5. The arithmetic expression can have only three parts: number, operation, number.
  6. Possible operations: +, -, *, /."""
  7.  
  8. # Sample input arithmetic expression
  9. input_expression = input("Enter arithmetic expression: ")
  10.  
  11. # Initialize variables to store the two numbers and the operator
  12. num1 = ""
  13. num2 = ""
  14. operator = ""
  15. operator_found = False
  16.  
  17. # Parse the input expression to extract numbers and the operator
  18. for char in input_expression:
  19.     if char in '+-*/':
  20.         operator = char
  21.         operator_found = True
  22.     elif operator_found:
  23.         num2 += char
  24.     else:
  25.         num1 += char
  26.  
  27. # Convert the string numbers to integers
  28. num1 = int(num1)
  29. num2 = int(num2)
  30.  
  31. # Perform the arithmetic operation based on the operator
  32. if operator == '+':
  33.     result = num1 + num2
  34. elif operator == '-':
  35.     result = num1 - num2
  36. elif operator == '*':
  37.     result = num1 * num2
  38. elif operator == '/':
  39.     result = num1 / num2
  40.  
  41. # Print the result
  42. print("Result:", result)
  43.  
  44.  
  45. """Task 2
  46. You have a list of integers filled with random numbers.
  47. Find the largest and the smallest elements, count negative and positive elements, as well as zeros.
  48. Print the results.
  49. """
  50.  
  51. print("\r\n")
  52.  
  53. # Sample list of random integers
  54. numbers = [3, -1, 4, 0, -5, 10, -2, 0, 8, -3]
  55.  
  56. # Initialize variables for the largest and smallest elements
  57. largest = numbers[0]
  58. smallest = numbers[0]
  59.  
  60. # Initialize counters for positive, negative, and zero elements
  61. count_positive = 0
  62. count_negative = 0
  63. count_zero = 0
  64.  
  65. # Loop through the list to find the largest, smallest elements and count the number of positives, negatives, and zeros
  66. for num in numbers:
  67.     # Find the largest element
  68.     if num > largest:
  69.         largest = num
  70.  
  71.     # Find the smallest element
  72.     if num < smallest:
  73.         smallest = num
  74.  
  75.     # Count positive elements
  76.     if num > 0:
  77.         count_positive += 1
  78.  
  79.     # Count negative elements
  80.     elif num < 0:
  81.         count_negative += 1
  82.  
  83.     # Count zeros
  84.     else:
  85.         count_zero += 1
  86.  
  87. # Print the results
  88. print("Largest element:", largest)
  89. print("Smallest element:", smallest)
  90. print("Number of positive elements:", count_positive)
  91. print("Number of negative elements:", count_negative)
  92. print("Number of zeros:", count_zero)
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement