Javatpoint Logo
Javatpoint Logo

How to install Python on Ubuntu 16.04 LTS?

Introduction

Python is a high level, dynamic and general purpose programming language. It was designed and developed by Python Software Foundation.

Python is a cross-platform language and was first released on 20, February 1991.

In this tutorial, we are working on the installation process of Python that includes the following steps.

Although Python 2.7 is installed by default in Ubuntu, but we can install other Python versions like python3.

To install python3, just type the following command in the terminal and it will be installed automatically.

History of Python

Python was founded in the late 1980s at Centrum Wiskunde & Information (CWI) by Guido van Rossum in the Netherlands as a replacement for the ABC programming language, influenced by SETL, able to interface and exception handling with Amoeba OS. Its implementation started in December 1989. Van Rossum carried sole responsibility for this project as a lead developer before 12 July 2018, when he disclosed his "permanent vacation" through his responsibilities as a "benevolent dictator for life" of Python.

In January 2019, active core developers of Python elected a five-member Steering Council for leading the project. The 2.0 version of Python was published on 16 October 2000, with several major new aspects, including Unicode support, reference counting, cycle-detecting garbage collection, and list comprehensions. The 3.0 version of Python was published on 3 December 2008, with several of its major aspects backported to the 2.6.x and 2.7.x versions of Python. Releases of the 3rd version of Python contain the 2to3 utility, which implements the Python 2 code translation to Python 3.

Features and design philosophy of Python

In Python, structured programming and object-oriented programming are completely supported, and various of their aspects support aspect-oriented programming and functional programming (including metaobjects and metaprogramming). Various other paradigms are also supported by extensions, such as logic programming and design by contract.

Python utilizes a dynamic typing solution of a cycle-detecting garbage collector and reference counting for memory management. It utilizes dynamic name resolution, which binds variable and method names at the time of program execution.

Its design provides some support in the Lisp tradition for functional programming. It has reduce, map, and filter functions, generate, set, dictionary, and list comprehension expressions. Also, the standard library has two different modules (functools and iteratools) that operate functional tools acquired from Standard ML and Haskell. Python philosophy is outlined in a document, i.e., The Zen of Python, which contains aphorisms.

Semantics and Syntax of Python

Python is defined to be an easily understandable language. Python formatting is uncluttered visually and often utilizes English keywords in which other languages apply punctuation. It doesn't apply curly brackets to bound blocks, and semicolons are allowed after any statement but are used rarely, unlike other languages. It has some special cases and syntactic exceptions than Pascal or C.

Indentation

Python utilizes whitespace indentation, instead of keywords or curly brackets, to bound blocks. An indentation increment comes after some statements, and an indentation decrement indicates the completion of the current block. Hence, the visual structure of the program correctly illustrates its semantic structure. The aspect is sometimes called the off-side rule.

Control flow and statements

Some important statements of Python are:

  • assignment statement with one equal sign (=).
  • if statement, which hypothetically runs a code block with elif and else.
  • for statement iterates on an iterable object, occupying all elements for a local variable by the attached block usage.
  • while statement runs a code block as its condition is true.
  • try statement permits exceptions raised in the attached block to be handled and caught by except clauses. Also, it ensures that in the finally block, clean-up code is always executed.
  • raise statement re-raises a caught exception or raises a specified exception.
  • class statement runs a code block and attaches the local namespace to the class in object-oriented programming.
  • def statement describes a method or function.
  • with statement encloses a block of code in the context manager, permitting resource-acquisition-is-initialization-like behavior and substituting a basic finally/try idiom.
  • break statement exits from a loop.
  • continue statement skips other current iterations and proceeds with the next.
  • del statement deletes a variable, deleting the reference through the name for the value and generating an error when the variable is specified before it's redefined.
  • pass statement syntactically requires to establish a blank code block, service as NOP.
  • assert statement utilized in debugging to inspect for conditions that should use.
  • yield statement returns from a generator function a value used to operate coroutines.
  • return statement returns a value using a function.
  • from and import statements are applied to import modules whose variables and functions can be applied in the current program.

Python doesn't support first-class continuation or tail call optimization, and it never will, according to Van Rossum. However, its coroutine-like functionality support is given by extending the generators of Python. Generators were considered lazy iterators, and data was unidirectionally passed out of the generator before the 2.5 version. It's possible to pass back data into the generator function from Python 2.5, which can be passed from two or more stack levels from Python 3.3.

