Python - Getting Started: Introduction
Note: Read the offical docs to get the most updated information.
Getting Started: Introduction
Python is a dynamic and strongly typed, object-oriented programming, that employs both duck typing and gradual typing (via type hints).
Supports multiple programming paradigms including both imperative (object-oriented, procedural) and declarative (functional, concurrent) flavors, but internally, everything in Python is an object.
Python was created by Guido van Rossum and first released in 1991. The Python Software Foundation manages and directs resources for Python and CPython development and receives proposals for changes to the language from members of the community via a PEP (Python Enhancement Proposal).
Basics
Indentation
Python puts a strong emphasis on code readability and uses significant indentation for function, method, and class definitions, described in PEP20
- https://blog.startifact.com/posts/older/what-is-pythonic.html.
There is no strict indentation amount (either space OR [tab] characters are acceptable), but indentation must be consistent for all indented statements.
Inconsistent indentation will raise an error: IndentationError: unindent does not match any outer indentation level
REPL
REPL is the language shell, the Python Interactive Shell. The REPL acronym is short for Read, Eval, Print and Loop.
~ $ python
>>>
Objects
Objects are assigned to names via the assignment operator =
Variables are written in lower_case
and constants usually in UPPER_CASE
. A name (variable or constant) is not itself typed, and can be attached or re-attached to different objects over its lifetime.
>>> var = 1
>>> var = "hello"
>>> print(var)
"hello"
Constants are typically defined on a module or global level, and although they can be changed, they are intended to be named only once.
Comments
Comments in Python start with a #
that is not part of a string, and end at line termination. Python does not support multi-line comment marks.
# this is a comment
Documentation
The first statement of a function body can optionally be a docstring, which concisely summarizes the function or object’s purpose. Docstrings are read by automated documentation tools and are returned by calling .__doc__()
on the function, method, or class name and their conventions are laid out in PEP257.
# An example on a user-defined function.
def my_function(parameter1, parameter2):
"""Example of a function
:param parameter1: int the base number.
:param parameter2: int the power to raise the base number to.
:return: int - number raised to power of second number
Takes number_one and raises it to the power of number_two, returning the result.
"""
return number_one ** number_two
>>> print(my_function.__doc__)
Returns float or int.
Takes number_one and raises it to the power of number_two, returning the result.
Leave a Comment