programming in Python
In this post I am going to implement some Python probrams with using the Python program rules which I have discussed in my previous posts regarding to Python and let us to go
How to Run Python script
As we know we can run in two ways a Python program:
1- Run Python Code Interactively
Start command line prompt and writ python as follow:
you can be prompted to the >>> and now you can write print(some text) or calculation of number and what else as follow
You can finish with CTR+Z to come out from interactive.
2- Using the Python Command
Python code files can be created with any plain text editor. Open your favorite text editor and write the following code:
#!/usr/bin/env python3
print('Hello World!')
To run Python scripts with the python
command, you need to open a command-line and type in the word python
, followed by the path to your script (filename.py), just like this:
As you see the output is Hello, World.
Now I want to write programs in Python and run them via Python command.
Python Program to Check Prime Number
We want to write a program, ask from user to give a number as input and program checks it is a prime number or not
Open an editor like as VS code editor and create a Python file and give a name, I am giving name to this file CheckNumberIsPrime in the my path: C:\Utvecklingprogram\Python\Python_progs and write the following code:
# check a input numbber is prime or not
numb = 407 # default value
# To take input from the user
numb = int(input("Enter a number: "))
# if numb >1
if numb > 1:
# find the f irst factor
for i in range(2,numb):
if (numb % i) == 0: # if reminder is 0 (devidble)
print(numb,"is not a prime number")
print(i,"times",numb//i,"is",numb)
break
else:
print(numb,"is a prime number")
# In case input number is less than or equal to 1, it is not prime
else:
print(numb,"is not a prime number")
open command line prompt in the path: C:\Utvecklingprogram\Python\Python_progs and run the program:
As you see if user gives number 107 output is 107 is a prime number and if user gives 55 the output is 55 is not a prime numbber
5 times 11 is 55
We can change the code and use flag (isPrime) and give the file name: CheckNumberIsPrime_flag
The code shall be as following:
# Use global varible tocheck a number is prime or not
numb= 29 # default value
# Ask from user to give a numbber
numb = int(input("Enter a numbber: "))
# define a bool variable which can be uppdated in the loop
isPrime = False
# prime numbbers are greater than 1
if numb > 1:
# check for factors
for i in range(2, numb):
if (numb % i) == 0:
# if factor is found, set flag to True
isPrime = True
# break out of loop
break
# check the value of bool varis True
if isPrime:
print(numb, "is not a prime number")
else:
print(numb, "is a prime numer")
If you run the program shall give you the same output as before.
More about Python comments look to this link How To Comment Your Python Code as a Data Scientist
Conclusion
In this post I have write a Python program and run it with Python command line and change the code by using a flag variable as global variable and updated it in the for loop
All program Source code can be found in my Github
This post is part of python-step-by-step