Lyuben_Andreev

FunctionsModules

Jun 21st, 2024
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.72 KB | Software | 0 0
  1. """Task 1
  2. Write a function that prints formatted text given below:
  3.  
  4. "Don't compare yourself with anyone in this world…
  5.  
  6. if you do so, you are insulting yourself."
  7.  
  8.                                                                    Bill Gates"""
  9. print("\r\n")
  10.  
  11. def print_formatted_text():
  12.     print('"Don\'t compare yourself with anyone in this world…')
  13.     print('if you do so, you are insulting yourself."')
  14.     print()
  15.     print(" "*68 + "Bill Gates")
  16.  
  17. # Example usage
  18. print_formatted_text()
  19. print("\r\n")
  20. """Task 2
  21. Write a function that takes two numbers as a parameter and displays all even numbers in between."""
  22.  
  23. def print_even_numbers(start, end):
  24.     for num in range(start, end + 1):
  25.         if num % 2 == 0:
  26.             print(num, end=' ')
  27.  
  28. # Example usage
  29. print("Even numbers between 10 and 20:")
  30. print_even_numbers(10, 20)
  31. print("\nEven numbers between 30 and 40:")
  32. print_even_numbers(30, 40)
  33. print("\r\n")
  34.  
  35. """Task 3
  36. Write a function that prints an empty or solid square made out of some symbol.
  37. The function takes the following as parameters: the length of the side of the square, a symbol, and a boolean variable:
  38. if it is True, the square is solid;
  39. if False, the square is empty."""
  40.  
  41. def print_square(length, symbol, solid):
  42.     if solid:
  43.         for _ in range(length):
  44.             print(symbol * length)
  45.     else:
  46.         print(symbol * length)
  47.         for _ in range(length - 2):
  48.             print(symbol + ' ' * (length - 2) + symbol)
  49.         if length > 1:
  50.             print(symbol * length)
  51.  
  52. # Example usage
  53. print("Solid square with length 5 and symbol '#':")
  54. print_square(5, '#', True)
  55. print("\nEmpty square with length 5 and symbol '*':")
  56. print_square(5, '*', False)
  57. print("\r\n")
  58.  
  59. """Task 4
  60. Write a function that returns the smallest of five numbers. Numbers are passed as parameters."""
  61.  
  62. def find_smallest(a, b, c, d, e):
  63.     smallest = a
  64.     if b < smallest:
  65.         smallest = b
  66.     if c < smallest:
  67.         smallest = c
  68.     if d < smallest:
  69.         smallest = d
  70.     if e < smallest:
  71.         smallest = e
  72.     return smallest
  73.  
  74. # Example usage
  75. result = find_smallest(12, 5, 23, 8, 1)
  76. print("The smallest number is:", result)
  77. print("\r\n")
  78.  
  79. """Task 5
  80. Write a function that returns the product of numbers in a specified range.
  81. The start and end points of the range are passed as parameters.
  82. If the order of these points is incorrect (e.g., 5 is the end, and 25 is the start), swap them."""
  83.  
  84. def product_in_range(start, end):
  85.     if start > end:
  86.         start, end = end, start  # Swap if start is greater than end
  87.     product = 1
  88.     for num in range(start, end + 1):
  89.         product *= num
  90.     return product
  91.  
  92. # Example usage
  93. result = product_in_range(5, 10)
  94. print("Product of numbers from 5 to 10 is:", result)
  95. print("\r\n")
  96.  
  97. """Task 6
  98. Write a function that counts how many digits a number has. The number is passed as a parameter.
  99. Return the received number of digits from the function. For example, if the passed number is 3456, it has 4 digits."""
  100.  
  101. def count_digits(number):
  102.     return len(str(abs(number)))
  103.  
  104. # Example usage
  105. result = count_digits(3456)
  106. print("Number of digits in 3456:", result)
  107. print("\r\n")
  108.  
  109. """Task 7
  110. Write a function that checks if a number is a palindrome.
  111. The number is passed as a parameter. If the number is a palindrome, return true, otherwise false.
  112.  
  113. A palindrome is a number which reads the same backward as forward.
  114. For instance, 123321 is a palindrome, 546645 is a palindrome, but 421987 is not.
  115. """
  116. def is_palindrome(number):
  117.     str_num = str(number)
  118.     return str_num == str_num[::-1]
  119.  
  120. # Example usage
  121. print("Is 123321 a palindrome?", is_palindrome(123321))
  122. print("Is 421987 a palindrome?", is_palindrome(421987))
  123.  
Add Comment
Please, Sign In to add comment