Expressions

Expressions of Python are explained below:

  • The *, -, and + operators for mathematical multiplication, subtraction, and addition are the same as in other languages. However, the behavior of the division operator is different. Two division types are available in Python: floor division and floating-point division. The ** operator is used by Python for exponentiation.
  • The + operator is used by Python for string concatenation. The * operator is used by Python to duplicate any string a defined number of times.
    The infix (@) operator is intended to be utilized by libraries, including NumPy, for matrix multiplication.
  • The := syntax, known as the "walrus operator", was published in the 3.8 version of Python. It selects values for variables as an element of a larger expression.
  • The == operator compares via values in Python. The is operator of Python may be used for comparing object identities, and comparisons may be confined.
  • Python uses and, not, and or as Boolean operators.
  • Python includes an expression type known as a list comprehension, and a more basic expression is known as a generator expression.
  • With lambda expressions, anonymous functions are operated.
  • Conditional expressions are specified as 'x if c else y'.
  • Python makes a difference between tuples and lists. Lists are mutable that are specified as [1, 2, 3] and can't be used as directory keys. Tuples are immutable, specified as (1, 2, 3), and can be used as directory keys, given every element of the tuple is immutable.
  • Python provides sequence unpacking in which two or more expressions, all checking to anything that could be assigned, are related in an identical way.

Methods

On objects, methods are functions connected to the class of the object, the instance.method(argument) syntax is syntactic sugar for the Class.method(instance, argument) syntax is for normal functions and methods. Python methods included a self-explicit parameter to use instance data to the implicit self in a few other object-oriented programming languages (such as Ruby, Objective-C, Java, C++, etc.).

Also, Python facilitates methods, often known as dunder methods, to permit user-defined classes to change how they are managed by native tasks, such as comparison, length, in type conversion and arithmetic operations.

Typing

Python includes typed objects (untyped variable names as well) and utilizes duck typing. Type constraints aren't evaluated at compile time; instead, operations may fail on an object, indicating that it's not of a compatible type. Python language is strongly typed, blocking operations that aren't well-defined instead of silently trying to make sense of them, regardless of being dynamically typed.

Python permits programmers to specify their types with classes, most usually utilized for object-oriented programming. New class instances are created by calling the class, and the classes are the metaclass-type instances, permitting reflection and metaprogramming.

  • Before the 3.0 version, Python had two types of classes: new style and old style; current versions of Python support the new style only.
  • Python enables gradual typing.
  • The syntax of Python permits defining static types. However, they aren't evaluated in the CPython default implementation.
  • mypy, an optional experimental static type-checker, supports type-checking at compile-time.

Arithmetic operations

  • For arithmetic operations, Python includes the basic symbols (/, *, +, -), the % modulo operation, and the // floor division operator. For exponentiation, it has a ** operator and a @ matrix multiplication operator.
  • These operators implement like in common math along with similar precedence rules.
  • Python facilitates a round function to round a float to the closest integer.
  • Python 3 utilizes round to even: round(2.5) and round(1.5) both generate 2 for tie-breaking. Before 3, versions utilized round-away-from-zero: round(-0.5) will produce -1.0 and round(0.5) will produce 1.0.
  • Python permits Boolean expressions with two or more equality relations in a way that is compatible with general mathematics usage.
  • Python utilizes arbitrary-precision arithmetic for every integer operation. In the decimal module, the Decimal class/type offers decimal floating-point numbers to many rounding modes and a pre-defined arbitrary precision. In the fractions module, the Fraction class offers arbitrary precision for logical numbers.
  • Due to the third-party NumPy library and large mathematics library of Python that further enhanced to native capabilities, it's often utilized as a scientific scripting language to help in issues, including numerical data manipulation and processing.

Libraries of Python

An extensive standard library of Python offers tools compatible with several operations and is usually cited as its biggest strength. Several standard protocols and formats, including HTTP and MIME, are supported for Internet-facing software. It contains modules to create GUIs, connect to unit testing, use regular expressions, arithmetic using arbitrary-precision decimals, produce pseudorandom numbers, and relational databases.

A few components of the standard library are enclosed by specifications, e.g., the WSGI (Web Server Gateway Interface) implementation wsgiref pursues PEP 333. However, almost all are described by their code, test suites, and internal documentation. But, due to almost all standard libraries being cross-platform code of Python, only some modules require rewriting or altering for different implementations.

