Better Programming

Advice for programmers.

Follow publication

Introduction to Python

2 min readAug 12, 2019
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

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Ionel Hindorean
Ionel Hindorean

Written by Ionel Hindorean

Web Engineer. Twitter: @ionelh Linkedin: Ionel Hindorean

No responses yet

Write a response