Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """Task 1
- The user types in an arithmetic expression. For example, 23+12.
- Print its result. In our example, the output is 35.
- The arithmetic expression can have only three parts: number, operation, number.
- Possible operations: +, -, *, /."""
- # Sample input arithmetic expression
- input_expression = input("Enter arithmetic expression: ")
- # Initialize variables to store the two numbers and the operator
- num1 = ""
- num2 = ""
- operator = ""
- operator_found = False
- # Parse the input expression to extract numbers and the operator
- for char in input_expression:
- if char in '+-*/':
- operator = char
- operator_found = True
- elif operator_found:
- num2 += char
- else:
- num1 += char
- # Convert the string numbers to integers
- num1 = int(num1)
- num2 = int(num2)
- # Perform the arithmetic operation based on the operator
- if operator == '+':
- result = num1 + num2
- elif operator == '-':
- result = num1 - num2
- elif operator == '*':
- result = num1 * num2
- elif operator == '/':
- result = num1 / num2
- # Print the result
- print("Result:", result)
- """Task 2
- You have a list of integers filled with random numbers.
- Find the largest and the smallest elements, count negative and positive elements, as well as zeros.
- Print the results.
- """
- print("\r\n")
- # Sample list of random integers
- numbers = [3, -1, 4, 0, -5, 10, -2, 0, 8, -3]
- # Initialize variables for the largest and smallest elements
- largest = numbers[0]
- smallest = numbers[0]
- # Initialize counters for positive, negative, and zero elements
- count_positive = 0
- count_negative = 0
- count_zero = 0
- # Loop through the list to find the largest, smallest elements and count the number of positives, negatives, and zeros
- for num in numbers:
- # Find the largest element
- if num > largest:
- largest = num
- # Find the smallest element
- if num < smallest:
- smallest = num
- # Count positive elements
- if num > 0:
- count_positive += 1
- # Count negative elements
- elif num < 0:
- count_negative += 1
- # Count zeros
- else:
- count_zero += 1
- # Print the results
- print("Largest element:", largest)
- print("Smallest element:", smallest)
- print("Number of positive elements:", count_positive)
- print("Number of negative elements:", count_negative)
- print("Number of zeros:", count_zero)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement