ALL Python Programmers Should Know This!! #python #programming #coding

preview_player
Показать описание

This quick demonstration shows you how powerful Python's filter() function can be!

Background Music:
Creative Commons Attribution-ShareAlike 3.0 Unported
Рекомендации по теме
Комментарии
Автор

Correction: 1 is not prime. My is_prime function is flawed!

b
Автор

Minor thing, but you can make the prime checking function faster by going up to floor(sqrt(n)) instead.

boredd
Автор

primes = [x for x in range(2, 1000) if isPrime(x)]

philfernandez
Автор

my man actually made the least efficient function in the history of functions

foxerity
Автор

"all python programers should know this: pep-8" when

aciddev_
Автор

A better is_prime function would be:

def is_prime(num):
if num == 1 or num == 0:
return False
for x in range(2, floor(sqrt(num))):
if num % n == 0:
return False
return True

This does three things. First, it factors in the fact that 1 is not prime. (It’s neither prime nor composite.) Second, it accounts for the fact that 0 isn’t prime either (as 0 is highly composite, having literally every integer as a factor). And third, it increases the speed of the function since it only needs to go to the square root of the number. This works because every composite number has at least one factor that is greater than 1 and less than or equal to its square root. Indeed, given any integer x such that x > 1, for every factor _a_ of x that is less than √x, there is exactly one other factor _b_ of x that is greater than √x such that _a_ × _b_ = x. (For 1, it has exactly one factor (itself), and its lone factor is also its square root. For 0, it has infinite factors (everything), but its square root (itself) is less than all other whole numbers.)

Incidentally, if num is less than 2, then both range(2, num) and range(2, floor(sqrt(num))) will return an empty iterable that will cause the body of the for loop to never be executed. As such, any value for num that is less than 2 will return True for the original version of is_prime, and any value for num that is less than 0, between 0 and 1, or between 1 and 2 will return True for my version of is_prime. We could add a check for nonintegers:

if num != floor(num):
return False

However, that isn’t strictly necessary, since the input should be an integer, and in Python, we assume the developer would not put a fraction into the function. And it’s not like the code would throw an error in such a case, anyways. The better question is with regards to negative numbers. There are four ways to consider this. First, we could just assume that the developer would never input a negative number. This is fine… unless we’re getting input from a user, in which case either the developer or the function should do a check.
The second way is to treat all negative numbers as composite on the grounds that they each have at least three factors: 1, itself, and −1 (where we only include −1 as a factor if the number itself is negative), and prime numbers must have exactly 2. In that case, our first conditional would instead be:

if num == 1 or num <= 0:
return False

(Though, to be more precise, it should be:

if num == 1 or num <= 0:
return num == -1

This is because, under that logic, −1 is prime since it has exactly two factors: 1 and −1. As such, is_prime(-1) ought to return True.)

The third idea is to have it so that a negative number is prime if and only if its opposite is also prime:

if num < 0:
num = -num
if num == 1 or num == 0:
return False

This will make it so that, if num is negative, it will run the calculations with its additive inverse instead. (num is a variable with a local scope, so we don’t have to worry about clobbering it.)

The final method is to throw an exception if num < 0. This would make it so that the function doesn’t assume what ought to be done if a negative number is input, as well as allowing for troubleshooting.

brianhull
Автор

For those concerned for its complexity, remember you can always use sieve of eratosthenes in most cases, this only would be required in case of big numbers

back
Автор

Even as a junior web developer, I really like how you do these shorts. It concisely and simply explains whats going on in the shorts.

AndrewMycol
Автор

You can also do primes = [i for i in list(nums) if is_prime(i)]

Siirxe
Автор

I usually prefer the list comprehension method instead of filter

ie:
[n for n in nums if is_prime(n)]

salvatorearpino
Автор

so if you had a list from 1 to 1000...
*proceeds to show a list from 1 to 999

txpvxlg
Автор

Sieve of Eratosthenes is much better for this, but only if u need nums from 1 sieve works with O(n) in spite of this which works withO(n*sqrt(n))

ubiabrail
Автор

scientists: use powerful computers to find new primes
me: types infinity intead of 1000

KillToGame
Автор

I would use a generator function to generate a (possibly infinite) sequence of primes by keeping a record of all previously found primes and checking if the next number is divisible by any of those (that should all be smaller than the number) in order. Uses a tiny bit more memory but cuts down on a lot of division operations.

Tomyb
Автор

For a relatively large number, the isprime func will take a long time to return true or false. Instead of checking every integer <= nums the isprime code should only consider previous primes up to nums.

reef
Автор

Dude makes me wanna get programing socks and learn python

uselessschmuck
Автор

You only need to check if it's divisible by smaller primes.

wiccanwanderer
Автор

Alternatively, you can use a conditional within a list comprehension: [ n for n in nums if is_prime(n) ]

AWriterWandering
Автор

I must admit that I'm a little mad that this didn't show up when I needed it but this tips are very cool and informative!

thcpuhr
Автор

I love python even more bcoz of you. You are absolutely amazing thanks & keep uploading such clips

newsjuice