Python Ceiling Function: What Is It And How Does It Work?

What is the ceiling function in Python, and how does it work? The ceiling function in Python is found in the math library and it’s method name is ceil() which takes a sole numeric parameter. To use the ceiling function in Python you will need to import the math library, like so: 1 2 3 4 5 >>> import math >>> math.ceil(1.6) 2 >>> math.ceil(-2.1) -2 As you can see from the above Python REPL code the output produces the greatest integer. ...

August 6, 2022 · 3 min · 463 words · Ryan Sheehy

Python Int Function: How It Works And Why You May Need To Create Your Own

How does the int() function work I. Python and could you write your own function? The int(x, base=10) function in Python takes two parameters: the first parameter x being either a number or string and the second representing the base number to return (10 being default which represents the decimal number system) and converts x into a whole integer number. A simple example of converting a string is demonstrated below: ...

March 29, 2022 · 7 min · 1440 words · Ryan Sheehy

** In Python In 2 Minutes With Code Examples

What operator is used to raise a number to a power in Python? In Python the double asterisk operator ** is used to help calculate the exponent of a number raised to a power. This is done without a need to import the Python math library. For example, 2 to the power of 3 can be expressed using the double asterisk operator 2 ** 3, here the resulting output with the Python REPL: ...

March 20, 2022 · 3 min · 448 words · Ryan Sheehy

Convert Percentage String To Decimal Like 30% To 0.3 In Python

How do you convert a string representing a percentage number into an actual number that can be used to perform calculations? To convert a string represented as a percentage number into an actual decimal number in Python use the built-in float() function after removing the percent symbol from the string and any other characters that prevent numeric conversion. Without purging from the original string the percent symbol you will get a ValueError as demonstrated below in the Python REPL: ...

February 10, 2022 · 4 min · 658 words · Ryan Sheehy

Square A Number In Python Without Importing Math Library

How do you square a number in Python, and how do you check if a number is a square using Python? To square a number in Python use the power operator ** followed by the number 2. For example, to square the number 9 simply write 9 ** 2. Here’s an example demonstrating the use of this operator when squaring a number: 1 2 3 4 5 6 >>> 5 ** 2 25.0 >>> 12 ** 2 144.0 >>> -9 ** 2 -81.0 Wait! I thought squaring a negative number produces a positive number? Doesn’t -9 x -9 = +81? Why then does Python give a negative answer? ...

January 14, 2022 · 4 min · 815 words · Ryan Sheehy

Python ++ How To Increment A Variable (Code Examples)

How do you increment an integer variable in Python? Many other languages use the double plus sign operator on the variable to increment by 1, but what is Python’s plus plus operator equivalent? Python does not yet (as of version 3.9) have the ++ operator. Instead to increment an integer variable in Python by 1 use the operator assignment syntax i += 1. Is There A ++ In Python? As of Python version 3.9 there is no double plus operator. This type of operation in other languages increments an integer variable by one. ...

December 24, 2021 · 3 min · 481 words · Ryan Sheehy

How To Check If A Number In Python Is Even Or Odd

How can you tell if a number is odd or even in Python? The easiest way to determine if a number is even or odd in Python is to use the modulus operator. This operator, denoted as the percentage sign % displays the remainder from a division operation. To determine if a number is even simply apply my_number % 2 == 0 where my_number is your number and if this result is True then your number is even, otherwise, it is odd. ...

December 24, 2021 · 2 min · 420 words · Ryan Sheehy

Python Increment By 1

How do you increase a variable by one in Python? In other popular programming languages to increment a variable by one you can use the simple increment syntax of ++, for example i++ will make the value of i increase by 1, but how do you do this in Python? To increment a variable in Python use the syntax += 1, for example to increment the variable i by 1 write i += 1. This is the shorter version of the longer form syntax i = i + 1. ...

October 24, 2021 · 6 min · 1079 words · Ryan Sheehy