The official repository of third-party software of Python, the PyPI (Python Package Index), includes 4,15,000+ packages with a huge variety of functionality as of 14 November 2022, including:

  • Web scraping
  • Web frameworks
  • Text processing
  • Test frameworks
  • System administration
  • Scientific computing
  • Computer networking
  • Multimedia
  • Mobile apps
  • Machine learning
  • Image processing
  • Graphical user interface
  • Documentation
  • Databases
  • Data analytics
  • Automation

Development environments of Python

Almost all implementations of Python (such as CPython) contain a REPL (real-eval-print loop), allowing them to act like a command line interpreter for which clients sequentially input statements and immediately get results. Also, Python provides an IDE (Integrated Development Environment) known as IDLE, which is more suitable for beginners. Other shells, such as IPython and IDLE, have other features, including syntax highlighting, session state retention, and improved auto-completion.

The standard desktop IDEs and several Web browser-based IDEs are available, including SageMath, to develop math- and science-related programs; a hosting environment and browser-based IDE, PythonAnywhere; a commercial IDE focusing on scientific computing, Canopy IDE.

Implementations of Python

  • Reference implementation

Python's reference implementation is CPython. It's specified in C, matching the C89 standard with many select features of C99. CPython contains its C extensions, although third-party extensions aren't restricted to previous C versions; for example, they can be run with C++ or C11. It compiles the programs of Python into an intermediate bytecode. Then, it is run by its virtual machine (VM).

CPython is shared with a huge standard library specified in a mixture of native Python and C and is available for several platforms, such as Windows and the latest Unix-like systems, such as macOS, and unofficial support for VMS. One of the earliest priorities was platform portability.

  • Other implementations
    • Stackless Python is an important CPython fork that runs microthreads. It doesn't utilize the call stack in a similar way, hence, permitting concurrent programs massively. Also, PyPy includes a stackless version.
    • PyPy is a compliant and fast interpreter of the 3.8 and 2.7 versions of Python. Often, the just-in-time compiler of it brings an important speed improvement on top of CPython, but a few libraries specified in C can't be used with it.
    • Python is an alternative to Python runtime that utilizes just-in-time compilation to boost the Python program execution.
    • CircuitPython and MicroPython are Python 3 alternatives used for microcontrollers, such as Lego Mindstorms EV3.
    • Cinder is the performance-oriented fork of the 3.8 version of CPython that includes several optimizations, such as an experimental bytecode compiler, a method-at-a-time JIT, eager coroutines evaluation, and bytecode inline caching.
  • Unsupported implementations
    • Many just-in-time compilers of Python have been improved, but they are unsupported now:
    • In 2009, Google started a project called Unladen Swallow with the goal of boosting the Python interpreter fivefold with LLVM.
    • Psyco is a suspended just-in-time specializing compiler that combines with CPython and converts bytecode into machine code during runtime. Psyco doesn't support the 2.7 or later versions of Python.
    • In 2005, PyS60 was an interpreter of Python 2 for Series 60 mobile phones published by Nokia. It implemented several modules through the standard library and a few other modules to integrate with the Symbian OS.
  • Other language cross-compilers
    Many compilers are available for high-level object languages, along with a restricted Python subset, unrestricted Python, or a language the same as Python as a source language:
    • Pyjs, Transcrypt, and Brython compile Python intro JavaScript.
    • Nuitka compiles Python to C.
    • CPython compiles Python to C.
    • Numba utilizes LLVM for compiling a Python subset to machine code.
    • RPython compiles to C, and it is used to create the Python PyPy interpreter.
    • Pythron compiles the Python 3 subset to C++.
  • Performance

Several Python implementation performance comparisons on a non-numerical workload were available at EuroSciPy '13. The performance of Python compared to other languages is benchmarked through the Computer Language Benchmark Game.

Development of Python

Development of Python is largely conducted by the PEP (Python Enhancement Proposal) process, the main mechanism to propose bigger new aspects, document Python design decisions and collect community input on problems. The coding style of Python is described in PEP 8. PEPs are revised and commented on through the steering council and the Python community.

The language enhancement relates to the improvement of the CPython reference implementation. The python-dev mailing list is the main forum for the development of the language. Originally, specific problems were considered in the Roundup big tracker introduced by the foundation. In January 2017, until Python migrated to GitHub, every discussion and issue was shifted to GitHub in 2022. Originally, development took place on a source code repository executing Mercurial.

