Basics

Here is some basic information about the Python programming language.

In this chapter code blocks used often. >>> symbol is used to denote the commands entered into the interpreter. ... denotes an indented code block following a for or while loop (will mention later) or a conditional statement. The rest of the lines represent outputs of the Python interpreter.

Installing Python

Most of the computers already come with Python installed. In order to check whether you already have up to date version of Python, go to the command line (terminal in Mac, cmd in Windows) and type Python, the prompt will show the version of Python you have.

>>> username$ python
Python 3.7.7 (default, Mar 26 2020, 10:32:53) 

If you don't have up to date version of Python, you can go install the one compatible with your device here. Moreover, if you are going to learn Python and use it for Data Science, probably the best option is to download Anaconda (or light version Miniconda) distribution as it comes with most of the up to date Machine Learning libraries included along with the most common Python and R interpreters (JupyterLab, Jupyter Notebook, PyCharm, RStudio, Spider, VSCode, etc.).

My personal favorites are Jupyter Notebook (or JupyterLab) and VSCode for Python, RStudio for R. Now, assuming you already have an interpreter let's dive into the basic commands of Python.

Print

The first command everybody should learn is print(). It simply prints the provided string/variable to the console.

String: Anything written between 2 apostrophes (' ')or quotation marks (" ") is called a string, and if one of the words requires an apostrophe we can use quotation marks to represent the string (see the last example below).

Variables

To create a variable use = sign followed by a variable name and then assign it to any value you want. You can print the value of the variable using print() function.

Data Types

Use type() command to check the data type of a variable. The list of data types below is mainly taken from the w3schools.com. Putting a pound # symbol before typing anything converts the code line into a comment line in Python.

  • Text type: String (str)

  • Numeric Types: Integer (int), Float (float), Complex (complex)

Note: int(3.5) gives the integer part of the float, i.e., 33, whereas float(3) yields 3.03.0. Complex i is denoted by j in Python since i often used to denote an index for iteration in a loop.

  • Sequence Types: List (list), Tuple (tuple), Range (range)

Lists and tuples can store different types of data types. However, the main difference between a list and a tuple is that the lists are mutable (i.e., the elements can be reassigned) while the latter is immutable (i.e., the elements cannot be reassigned or changed). The range function is mainly used to create integer sequences in the following format range(start=0, stop, step). Some examples are given below:

  • Mapping Type (Hash Table): Dictionary (dict)

Dictionaries consist of key:value pairs and are mutable objects in Python.

  • Set Types: Set (set, or frozenset)

Sets are collections of unordered distinct objects. The main difference between a set and a frozenset is that set is mutable while frozentset is immutable. Also, sets cannot contain mutable objects, as a result, only a frozenset can be an element of either set.

  • Boolean Type: Boolean (bool)

  • None Type: None

Last updated

Was this helpful?