In this tutorial we are going to learn Python programming language starting from basic up-to it’s the advanced level.
Python is an open-source project, supported by many individuals. It is a platform-independent ( used in Windows, Mac, Linux, Raspberry Pi, etc.), scripted language, with complete access to operating system APIs. It was developed by Guido van Rossum, in February 1991. This allows users to integrate applications seamlessly to create high-powered, highly-focused applications. The contents of this article are shown below:
- Why do we need Python
- Python IDE
- Python IDLE
- Jupyter
- PyCharm
- PyDev
- Anaconda
- Spyder
- Python Version
- Python Installed
- Syntax
- Comment
- Indentation
Why do we need Python?
Python has been constantly improved by a community of users who have always strived hard to take it to the next level.
- Simple Python is a simple and small language. Reading a program in python is almost reading in English. Therefore, an advantage of python that allows programmers to concentrate on the solution to the problem rather than the language itself.
- Easy to learn Python program is clearly defined and easily readable. The structure of the program is very simple. The keywords and syntax are very easy to remember. This makes it easy for just anyone to pick up the language quickly.
- Versatile Python supports the development of a wide range of applications ranging from simple text processing to WWW browsers to games.
- Free & open-source Python is open-source software. So that anyone can read the source code, edit it, and even use the code to write new (free) programs.
- High-level language Programmers don’t have to worry about the memory used by the program like any low-level language. They just concentrate on writing the solutions to the problem.
- Object-Oriented Python supports object-oriented as well as a procedure-oriented programming approach. Object-oriented simply means that encapsulation of data and functionalities within objects. On the other hand, procedure-oriented techniques mean reusable pieces of programs.
- Secure The Python language environment is secure from tampering. Additional security checks can be easily added to implement additional security features. Apart from that, modules can easily be distributed for the prevention of altering the source code.
Python IDE
Python IDE refers to the Integrated Development Environment which integrates several tools specifically designed for software development. This includes :
- Builds, Execution, Debugging tools
- Some form of source control
- Designed to handle codes(like auto-completion, save and reload code files, run code from within the environment, auto code formatting, syntax highlighting.)
Python IDLE
Python IDLE intended to be a simple IDE for beginners, especially for educational purpose. It is packaged as an optional part of the Python packaging of many Linux distributors. IDLE has the following features:
- coded in 100% pure Python, using the Tkinter GUI toolkit
- cross-platform: works mostly the same on Windows, Unix, and MAC.
- Python shell window with colourizing of code input, output, and error messages.
- multi-window text editor with multiple undo, Python colourizing, smart indent, call tips, auto-completion, and other features
- search within any window, replace within editor windows, and search through multiple files.
- debugger with persistent breakpoints, stepping, and viewing of global and local namespaces
- configuration, browsers, and other dialogues.
After download and install it you can just write “IDLE” in the search box in the taskbar of your system and can see the app. Your Python IDLE display will be like this :
The following are the most used IDEs for Python:
Jupyter
The Jupyter Notebook supports markdowns, allowing you to add HTML components from images to videos. Thanks to Jupyter, you can easily see and edit your code in order to create compelling presentations. For instance, you can use data visualization libraries like Matplotlib and Seaborn and show your graphs in the same document where your code is. Besides all of this, you can export your final work to PDF and HTML files, or you can just export it as a .py file. In addition, you can also create blogs and presentations from your notebooks.
PyCharm
PyCharm is an IDE for professional developers. It is created by JetBrains, a company known for creating great software development tools. There are two versions of PyCharm:
- Community – free open-source version, lightweight, good for Python and scientific development.
- Professional – paid version, full-featured IDE with support for Web development as well.
PyCharm provides all major features that a good IDE should provide: code completion, code inspections, error-highlighting and fixes, debugging, version control system and code refactoring. All these features come out of the box.
Eclipse + PyDev
If you’ve spent any amount of time in the open-source community, you’ve heard about Eclipse. It has a rich marketplace of extensions which makes Eclipse useful for a wide range of development activities. One such extension is PyDev, which enables Python debugging, code completion, and an interactive Python console.
Installing PyDev into Eclipse is easy. From Eclipse, select Help, Eclipse Marketplace, then search for PyDev. Click Install and restart Eclipse if necessary.
Anaconda
Anaconda® is a package manager, an environment manager, a Python/R data science distribution, and a collection of over 7,500+ open-source packages. Anaconda is free and easy to install, and it offers free community support.
Spyder
Spyder is an open-source Python IDE that’s optimized for data science workflows. It comes included with the Anaconda package manager distribution, so depending on your setup you may already have it installed on your machine. What’s interesting about the Spyder is that its target audience is data scientists using Python. You’ll notice this throughout. For example, Spyder integrates well with common Python data science libraries like SciPy, NumPy, and Matplotlib.
Python Versions
Here are the versions of Python (old and new). Python 2 versions are being obsolete. The latest version which is used mostly everywhere is Python 3.8. In this tutorial, we will work on Python 3.7.4 version.
Python Install
Many PCs and MAC have already installed Python in it. Check if the Python is already installed or not we can simply go to command prompt window (in short cmd.exe), then check it.
C:\Users\Your Name>python --version
If the version is not showing then you can simply click this link —https://www.python.org/ and then download Python for your Windows version and install, then again check it in the command prompt.
Python Syntax
Python syntax can be executed by writing directly in the Command Line
print("hello World")
You can see like this —
Or, also we can create a python file on the server using .py type file extension and run it in the command line.
C:\Users\Your Name>python myfile.py
It can seem like the above.
Python has a set of keywords that are reserved. It can’t be used further as names of variables. Here is the list of Python keywords.
and | as | assert | break | class |
continue | def | del | elif | except |
False | finally | far | from | global |
if | import | in | is | lambda |
None | nonlocal | not | or | pass |
raise | return | True | try | while |
with | yield |
Python Comment
Comments are the non-executable statements in a program. Comments make the program easily readily and understandable by the programmer. Python uses a hash sign (#) to indicate a comment.
#This is a comment
print("hello World")
To make code more readable and understandable, Python uses multiple comments.
#This is a comment
print("Hello World") #To display hello
#The code ends here
A multi-lined comment uses triple quotes like (‘ ‘ ‘) or (” ” “)
def double(val):
"""function to double the val"""
return 2*val
Python Indentation
White-space at the beginning of the line is called indentation. These indentations are very important in Python. The leading white-space including spaces (by default Python puts 4 spaces, it can be changed by programmers.). Tabs at the beginning of the logical line determine the indentation level of that logical line. Program to exhibit indentation errors:
age= 21
print("You can vote")
An important thing to know is that we can only use 4 spaces under a condition(like any for statement)
If there another condition is introduced within the previous, for we should use 4 more spaces(i.e., 4+4=8 spaces) and so on.
for i in range(1,5):
print('Table of '+str(i))
for j in range(1,5):
x=i*j;
print(str(i)+'x'+str(j)+'='+str(x))
In the above code the j’s loop iterates within I’s loop. So, 4 spaces are used for the statements under I’s loop and 8 spaces for j’s loop.
The following is the output of the above code.
We should not leave space in Python. It will show an error like the below.
if(10>2)
print("10 is greater than 2")
print("10 is greater than 2")
If we don’t use same no. of spaces in the same block of codes Python will give an error.