Python is a new powerful, popular and demanding programming language in the market. Well, this does not explain it all as there’s much more to dig into it!
Here, we are going to explore all about it like what is it, why we need to learn it, what syntax are and how to create different programs using this language. So, let’s get started…
What is python language?
Python is a general purpose interpreted, high level and object-oriented programming language which can be used for web application development.
Here, Interpreted means there is no compilation requires for the python program. It allows a programmer to do programming in the different styles. This language runs on different platforms (different operating systems) like Windows, Linux, and Unix operating system. It is a free and open source programming language.
Why learn python?
First of all, it is the most important to know that why we should learn python. Following are the reasons:
- It is a scripting and high-level programming language.
- This language is totally different from other programming languages.
- It is easy to understand, has the simplest syntax, and has large numbers of libraries modules (built-in functions).
- Python performs a variety of task that can be used in a Database application, Desktop application, Network programming application and Game programming application etc.
Advantages of python language
- Python is easy to learn and it has simple syntax and structure compared to other languages.
- As the code in this language are already interpreted codes that mean there is no need to compile programs or codes.
- It is portable, means we can runs programs or codes on different hardware platforms.
- It is free to download and open source programming language this means you can download python from their official websites without paying any cost.
- One more important advantage of python is that it provides interfaces for large commercial databases.
- This language supports Graphical user interface(GUI) by which we can create libraries and windows systems.
- It supports structural and functional programming methods and also supports OOPs concepts. Structural programming in terms that we create programs by making subroutines, Block structure, for and while loops. It uses conditional statements (such as if-else) and also uses GoTo statement which is more difficult to follow and maintain. But in functional programming, we make functions in the program.
- Garbage collection is the facility which is provided by python also known as the collector. A garbage collector is used to free the memory which is occupied by the different objects that are no longer used in the program.
- It can be easily integrated with C, C++ and Java programming languages as it uses OOPs concepts. Also with Com stands for Component Object Model.
You can get most up to date or updated Source code, News, Binaries, and documentation etc. are available on official website of python https://www.python.org/.
You can download documentation from https://www.python.org/doc/. The documentation is available in HTML, PDF and PostScript formats.
Python Installation Process
Step 1: How to install and setup python on windows?
Here, I am going to define step by step process of how to install it on Windows, Unix, Linux and other different operating systems. So first, we are going to show how to install on windows.
Here are the steps to install it on windows:
- First, open your browser which is installed on your system and go to https://www.python.org/downloads/.
- After visiting this website follow the windows installer python-abc.msi file where abc is the file version you need to install.
- If you want to use this file python-abc.msi, your windows must support Microsoft installer 2.0. Save installer file to your local windows machine and run it to find the output if your local machine supports MSI.
- The next step is to run the download file. After running this file, windows will show a Wizard, so just accepts the default setting and wait until the installation is finished.
Step 2: How to install python on Unix and Linux?
As like windows installation here is the simple steps to install it on Unix and Linux operating systems.
- First, visit your browser as like windows installation and type the link: https://www.python.org/downloads/.
- After typing the link, a page will be open. Follow the instruction on the web page and download the zipped source code which is available for Unix and Linux.
- The third step is to download and extract the files (a zipped file which is downloaded from the link).
- If you want to customize some options edit the modules/ setup of a file.
- run ./configure script.
- Do install.
The location of the file will be /user/local/bin. All of its libraries will be at /user/local/lib/python-AA where AA is the version of python.
Step 3: How to install python on Macintosh (mac)?
- As like windows and Unix/ Linux go to the official websites https://www.python.org/downloads/mac/.
- Follow the instruction on the page.
- You can download the latest version for mac.
Setting Path at Windows
To set path at windows you follow the step.
- At the command prompt type path %path%;C:\python
- Press enter.
Note– C:\python is the path of python directory.
How to Set Environment Variables Path in Python for Windows?
Here, I am going to define how can we add environment variable path for windows 7. To set the path, follow the given steps-
My Computer -> Properties -> Advanced System Setting -> Environment Variables.
After doing this simple step then go to under system variable and create a new variable called Python Path. In this variable, I have the following path –
This is the best way to set the environment variable path for windows in the python language.
Setting Path at Unix and Linux
To add the path 0f python directory for the Unix/Linux operating system follow the steps-
1.In the csh shell- Type set environment path “$PATH:/usr/local/bin/python” and press enter.
2.In the bash shell(Linux)- Type export ATH=”$PATH:/usr/local/bin/python” and press enter.
3.In the sh or ksh shell- Type PATH=”$PATH:/usr/local/bin/python” and press enter.
Note-/usr/local/bin/python is the path of the Python directory
How to run python on Windows DOS and Unix/Linux?
There are three different ways to start with this process. Here they are:
Interactive Interpreter (Shell)
You can run python from Dos, Unix and other different operating systems that provides you a command line interpreter or shell window.
Enter Python at a command line and write the command at the interactive interpreter.
$python # Unix/Linux or python% # Unix/Linux or C:> python # Windows/DOS .
Script from the command line
If you want to execute a script at a command line, first you need to call interactive interpreter in an application.
$python script.py # Unix/Linux or python% script.py # Unix/Linux or C: >python script.py # Windows/DOS.
Running Python Program
Executing Programs in Python
Here, we see how we can execute programs in python. There are different modes by which we can execute a program in this language. First is Interactive mode and second is Scripting mode.
Interactive Mode Programming (Live Interpreter)
If you use this type of programming we do not pass script as a parameter only calling interpreter it show prompt-
$ python Python 2.4.3 (#1, Nov 11 2010, 13:34:43) [GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2 Type "help", "copyright", "credits" or "license" for more information >>>
Type following text at the prompt and then press enter key.
>>> print "Hello, Welcome!";
If you are using the latest version of python, then you would need to use print with parenthesis like print().
Every programmer should keep in mind one thing that while working with python live interpreter, everything is real and interpreted in real time.
Python shell is typically used to execute code interactively because it is a live interpreter. If you want to run a script from an interpreter, you must either import or include it or call the python executable.
Scripting Mode Programming
The second mode of programming is script mode. In that programming, we can call the interpreter with a script parameter and start the execution whenever the script finished. After the execution, an interpreter is no longer active.
The extension of the file is .PY which indicate to the operating system and the programmer that the file is actually a Python file. After interpreter called, it reads and executes the file. Here is an example of script mode suppose file name is str.py –
print “Hello, Welcome!”
If you have python interpreter then set in the path variable, then do this code-
$ python test.py
This produces the following output as-
We can run this script by another way and that is as below-
#!/usr/bin/python print "Hello, Welcome!"
Assume that python interpreter is available in the directory /usr/bin/. Now, try to run this program-
$ chmod +x test.py # This is to make file executable $./test.py
This produces the following output as-
Note– All the examples given in this subsequent are executed with python 2.4.3 version.
What is variable?
A variable is nothing but a reserved memory location to store values. When you create a variable you reserve some space in memory. In other words, a variable in a program gives data to the computer for work on.
Every value in a python has a datatype. Different data types in python are –
Variable can be declared by you according to the condition. It can be named, number etc.
How to declare and use a variable?
Here I am going to give an example in which I will declare a variable and print it.
The output of variable should be “200”
What are the Identifiers in python?
Identifiers are the name which is given to a variables, functions, modules or other objects. Identifiers can start with –
- letter a-z
- letter A-Z
- an Underscore(_)
- digit (0-9).
Python does not support punctuation characters such as $, and @.
Python is a case sensitive programming language.
Login works and login works both are different in identifiers.
Here are some naming convention for python identifiers-
- The class name always starts with an uppercase letter. All others are lower case.
- If you start an identifier with a single underscore that indicates identifier is private.
- If the identifier starts with two underscores indicates that identifier is more secure. Strongly means more secure.
- If the identifier ends with two trailing underscores it means the identifiers is a language-defined special name.
How to write programs in python?
Here is an example to show how we can write programs in python.
Program source code is given below:
Here is a source code of the program to check whether the given number is prime or not.
ak i a// + : a%i : k k+ k : :
Enter the number to check and find the desired output.
Runtime test cases
Enter number: 7
Number is prime
Enter number: 35
Number isn’t prime.
Here in this article, I have tried to explain about new popular programming language Python. It is more easy and open source programming language that means it is free to download and you can use their official websites. Nowadays, it is more demanding language in the industry.
So, I have clearly defined each part of this blog. In this article, I have defined all about installation, how to run programs in windows and Unix/Linux operating system etc. So, learn this programming language and go ahead.
I have tried my best to deliver easy, simple and valuable content regarding this new innovated programming language to learn step by step. I hope this blog will be more valuable and important for a new learner.