In the previous article, we have discussed Python Program for Set discard() Method
ceil() Function in Python:
If necessary, the math.ceil() method rounds a number UP to the nearest integer and returns the result.
The ceil function in Python is used to return the smallest integer value that is greater than or equal to the specified expression or number.
Syntax:
math.ceil(number)
Parameter Values:
number: This is required. It Sets the number to round up.
Note: The ceil function returns the ceiling value if the number argument is positive or negative.
The math ceil function returns TypeError if the number argument is not a number.
Return Value: The return value is an int value that represents the rounded number.
Examples:
Example1:
Input:
Given tuple = (1.3, 10.04, -4.4, 7.7, 9.45) Given List = [2.8, 3.4, -1.7, -6.82, 12.2]
Output:
Given positive number 9.2 ceiling value = 10.00 Given negative number -8.6 ceiling value = -8.00 Given Tuple element(gvn_tupl[3]) ceiling value = 8.00 Given Tuple element(gvn_lst[2]) ceiling value = -1.00 The Ceil of pi is: 4 Given multiple number(35+40-10.3) ceiling value = 65.00
Example2:
Input:
Given tuple = (2.4, 85.67, 37.8, -25.5) Given List = [45.9, 11.32, -25, 6, 12.8, -10]
Output:
Given positive number 30.1 ceiling value = 31.00 Given negative number -20.4 ceiling value = -20.00 Given Tuple element(gvn_tupl[0]) ceiling value = 3.00 Given Tuple element(gvn_lst[3]) ceiling value = 6.00 The Ceil of pi is: 4 Given multiple number (23-10+5) ceiling value = 18.00
Program for ceil() Function in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Import math module using the import keyword.
- Give the tuple as static input and store it in a variable.
- Give the list as static input and store it in another variable.
- Apply math. ceil() function to any positive number and print it.
- Apply math. ceil() function to any negative number and print it.
- Apply math. ceil() function to the given tuple element and print it.
- Apply math. ceil() function to the pi value and print it.
- Apply math. ceil() function to any multiple numbers and print it.
- The Exit of Program.
Below is the implementation:
# Import math module using the import keyword. import math # Give the tuple as static input and store it in a variable. gvn_tupl = (1.3, 10.04, -4.4, 7.7, 9.45) # Give the list as static input and store it in another variable. gvn_lst = [2.8, 3.4, -1.7, -6.82, 12.2] # Apply math. ceil() function to any positive number and print it. print('Given positive number 9.2 ceiling value = %.2f' % math.ceil(9.2)) # Apply math. ceil() function to any negative number and print it. print('Given negative number -8.6 ceiling value = %.2f' % math.ceil(-8.6)) # Apply math. ceil() function to the given tuple element and print it. print('Given Tuple element(gvn_tupl[3]) ceiling value = %.2f' % math.ceil( gvn_tupl[3])) # Apply math. ceil() function to the given list element and print it. print( 'Given Tuple element(gvn_lst[2]) ceiling value = %.2f' % math.ceil(gvn_lst[2])) # Apply math. ceil() function to the pi value and print it. print("The Ceil of pi is: ", math.ceil(math.pi)) # Apply math. ceil() function to any multiple numbers and print it. print('Given multiple number(35+40-10.3) ceiling value = %.2f' % math.ceil(35+40-10.3))
Output:
Given positive number 9.2 ceiling value = 10.00 Given negative number -8.6 ceiling value = -8.00 Given Tuple element(gvn_tupl[3]) ceiling value = 8.00 Given Tuple element(gvn_lst[2]) ceiling value = -1.00 The Ceil of pi is: 4 Given multiple number(35+40-10.3) ceiling value = 65.00
Method #2: Using Built-in Functions (User Input)
Approach:
- Import math module using the import keyword.
- Give the tuple as user input using list(),map(),input(),and split() functions.
- Store it in a variable.
- Give the list as user input using list(),map(),input(),and split() functions.
- Store it in another variable.
- Give the positive number as user input using the float(input()) function
- Store it in another variable.
- Apply math. ceil() function to the given positive number and print it.
- Give the negative number as user input using the float(input()) function and store it in another variable.
- Apply math. ceil() function to the given negative number and print it.
- Apply math. ceil() function to the given tuple element and print it.
- Apply math. ceil() function to the given list element and print it.
- The Exit of Program.
Below is the implementation:
# Import math module using the import keyword. import math # Give the tuple as user input using list(),map(),input(),and split() functions. # Store it in a variable. gvn_tupl = tuple(map(float, input( 'Enter some random tuple Elements separated by spaces = ').split())) # Give the list as user input using list(),map(),input(),and split() functions. # Store it in another variable. gvn_lst = list(map(float, input( 'Enter some random List Elements separated by spaces = ').split())) # Give the positive number as user input using the float(input()) function and # Store it in another variable. gvn_positvenum=float(input("Enter some random number = ")) # Apply math. ceil() function to the given positive number and print it. print('Given positive number',gvn_positvenum,'ceiling value = %.2f' % math.ceil(gvn_positvenum)) # # Give the negative number as user input using the float(input()) function and # Store it in another variable. gvn_negatvenum=float(input("Enter some random number = ")) # Apply math. ceil() function to the given negative number and print it. print('Given negative number',gvn_negatvenum,'ceiling value = %.2f' % math.ceil(gvn_negatvenum)) # Apply math. ceil() function to the given tuple element and print it. print('Given Tuple element(gvn_tupl[0]) ceiling value = %.2f' % math.ceil( gvn_tupl[0])) # Apply math. ceil() function to the given list element and print it. print( 'Given Tuple element(gvn_lst[3]) ceiling value = %.2f' % math.ceil(gvn_lst[3]))
Output:
Enter some random tuple Elements separated by spaces = 12.3 -11.2 6.7 4.6 Enter some random List Elements separated by spaces = 8.5 10.3 -6.4 -20.8 Enter some random number = 30.1 Given positive number 30.1 ceiling value = 31.00 Enter some random number = -20.4 Given negative number -20.4 ceiling value = -20.00 Given Tuple element(gvn_tupl[0]) ceiling value = 13.00 Given Tuple element(gvn_lst[3]) ceiling value = -20.00
Read all the mathematical functions available in Python and understand how to implement them in your program by using the tutorial of Python Mathematical Methods Examples.