What is Python?
Tags: python, beginners, programming
Published: 2022-02-16 13:36:13

In a nutshell — Python is an interpreted,  interactive, object-oriented, general-purpose programming language.

### Design philosophy & Features
* The Python language's core philosophy is summarized in [PEP 20](https://www.python.org/dev/peps/pep-0020/), titled _"The Zen of Python"_.
* Python is a multi-paradigm programming language where _Object-oriented programming_, _procedural programming_ and _functional programming_ are fully supported. Many of Python's features support _aspect-oriented programming_, including _metaprogramming_ and _metaobjects_ (also called _magic methods_). Many other paradigms like _design by contract_ and _logic programming_ are also supported via extensions.
* Python's extensive [standard library](https://docs.python.org/3/library/) is commonly cited as one of its greatest strengths and provides tools suited to many tasks. Rather than having all of this functionality built into its core, Python was designed to be highly extensible with [modules](https://docs.python.org/3/glossary.html#term-package).
* Python features _dynamic name resolution_ during runtime.
* Python is a _strongly-typed_ language, meaning you can't perform operations inappropriate to the [type](https://docs.python.org/3/glossary.html#term-type) of the [object](https://docs.python.org/3/glossary.html#term-object) (e.g. adding numbers to strings). Python is also a _dynamically-typed_ language, meaning a variable is simply a value bound to a name with no associated data type.
* Python uses _duck typing_, a concept related to dynamic typing — Python don't check types, it checks for the presence of a given method or attribute.
* Since Python 3.5, type [annotations](https://docs.python.org/3/glossary.html#term-annotation) can be used for possible static analysis, refactoring, runtime type checking and code generation. An optional static type checker named [mypy](https://github.com/python/mypy) supports compile-time type checking.
* Python allows you to _assign_ a value to a variable without first _declaring_ it, but it will not let you _reference_ a variable that has never been assigned a value.
* Python doesn't support _tail call_ optimization and _first-class continuations_. However, Python has better support for coroutine-like functionality by extending Python's [generator](https://docs.python.org/3/glossary.html#term-generator) functions.
* Python developers should strive to avoid _premature optimization_ since it can reduce readability and add code that only exists to improve performance. This may complicate programs, making them harder to maintain and debug. When speed is important, you can move time-critical functions to extension modules written in languages such as C/C++. You can also use [PyPy](https://www.pypy.org/) or [Cython](https://cython.org/).

> We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%. — Donald Knuth

### Statements & Control flow
* Python uses a _carriage return_ to separate statements.
* _Code blocks_ are defined by their indentation. Python use _whitespace indentation_, there are no explicit _braces_, _brackets_, or _keywords_. This means that _whitespace_ is significant, and must be consistent. Four spaces are the preferred indentation method, avoid using tabs.
* Python uses `if` along with `else` and `elif` (a contraction of else-if) statements for _conditional execution_ of a code block.
* Since Python 3.10, the `match` and `case` statements can be used for pattern matching. The `match` statement is similar to the _switch statement_ of other languages.
* Python uses the `for` statement to _iterate_ over an _iterable object_ and the `while` statement to execute a code block as long as its _condition_ is true.
* Python uses the `break` statement to exit the loop containing it, after which the program flow continues immediately after the loop body.
* The `continue` statement, skips this iteration and continues with the next statement.
* A [function](https://docs.python.org/3/glossary.html#term-function) or [method](https://docs.python.org/3/glossary.html#term-method) can be defined with the `def` statement.
* Python defines a [class](https://docs.python.org/3/glossary.html#term-class) with the `class` statement.
*  A _colon_ `:` is used to start a code block in the case of `if`, `elif`, `while`, `for`, `def`, and `class` statements. It is also used to fetch data and _index ranges_, _slicing_, and to identify _keys_ in _dictionaries_.
* All names in Python are _case-sensitive_: i.e. names of _variables_, _functions_, _classes_, _modules_, and _exceptions_.
* The `del` statement is used to delete variables and objects from existence.
* In Python, the `pass` statement serves as a _NULL statement_. The interpreter parses it to a _NOP instruction_, meaning no operation in machine language. It is syntactically needed to create an empty code block.
* Python uses `try`... `except` code blocks to handle _exceptions_, and `raise` statements to generate them.
* In Python, the `assert` statement can be used during debugging to check conditions that should always be true. If the condition is not satisfied, the program stop and gives an `AssertionError` along with an optional message.
* The `return` statement is used to return a value from functions.
* The `yield` statement returns a value from generator functions.
* Python uses these `=`, `+=`, `-=`, `*=`, `/=`, `%=`, `//=`, `**=` _assignment operators_. In Python assignment is a [statement](https://docs.python.org/3/glossary.html#term-statement) and not an [expression](https://docs.python.org/3/glossary.html#term-expression). Consequently, Python does not support _in-line assignment_.
* Python features _sequence unpacking_ which means to assign elements of an iterable object into multiple variables, it's written as `as many variables as elements in sequence = sequence`.
* The `import` statement is used to import [modules]((https://docs.python.org/3/glossary.html#term-module)). Importing a python module, runs all of its contents. Use an `if __name__ == "__main__":` code block to allow or prevent parts of code from being run when modules are imported.

### Built-in types
* Python's native types are _numbers_, _sequences_, _classes_, _instances_ and _exceptions_.

| Type | Mutability | Description |Example 
|:---------:|:---------:|:---------:|:---------:|
| `bool` | immutable | boolean value |`True` or `False` |
| `int` | immutable | integer value | `2` |
| `float` | immutable | double-precision floating-point value | `3.14159` |
| `complex` | immutable | complex number with real and imaginary parts |`1+2j` |
| `str` | immutable | sequence of Unicode characters |`'Python'` |
| `bytes` | immutable | sequence of bytes |`b'Python'` |
| `bytearray` | mutable | sequence of bytes |`bytearray(b'Python')` |
| `list` | mutable | ordered sequence of values |`[3, True, 'Python']` |
| `tuple` | immutable | ordered sequence of values |`('Python', 5, False)` |
| `dict` | mutable | associative array of key and value pairs |`{'name': 'Adam', 7: 'apple'}` |
| `set` | mutable | unordered sequence of values with no duplicates |`{True, 11, 'Python'}` |
The table list the most commonly used types, but there are more types.

### Expressions
* Python follows mathematical _rules of precedence_ for mathematical operations: parentheses, exponentiation, multiplication & division, addition & subtraction, and operators with the same precedence are evaluated from left to right.
* Python uses the `+`, `-`, `*`, `%`, `**` _arithmetic operators_. There are two types of division in Python called _floor division_ (or _integer division_) `//` and _floating-point division_ `/`.
* Python uses the `==`, `>`, `<`, `!=`, `>=`,`<=` _comparison operators_. Comparisons may even be chained (e.g. `a <= b <= c`).
* Since Python 3.8, the _walrus operator_ `:=` can be used to assign values to variables as part of an expression.
* Python uses the words `and`, `or`, `not` for _Boolean_ (or _logic_) _operators_ instead of the symbolic equivalent `&&`, `||` and `!`.
* Python uses the `is` and `is not` _identity operators_ to check if two values refer to the same object or not.
* Python uses the `in` and `not in` _membership operators_ to test whether or not a sequence contains a value. The `in` keyword is also used to iterate through a sequence in a `for`loop.
* Python makes a distinction between [lists](https://docs.python.org/3/glossary.html#term-list) ([mutable](https://docs.python.org/3/glossary.html#term-mutable)) written as `[2, 3, 5]` and _tuples_ ([immutable](https://docs.python.org/3/glossary.html#term-immutable)) written as `(7, 11, 13)`.
* In Python strings can be _concatenated_ by adding them, using the arithmetic operator for addition.
* _Anonymous functions_ are implemented using [lambda](https://docs.python.org/3/glossary.html#term-lambda) expressions that is written as `lambda [parameters]: expression`.
* In Python, a [List comprehension](https://docs.python.org/3/glossary.html#term-list-comprehension) is written as `[expression for item in list]`. List comprehensions are a more elegant and concise way to create lists. It can also be used when working with strings or tuples.
* A Python [generator expression](https://docs.python.org/3/glossary.html#term-generator-expression) is written as `(expression for item in list)`. These anonymous generator functions create a single element on request, and therefore more memory efficient than an equivalent list comprehension.
* [Conditional expressions](https://www.python.org/dev/peps/pep-0308/) are written as `X if condition else Y`. The `condition` is evaluated first. If `condition` is `True`, `X` is evaluated and its value is returned, and if `condition` is `False`, `Y` is evaluated and its value is returned.

### Uses
* Python can serve as a scripting language for automation.
* Python is often used for web development and web applications.
* Python competes against Fortran and C/C++ in scientific computing and data science.
* Python is commonly used in data analysis, artificial intelligence and machine learning.
* Python is often used in natural language processing.
* Python can be used in game development.
* Python is embedded in many software products as a scripting language.
* Many operating systems include Python as a standard component.
* Python is used extensively in information security and exploit development.