A walkthrough of the basics with code examples

Ionel Hindorean
Aug 12 · 2 min read
Source

Introduction

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 python file_name.py.

Probably its most noticeable characteristic is its use of significant white space to delimit code blocks, instead of the more popular {} symbols.

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.


Installation

Download the latest version here.

PyCharm is an IDE that makes it easy to write and run Python code. The community edition is free and you can get it from here.


Print to the Console


Comments


Arithmetic Operators


Variables

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

Comparison: ==, !=, <, >, <=, >=

Logical: and, or, not

Conditionals: if, else, elif


Data Types

Strings

Numbers

Python supports three numeric data types: int, float, and complex. They are inferred, so need not be specified.

Booleans

Lists

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

Tuples are just like lists, but immutable (cannot be modified). They are surrounded by ().

Dictionaries

Dictionaries are key-value pairs. They are surrounded by {} and are similar to objects in JavaScript. The values can have any data type.


Loops

For loops

While loops

Functions

Functions are defined using the def keyword.

Default values for arguments may also be defined.


Classes

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 classname. 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).


File I/O


Better Programming

Advice for programmers.

Ionel Hindorean

Written by

Web Engineer. Twitter: @ionelh Linkedin: Ionel Hindorean

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade