**CHAPTER 10**

Python has a vast amount of libraries that you can import and make your work extremely easy as long as you understand how they work and what are the things that go behind the scenes when you use a particular method.

Here I have used a different library to show how much of your work can become easy when you start working with different modules and get familiar with them. Python is mostly used for Machine Learning as well as Data Analysis. So you can find a ton of things that you can do with this fun language as long as you know how to play with it.

Here I have shown a way to code a program that will help you find out if a particular number is prime or not. I have used very basic methodology to find it out and then I have shown another way to do the same time. I want you to observe the lines of code and how it works.

**Input :**

def checkPrime(n): '''to check if the number is prime or not. we will use the basic test of divisibility from mathematics.''' if(n==2 or n==3 or n== 5 or n==7 ): return True elif(n%2==0): return False elif(n%3==0): return False elif(n%5==0): return False elif(n%7==0): return False else: return True n=int(input("Enter Any Integer from 2-100\n")) result=checkPrime(n) if(result==True): print(n," is a prime number.") else: print(n," is not a prime number.")

**Output:**

Enter Any Integer from 2-100

45

45 is not a prime number.

45

45 is not a prime number.

**Input :**

import sympy n=int(input("Enter Any Integer from 2-100 to check if the number is prime or not.\n")) print (sympy.isprime(n))

**Output:**

Enter Any Integer from 2-100 to check if the number is prime or not.

45

False

45

False

- In the first program, I have created a little function that I call upon to check if a particular number is prime or not.
- In that I have made a loop to first pick out the basic prime numbers which we all know, following which I have made a little else-if ladder to find out if the number passes the test of divisibilities or not.
- If the number is not divisible by any of those then it is a prime number else it isn't a prime number.
- I have used the restriction for the number 1 as number one is a topic of debate when it comes to say it is prime or not. Some argue that it is a prime number while some say it is not, and it is something new. I forgot the term that was used for it and can't find in articles but if I do, I will surely update it here.
- Secondly, I have used only positive integers here for the sake of simplicity of the programming. You can include negative numbers as well as floating point numbers.
- If you can see, I haven't added any protection against invalid inputs here as I wanted to keep the code simple as possible. Since I wanted to show the difference in number of lines between using a library function by importing it and coding it manually.
- In the second code, I have imported a library and used it. Just like your math library which you can use to easily perform addition and other such basic math functions. Here this library holds a lot of special functions related to prime numbers. You can search on the web for other functions that this library has or use the help command to find out.
- This code is very simple hence I expect you to understand it, even though if you find anything difficult, please comment down below and I'd be happy to clear the doubts for you.

I hope you enjoyed as much as I did writing this chapter. Here you learnt something more about loops, variables as well as functions in a gist and how to go about using them and build your own logic for various things.

**~*~*~*~**

*This series is totally authored by me (Dhawal Joshi). Any similarities found on the text, or codes or anything is purely accidental. All the sources of reference will be mentioned, linked and will be given the proper credits. If I miss anything or there's anything wrong, feel free to comment or send me an email and I'll try to edit it out. I am not a Python expert, I am sharing whatever I have learnt on my own and with a few sources around to refer from which will be mentioned. Also feel free to share this series with others so most can benefit out of it.***~*~*~*~**

Do comment and share your thoughts about it! I'd love to know what do you think. Also, I'd keep updating it quite often so do follow the Website to get all the updates by clicking here.

**Also, a minor headsup.... Obsessed is free to read on Kindle Unlimited! Do check it out. I'd be glad to read your reviews!**

## 0 comments:

## Post a Comment

Do comment and share your thoughts.