HOW TO CREATE A PYTHON SCRIPT

 To create and run a python script, please make sure you have already installed python language and in most computers it is already there.A python script can be run on different OS platforms and can be used to perform a wide variety of system and user functions.

Let me show you how to create a simple python script.Use some good editor to start typing python code.You may use powerful tools like Visual Studio Code and PyCharm to start python coding.Open a blank page and start the code with lines like:

#!/usr/bin/python
#help text: this is my first script

The first line is shebang line and usually used in unix-like systems like Linux.It tells the location of python interpreter.The second line is a simple comment.We shall add more code to create a simple script which will ask user to input two numbers which can be added together.

#help text: simple script to input numbers to get the sum
number1 = input('Please enter first number: ')
number2 = input('Please enter second number: ')
total = number1 + number2
print(' ')
print('************************ RESULT ******************')
print('The sum of two numbers '+str(number1)+' and '+str(number2)+' is '+str(total))
print('**************************************************')

Copy the above code and save the file with name as myfirstscript.py
On windows,open the command prompt and type the file name to run the script.
On Linux/Mac, you may need to set appropriate permission before running the script:

chmod +x myfirstscript.py

python myfirstscript.py
 
Let 's try to understand the above code.The first line strating with # is simple comment and not executed by python.The second and third line define variables - number1 and number2.The fourth line calculate sum of the numbers.To display the output, we are using the in-built function print. The in-built function str is used to convert data with int(integer) type to str(string type) so that we can concatenate the text with the output data.  

Comments