Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """Task 1
- Write a function that prints formatted text given below:
- "Don't compare yourself with anyone in this world…
- if you do so, you are insulting yourself."
- Bill Gates"""
- print("\r\n")
- def print_formatted_text():
- print('"Don\'t compare yourself with anyone in this world…')
- print('if you do so, you are insulting yourself."')
- print()
- print(" "*68 + "Bill Gates")
- # Example usage
- print_formatted_text()
- print("\r\n")
- """Task 2
- Write a function that takes two numbers as a parameter and displays all even numbers in between."""
- def print_even_numbers(start, end):
- for num in range(start, end + 1):
- if num % 2 == 0:
- print(num, end=' ')
- # Example usage
- print("Even numbers between 10 and 20:")
- print_even_numbers(10, 20)
- print("\nEven numbers between 30 and 40:")
- print_even_numbers(30, 40)
- print("\r\n")
- """Task 3
- Write a function that prints an empty or solid square made out of some symbol.
- The function takes the following as parameters: the length of the side of the square, a symbol, and a boolean variable:
- if it is True, the square is solid;
- if False, the square is empty."""
- def print_square(length, symbol, solid):
- if solid:
- for _ in range(length):
- print(symbol * length)
- else:
- print(symbol * length)
- for _ in range(length - 2):
- print(symbol + ' ' * (length - 2) + symbol)
- if length > 1:
- print(symbol * length)
- # Example usage
- print("Solid square with length 5 and symbol '#':")
- print_square(5, '#', True)
- print("\nEmpty square with length 5 and symbol '*':")
- print_square(5, '*', False)
- print("\r\n")
- """Task 4
- Write a function that returns the smallest of five numbers. Numbers are passed as parameters."""
- def find_smallest(a, b, c, d, e):
- smallest = a
- if b < smallest:
- smallest = b
- if c < smallest:
- smallest = c
- if d < smallest:
- smallest = d
- if e < smallest:
- smallest = e
- return smallest
- # Example usage
- result = find_smallest(12, 5, 23, 8, 1)
- print("The smallest number is:", result)
- print("\r\n")
- """Task 5
- Write a function that returns the product of numbers in a specified range.
- The start and end points of the range are passed as parameters.
- If the order of these points is incorrect (e.g., 5 is the end, and 25 is the start), swap them."""
- def product_in_range(start, end):
- if start > end:
- start, end = end, start # Swap if start is greater than end
- product = 1
- for num in range(start, end + 1):
- product *= num
- return product
- # Example usage
- result = product_in_range(5, 10)
- print("Product of numbers from 5 to 10 is:", result)
- print("\r\n")
- """Task 6
- Write a function that counts how many digits a number has. The number is passed as a parameter.
- Return the received number of digits from the function. For example, if the passed number is 3456, it has 4 digits."""
- def count_digits(number):
- return len(str(abs(number)))
- # Example usage
- result = count_digits(3456)
- print("Number of digits in 3456:", result)
- print("\r\n")
- """Task 7
- Write a function that checks if a number is a palindrome.
- The number is passed as a parameter. If the number is a palindrome, return true, otherwise false.
- A palindrome is a number which reads the same backward as forward.
- For instance, 123321 is a palindrome, 546645 is a palindrome, but 421987 is not.
- """
- def is_palindrome(number):
- str_num = str(number)
- return str_num == str_num[::-1]
- # Example usage
- print("Is 123321 a palindrome?", is_palindrome(123321))
- print("Is 421987 a palindrome?", is_palindrome(421987))
Add Comment
Please, Sign In to add comment