Skip to Main Content
Advanced Python Programming
book

Advanced Python Programming

by Dr. Gabriele Lanaro, Quan Nguyen, Sakis Kasampalis
February 2019
Intermediate to advanced content levelIntermediate to advanced
672 pages
16h 50m
English
Packt Publishing
Content preview from Advanced Python Programming

Asynchronous prime-checking

Moving on from our starting counting-down example, let's reconsider the example from the previous chapter. As a refresher, the following is the code for the synchronous version of the program:

# Chapter16/example1.pyfrom math import sqrtdef is_prime(x):    print('Processing %i...' % x)    if x < 2:        print('%i is not a prime number.' % x)    elif x == 2:        print('%i is a prime number.' % x)    elif x % 2 == 0:        print('%i is not a prime number.' % x)    else:        limit = int(sqrt(x)) + 1        for i in range(3, limit, 2):            if x % i == 0:                print('%i is not a prime number.' % x)                return        print('%i is a prime number.' % x)if __name__ == '__main__':    is_prime(9637529763296797)    is_prime(427920331)    is_prime(157)

As we discussed in the last chapter, here, ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Advanced Python Programming - Second Edition

Advanced Python Programming - Second Edition

Quan Nguyen
Expert Python Programming - Third Edition

Expert Python Programming - Third Edition

Michał Jaworski, Tarek Ziadé, Cody Jackson
Python Workout

Python Workout

Reuven M. Lerner

Publisher Resources

ISBN: 9781838551216Supplemental Content