The public releases of Python provide three types, differentiated by which element of the version number is added:

  • Backward-incompatible releases, where code is supposed to break and requires to be ported manually. The first segment of the version number is added. These versions frequently happen- the 3.0 version was published eight years after the 2.0 version.
  • Feature or major releases are compatible largely with the older version but announce new aspects. The second segment of the version number is added. Starting with the 3.9 Python version, these releases are supposed to manually happen.
  • Bugfix releases, which announce no new aspects, appear around every three months and are created when enough number of bugs have been resolved upstream since the end release. Also, security vulnerabilities are patched in these versions. The third and last segment of the version number is added.

Also, several betas, alphas, and release-candidates are published as previews and to test before the last release. Often, they are delayed if the code isn't ready, although a rough schedule is available for all releases. The development team of Python checks the code state by executing the wide unit test suite at the time of development. On Python, PyCon is the biggest academic conference. Also, there are unique Python monitoring functions, including Pyladies.

Uses of Python

For web applications, Python serves as a scripting language, e.g., for the Apache web server by mod_wsgi. A standard API has been derived to provide these apps using Web Server Gateway Interface. Web frameworks, including Zope, Bottle, Flask, Tornado, web2py, TurboGears, Pyramid, Pylons, and Django, support developers in the creation and maintenance of complicated applications. IronPython and Pyjs can be used to improve the Ajax-based application's client side.

  • SQLAlchemy can be utilized as a data mapper to the relational database. A framework, i.e., Twisted, is used to build communications between systems and is utilized by Dropbox.
  • Many libraries, including Matplotlib, SciPy, and NumPy, use Python effectively in scientific computing, along with specialized libraries, including Astropy and Biopython, offering domain-specific functionality.
  • A computer algebra system using a notebook interface, i.e., SageMath, is programmable in Python; the library of SageMath covers several mathematics aspects, such as calculus, number theory, numerical mathematics, combinators, and algebra.
  • OpenCV includes Python bindings with a set of aspects for image processing and computer vision.
  • Commonly, Python is used in machine learning and AI projects with libraries, including scikit-learn, Pytorch, Keras, and TensorFlow.
  • Often, Python is utilized for natural language processing as a scripting language using a modular architecture, rich text processing tools, and simple syntax.
  • Also, Python can be used for GUI (Graphical User Interface) with libraries, such as Tkinter.
  • Also, Python can be used to make games using libraries, which can create 2D games, such as Pygame.
  • Python has been embedded successfully as a scripting language in various software products, such as in infinite element method software like Abaqus, 3D animation packages like Softimage, MotionBuilder, modo, Maya, Houdini, Lightwave, Cinema 4D, Blender, and 3ds Max, 3D parametric modelers, 2D imaging programs like Paint Shop Pro, Inkscape, and GIMP, and musical notation programs like capella and scorewriter.

Languages affected by Python

The design and philosophy of Python have affected various programming languages:

  • Cobra uses the same syntax and indentation, and document, i.e., Acknowledgements, first lists Python among languages that affected it.
  • Boo uses the same syntax, object model, and indentation.
  • CoffeeScript includes Python-inspired syntax.
  • JavaScript/ECMAScript borrowed generators and iterators from Python.
  • Go is developed for the working speed in a dynamic language, such as Python, and shares a similar syntax to slice arrays.
  • GDScript, which is a scripting language the same as Python, is pre-installed in the Godot game engine.
  • Groovy was inspired by the ambition to deliver the design philosophy of Python to Java.
  • Currently, Mojo is a non-strict Python superset and is up to 35,000 times faster for a few codes, where static typing aids.
  • Julia was developed to be usable as Python for general programming.
  • Nim uses the same syntax and indentation.
  • Swift is a programming language designed by Apple and includes a few Python-inspired syntaxes.

Also, the development practices of Python have been copied by other languages. For instance, the practice of needing a document defining the rationale for modifying the language is also used in Swift, Erlang, and Tcl.

Python Installation

1) Update the APT Repository

2) Install Python

2) Verify Python

When we type python it shows default installed python that is 2.7.


Software Python 1

For Python3 type the following command, then it will show the other version as well.


Software Python 2

Well, on the basis of these commands, we can test application for both Python versions.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA