Basics of Python Programming

Introduction to Python

Guido van Rossum designed Python at the National Research Institute for Mathematics and Computer Science in the Netherlands during the late 1980s and early 1990s. Derived from the Unix shell command-line interpreter and other programming languages including C and C++, it was designed to enable developers to write programs with fewer lines of code than other languages. Unlike other programming languages, Python also incorporates many English keywords where other languages use punctuation symbols.

In Python, the input code is read by the interpreter to perform an output. Any errors, including poor formatting, misspelled functions, or random characters left in your script will be picked up by the Python interpreter and cause a syntax error.

In this lesson, you will learn the basic syntax to help you write fluid and effective code.

Comments

Adding comments is good practice in computer programming to signpost the purpose and content of your code.

In Python, comments can be added to your code using the # (hash) symbol. Everything placed after the hash symbol (on that line of code) is then ignored by the Python interpreter.

#Import Melbourne Housing dataset from my Downloads folder
dataframe = pd.read_csv('~/Downloads/Melbourne_housing_FULL.csv')

In this example, the second line of code will be executed, while the Python interpreter will ignore the first line of code.

Indentation & Spaces

Unlike other programming languages, Python uses indentation to group code statements, such as functions and loops, rather than keywords or punctuation to separate code blocks.

new_user = [
    66.00, #Daily Time Spent on Site
    48, #Age
    24593.33, #Area Income
    131.76, #Daily Internet Usage
    1, #Male
    0, #Country_Afghanistan
    1, #Country_ Albania
    0, #Country_Algeria
   ]

In expressions, spaces are ignored by the Python interpreter. In other words, 8+4 or 8 + 4 are the same. It’s helpful to add spaces between characters for readability.

Python data types

Common data types in Python are shown in the following table.

Get hands-on with 1400+ tech skills courses.