Introduction to python & installation
Python
In python tutorial, you can learn basic to advanced level python programming topics with examples. In this python tutorial, we covered python functions, python tuples, python modules, python arrays, python iterators, python closures, etc. for beginners and experienced with examples.
Python Tutorial Overview
Python is an object-oriented, interpreted, and high-level cross-platform programming language. Due to its interpreted nature and elegant syntax, the python is an ideal programming language for developers to build applications easily and efficiently. Python is used successfully in thousands of real-world business applications worldwide, including?many large and mission-critical systems.
Python Introduction
Here you will learn the basics of python programming language like what is python, advantages python, features of python, and the history of python programming language.
What is Python?
Python is an object-oriented, interpreted, and high-level programming language. Due to its interpreted nature and elegant syntax, python is an ideal programming language for developers to easily and efficiently build the applications.
?Guido Van Rossum?created the Python language, and it was released in 1991 as a successor of ABC programming language. He named it a python after the BBC great comedy show ‘Monty Python's Flying Circus’.
?Python is a free, open-source, most popular programming language, and it runs on different platforms like Windows, Linux, Mac, etc. Nowadays, python is using in thousands of real-world business applications worldwide, including many large and mission-critical systems.
Advantages of Python
The following are the advantages of using a python programming language.
? Due to its simple syntax, dynamic typing, and interpreted nature, python is the best choice for rapid application development.
? Python is free and open-source software. So, we can use it freely for commercial purposes.
? Python will allow the developers to write programs with fewer code lines than other programming languages like JAVA, C#, etc.
? Python is a cross-platform language. So, we can easily move and run the python applications between different OS platforms without any changes.
? Due to its readability nature, learning python is very easy when compared with other programming languages.
? Python language helps build applications in many areas like web development, data science, machine learning, etc.?
? We can easily integrate python language with other programming languages such as C, C++, JAVA, etc.
Features of Python
The following features of python will make it a language most people like to work with.
? The python code syntax is very easy to read, and it resembles the same as the English language.
? Python is an interpreter-based language that means the code will execute as soon as it was written.
? Python supports automatic memory management. You don’t need to worry about the allocation and deallocation of memory while writing the programs.
? In python, variables can be dynamically typed as well as strongly typed.
? It supports several programming paradigms, can be used for imperative, Procedural Programming, Functional Programming, and Object-Oriented Programming.
? Python supports basic data types like numbers, strings, and complex types like lists and dictionaries that can greatly simplify data processing.
? Python is an open-source language, and it is licensed under GPL. Python is becoming better day by day with the help of a large group of the programming community.
? Python has extensive free standard libraries to build web applications, machine learning, data science, etc. applications.
History of Python
Guido Van Rossum?has created the python language, and the initial version (0.9) was released in February 1991. The first python version includes exception handling, functions, data types, modules, etc.
After 3 years, the next python version (1.0) was released in January 1994 by including features like functional programming tools (lambda, map, filter, and reduce), functions with arguments, etc.?
The next Python version?2.0,?was?released in October 2000 after six and half years by including many features like Unicode support, list comprehensions, full garbage collector, etc.
Python continued the development in?2.x?versions for 8 years and released the next python version?3.0?in December 2008 to fix the previous version problems. The problem is the python version?3.0?is not fully backward compatible with the previous versions. The current Python version is?3.8, released in October 2019.
Currently, the Python Software Foundation (PSF) supports both 2.x and 3.x versions, but there will be no further development support for python 2. The Python version?2.7?is the final version, and will no longer be supported after 2020.?
Python Application Types
In the last few years, python has become the most popular language to build applications in many areas. The following are the few areas where the python programming language becomes much popular.?
? Data Science: Python is having extensive libraries like Numpy (Numerical python for numerical related operations), Pandas (for data related operations), Matplotlib (for graphs and charts), etc. to analyze the raw data.
? Machine learning: This is one of the areas where Python is most popular because of its wide variety of libraries like Scikit-learn, Tensorflow (used for neural networks), NLTK (natural language tool kit for language-related and speech-related applications), etc.
? Image Processing: Python becomes the best choice in face detection and image process areas because of the OpenCV library.
? Web Development: In the web development area, also python is most popular because of the libraries like Django, Flask, etc. make the web applications development easy.
? Gaming Development: For games development also python is the popular choice because of the PyGame library.
? Automated Jobs: It is extremely useful in automating jobs.
Python Installation
As we learned in the previous?Python Introduction?article, python is a free, open-source, and cross-platform language. So, the python can run on multiple OS platforms like Windows, Linux, Mac, etc.
We will now learn how to install python or set up a Python development environment on different OS platforms like Windows, Mac, and Linux machines.
Download and Install Python
Before you start python installation, first verify whether the python has already installed on your machine or not. Nowadays, most of the devices are coming with preinstalled python.
To verify python installation, open the command prompt (cmd.exe) or Terminal and type the command like?python --version. If you found that python has not installed on your machine, follow the below steps based on your OS version.
Python Installation on Windows
To install Python on a Windows machine, visit?download python?Url to download and install the latest python version. When you open the URL, it will automatically detect your OS and display the download link as per your operating system like as shown below.
When you click on the?Download Python 3.8.3?button, it will download the?python-3.8.3.exe?file for a?32-bit?version. If you want to download the?64-bit?version, visit the?python for windows?page and download the appropriate 64-bit installer like as shown below.
As you have downloaded the python installer for windows, install python on your machine by double-clicking on the setup file to start the installation process like as shown below.
If you choose the?Install Now?option, it will install Python in the default installation folder (C:\Users\{UserName}\AppData\Local\Programs\Python\Python38) with default settings. If you want to customize the python installation folder location & features, you can choose the?Customize installation?option. Select?Add Python 3.8 to path?option so that you can execute the python from any path.
If you choose the?Customize installation?option in the next step, it will show the?Optional Features?to select like as shown below.
After selecting the required features, click the?Next?button to continue. In the next step in Advanced Options, choose to?Install for all users?option so that all the machine users can execute the python programs. Also, change the python installation folder location and click the?Install?button by keeping all the default options as shown below.
?After you click on the?Installation?button, the python setup process will start, and you can see the progress of the python installation like as shown below.
?After completing the python installation, you will see the success message window like as shown below, and click on the?Close?button to close the setup wizard.
?Verify Python Installation
After completing python installation on your machine, you can verify it by opening the command prompt and typing?python --version?command.?
If python is installed successfully, it will display the version of python installed on your machine as shown below.
Python Installation on Mac
To install Python on Mac machines, visit?Python for Mac OS?page, download and install the latest Python 3.x version based on your requirements.
Python Installation on Linux
Nowadays, Linux machines are coming with preinstalled python. To verify whether the python installed or not, open the command line and execute the following command.
??python --version
If you find that python is not installed, run the following commands to install python.
?$ sudo apt-get update
$ sudo apt-get install python3.8
领英推荐
Python Interpreter (Shell)
In the previous chapter, we learned how to?install python?on different OS machines. In case if you didn’t install python, check the?python installation?article.
?When you install python on your machine, it will install the python interpreter or python shell, IDLE, and other required components to implement the applications using the python programming language.
As python is an interpreted programming language, you can execute the Python programs in two ways, i.e., directly write and run the code in python interpreter command-line interface or write code in the source (.py) file and execute the file from python interpreter. Now, we will learn how to write and run python programs using both methods.
Using Python Interpreter
To open the python interpreter command-line interface, search for?python?in the start menu and click on the?Python?app like as shown below.
?After opening the python interpreter command-line interface, you will see the greater than (>>>) symbols like as shown below, that means you are ready to start typing your code.
?After opening your python interpreter, type?print("Hello World")?command in the console and click on the?Enter?button to print the?Hello World?text like as shown below.
?If you observe the above result, we tried to print a message in the python shell by typing the message?print("Hello World"). After we type a message and click on?Enter, immediately it returned the string whatever we mentioned in a terminal.
Using Command Prompt
Instead of a Python shell or interpreter, you can also execute the python code through a command prompt for that open the command prompt, type?python, and press the?Enter?button like as shown below.
?Whenever the python started, you will see the greater than (>>>) symbol that means you are ready to start typing your code. If you want, you can also perform different operations like as shown below.
?If you observe the above results, the python shell or interpreter executes a single statement and returns the results. If you want to execute the multiple code lines, you need to create python files to execute the code using a python interpreter.
Using Files in Python
As python is an interpreted programming language, you can write the python code using different text editors and save those files with?.py?extension. After that, you can execute that python (.py) files either using direct python shell or by enabling the python in command prompt.
You can open any text editor like Notepad, create a file called?Helloworld.py?and write the code like as shown below.
print("Welcome to Tutlane.com")
print("Learn Python with Examples")
Now, the python files can execute by using command prompt (cmd) for that open command prompt and navigate to the folder that contains the?Helloworld.py?file like as shown below.
?Generally, the python files can execute by entering a command like?python <Your_File_Name>, so enter a command?python Helloworld.py?and hit the?enter?button.
Instead of the Python shell or command prompt, you can also execute the code using Python IDLE. In the next chapter, we will learn more about Python IDLE to execute the Python programs.
Exit Python Interpreter Prompt
After completing python code execution, if you want to exit or quit the python interpreter or command prompt, enter command?exit()?and press?Enter?to exit the python interpreter prompt.
Python Hello World Program
In the previous chapter, we learned how to?install python?on different OS machines. In case if you didn’t install python, check the?python installation?article.
As python is an interpreted programming language, you can execute the Python programs in two ways, i.e., directly write and run the code in python interpreter command-line interface or write code in the source (.py) file and execute the file from python interpreter. Now, we will learn how to write and run python programs using both methods.
Using Python Interpreter
When you install python on your machine, it will install the python interpreter or python shell, IDLE, and other required components to implement the applications using a python programming language.
To open the python interpreter command-line interface, search for?python?in the start menu and click on the?Python?app like as shown below.
?After opening the python interpreter command-line interface, you will see the greater than (>>>) symbols like as shown below, that means you are ready to start typing your code.
?After opening your python interpreter, type?print("Hello World")?command in the console and click on the?Enter?button to print the?Hello World?text like as shown below.
?
?If you observe the above result, we tried to print a message in the python shell by typing the message?print("Hello World"). After we type a message and click on?Enter, immediately it returned the string whatever we mentioned in a terminal.
Using Command Prompt
Instead of a Python shell or interpreter, you can also execute the python code through a command prompt for that open the command prompt, type?python, and press the?Enter?button like as shown below.
?Whenever the python started, you will see the greater than (>>>) symbol that means you are ready to start typing your code. If you want, you can also perform different operations like as shown below.
?If you observe the above results, the python shell or interpreter executes a single statement and returns the results. If you want to execute the multiple code lines, you need to create python files to execute the code using a python interpreter.
Using Files in Python
As python is an interpreted programming language, you can write the python code using different text editors and save those files with?.py?extension. After that, you can execute that python (.py) files either using direct python shell or by enabling the python in command prompt.
?You can open any text editor like Notepad, create a file called?Helloworld.py?and write the code like as shown below.
print("Welcome to Tutlane.com")
print("Learn Python with Examples")
Now, the python files can execute by using command prompt (cmd) for that open command prompt and navigate to the folder that contains the?Helloworld.py?file like as shown below.
?Generally, the python files can execute by entering a command like?python <Your_File_Name>, so enter a command?python Helloworld.py?and hit the?enter?button.
?Instead of the Python shell or command prompt, you can also execute the code using Python IDLE. In the next chapter, we will learn more about Python IDLE to execute the Python programs.
Comparing Python to Other Languages
Disclaimer:?This essay was written sometime in 1997. It shows its age. It is retained here merely as a historical artifact. --Guido van Rossum
Python is often compared to other interpreted languages such as Java, JavaScript, Perl, Tcl, or Smalltalk. Comparisons to C++, Common Lisp and Scheme can also be enlightening. In this section I will briefly compare Python to each of these languages. These comparisons concentrate on language issues only. In practice, the choice of a programming language is often dictated by other real-world constraints such as cost, availability, training, and prior investment, or even emotional attachment. Since these aspects are highly variable, it seems a waste of time to consider them much for this comparison.
Java
Python programs are generally expected to run slower than Java programs, but they also take much less time to develop. Python programs are typically 3-5 times shorter than equivalent Java programs. This difference can be attributed to Python's built-in high-level data types and its dynamic typing. For example, a Python programmer wastes no time declaring the types of arguments or variables, and Python's powerful polymorphic list and dictionary types, for which rich syntactic support is built straight into the language, find a use in almost every Python program. Because of the run-time typing, Python's run time must work harder than Java's. For example, when evaluating the expression a+b, it must first inspect the objects a and b to find out their type, which is not known at compile time. It then invokes the appropriate addition operation, which may be an overloaded user-defined method. Java, on the other hand, can perform an efficient integer or floating point addition, but requires variable declarations for a and b, and does not allow overloading of the + operator for instances of user-defined classes.
For these reasons, Python is much better suited as a "glue" language, while Java is better characterized as a low-level implementation language. In fact, the two together make an excellent combination. Components can be developed in Java and combined to form applications in Python; Python can also be used to prototype components until their design can be "hardened" in a Java implementation. To support this type of development, a Python implementation written in Java is under development, which allows calling Python code from Java and vice versa. In this implementation, Python source code is translated to Java bytecode (with help from a run-time library to support Python's dynamic semantics).
Javascript
Python's "object-based" subset is roughly equivalent to JavaScript. Like JavaScript (and unlike Java), Python supports a programming style that uses simple functions and variables without engaging in class definitions. However, for JavaScript, that's all there is. Python, on the other hand, supports writing much larger programs and better code reuse through a true object-oriented programming style, where classes and inheritance play an important role.
Perl
Python and Perl come from a similar background (Unix scripting, which both have long outgrown), and sport many similar features, but have a different philosophy. Perl emphasizes support for common application-oriented tasks, e.g. by having built-in regular expressions, file scanning and report generating features. Python emphasizes support for common programming methodologies such as data structure design and object-oriented programming, and encourages programmers to write readable (and thus maintainable) code by providing an elegant but not overly cryptic notation. As a consequence, Python comes close to Perl but rarely beats it in its original application domain; however Python has an applicability well beyond Perl's niche.
Tcl
Like Python, Tcl is usable as an application extension language, as well as a stand-alone programming language. However, Tcl, which traditionally stores all data as strings, is weak on data structures, and executes typical code much slower than Python. Tcl also lacks features needed for writing large programs, such as modular namespaces. Thus, while a "typical" large application using Tcl usually contains Tcl extensions written in C or C++ that are specific to that application, an equivalent Python application can often be written in "pure Python". Of course, pure Python development is much quicker than having to write and debug a C or C++ component. It has been said that Tcl's one redeeming quality is the Tk toolkit. Python has adopted an interface to Tk as its standard GUI component library.
Tcl 8.0 addresses the speed issuse by providing a bytecode compiler with limited data type support, and adds namespaces. However, it is still a much more cumbersome programming language.
Smalltalk
Perhaps the biggest difference between Python and Smalltalk is Python's more "mainstream" syntax, which gives it a leg up on programmer training. Like Smalltalk, Python has dynamic typing and binding, and everything in Python is an object. However, Python distinguishes built-in object types from user-defined classes, and currently doesn't allow inheritance from built-in types. Smalltalk's standard library of collection data types is more refined, while Python's library has more facilities for dealing with Internet and WWW realities such as email, HTML and FTP.
Python has a different philosophy regarding the development environment and distribution of code. Where Smalltalk traditionally has a monolithic "system image" which comprises both the environment and the user's program, Python stores both standard modules and user modules in individual files which can easily be rearranged or distributed outside the system. One consequence is that there is more than one option for attaching a Graphical User Interface (GUI) to a Python program, since the GUI is not built into the system.
C++
Almost everything said for Java also applies for C++, just more so: where Python code is typically 3-5 times shorter than equivalent Java code, it is often 5-10 times shorter than equivalent C++ code! Anecdotal evidence suggests that one Python programmer can finish in two months what two C++ programmers can't complete in a year. Python shines as a glue language, used to combine components written in C++.
Common Lisp and Scheme
These languages are close to Python in their dynamic semantics, but so different in their approach to syntax that a comparison becomes almost a religious argument: is Lisp's lack of syntax an advantage or a disadvantage? It should be noted that Python has introspective capabilities similar to those of Lisp, and Python programs can construct and execute program fragments on the fly. Usually, real-world properties are decisive: Common Lisp is big (in every sense), and the Scheme world is fragmented between many incompatible versions, where Python has a single, free, compact implementation.