Introduction to Python
A walkthrough of the basics with code examples
Python is an interpreted, high-level language created by Guido van Rossum and released in 1991. It is dynamically typed and garbage collected.
Python programs have the extension
.py and can be run from the command line by typing
Probably its most noticeable characteristic is its use of significant white space to delimit code blocks, instead of the more popular
End-of-line semicolons (
;) are optional and usually not used in Python.
The latest version is 3.7.4 (when this article was written), which is what I will use in the examples.
Print to the Console
Variables do not need to be declared.
Their data types are inferred from their assignment statement, which makes Python a dynamically typed language. This means that different data types can be assigned to the same variable throughout the code.
Comparison, Logical, and Conditional Operators
Python supports three numeric data types: int, float, and complex. They are inferred, so need not be specified.
A list in Python is what other programming languages call an array. They use Zero-based indexing, and the items can contain any type of data. List items are nested in
Tuples are just like lists, but immutable (cannot be modified). They are surrounded by
Dictionaries are key-value pairs. They are surrounded by
Functions are defined using the
Default values for arguments may also be defined.
Classes are collections of variables and functions that work with those variables.
Classes in Python are defined with the
class keyword. They are similar to classes in other languages like Java and C#, but differences include
self being used instead of
this to refer to the object resulted from the class. Also, the constructor function’s name is
__init__, instead of the more popular
self must be used every time a class variable is referenced and must be the first argument in each function’s argument list, including the constructor.
Python does not support method overloading, but it can be achieved to some extent by using default values for arguments (shown in the Functions section).
Python also supports inheritance, which means that classes can be set to inherit methods and variables from another class or multiple classes (multiple inheritance).