What Does Double Slash Operation // Do In Python?

What is the double slash operator and what does the double slash operator // do in Python? The double slash operator in Python returns the quotient value from a division operation. The operator requires two numbers: a dividend and a divisor, which are the same numbers used with standard division operations in mathematics. For example, the mathematical expression 75 ÷ 10 has the 75 as the dividend, 10 as the divisor and returns the value 7 as quotient and 5 remainder. Therefore, when the double slash operator is used in Python on the same mathematical calculation the result will be just 7, as seen in the Python REPL below: ...

January 15, 2022 · 3 min · 506 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

Square Root In Python Without Importing Math Library

How do you find the square root of a number in Python? Can you find the square root without needing to import the math library? The most common approach in calculating the square root of a number in Python is to import the math library and use the method math.sqrt(n) where n is the number you want to square root. But there is an even easier way of being able to calculate the square root of a number in Python without having to import the math library. How is this done? ...

January 14, 2022 · 5 min · 1026 words · Ryan Sheehy

Find Duplicates In List: Python One Liner

How do you find all the duplicates from a list in Python? To find all the duplicate elements from a list using Python, use the following list comprehension: 1 [x for idx, x in enumerate(original_list) if x in original_list[idx+1:] and x not in original_list[:idx]] The result from the above code will be a list of unique elements representing all the duplicate elements from the original list. Here’s an example of how this looks in your Python console: 1 2 3 >>> my_list = [1, 1, 2, 3, 3, 3] >>> [x for idx, x in enumerate(my_list) if x in my_list[idx+1:] and x not in my_list[:idx]] [1, 3] Get List Of Duplicates What if you want a list of all the duplicate entries? ...

January 12, 2022 · 3 min · 496 words · Ryan Sheehy

Python Code To Remove Duplicates From List - 1 Liner

How do you remove duplicates from a list using Python? To remove duplicate elements from a list in Python use a list comprehension to create a new list with no duplicates with this code: [x for idx, x in enumerate(original_list) if x not in original_list[idx+1:]] Here is an example demonstrating how this code works: 1 2 3 4 >>> original_list = [1, 2, 1, 3, 1, 2, 3] >>> [x for idx, x in enumerate(original_list) if x not in original_list[idx+1:]] [1, 2, 3] As you can see from the example above the result from the list comprehension produces a unique list from the original. ...

January 11, 2022 · 3 min · 578 words · Ryan Sheehy

How To Add Blank Line In HTML

How do you add a blank line into HTML? Several ways are possible, with each unique approach depending on your specific use case and what is permissible for you to edit with the HTML. Reasons for why you might need to insert blank lines into your HTML code could be to render it in a PDF. One recent case for myself with a project was having an area at the bottom of a letter where somebody could sign. This might be useful for example when uploading the document to Adobe Sign and having it signed by a couple of parties. ...

January 6, 2022 · 8 min · 1613 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

How To Find Most Frequent Element In List In Python (Code Examples, No Import Statements)

How do you find the most common element in a list using Python without importing any special libraries? To find the most frequent elements in a list iterate through the list and store the frequency of each element into a dictionary with the key representing the element and the value representing the frequency. Finally, sort the dictionary by value in descending order to see the results with the highest frequency to the lowest. ...

December 22, 2021 · 4 min · 710 words · Ryan Sheehy

How To Use The SWITCH Function In Google Sheets?

The SWITCH() function in Google Sheets is handy when dealing with multiple criteria based on a single result. The SWITCH() function takes at least 3 parameters, with the first parameter being the value to evaluate, the second and third parameter representing a pair of combined cases and values. There’s also an optional final parameter that acts as the default result if no cases are satisfied. Here is a simple demonstration of the SWITCH function: ...

December 16, 2021 · 4 min · 830 words · Ryan Sheehy