Introduction to Python

Objectives ⟶

  • Introduce you to Python

  • Discuss why Python is popular

  • Discuss why Python is great for Data Analytics

  • Learn how to add comments

What is Python?

Python Logo

Python is a broad-purpose programming language that is designed to be human-readable. It was created by Guido van Rossum in the 1980s. Its simple syntax emphasizes readability. Unlike many other programming languages, Python code often reads like natural English sentences. According to PYPL Programming Language Popularity Index, Python is the world's most popular programming language as of May 2022.

Rank Change Language Share Trend
1Python27.85% -2.5%
2Java 17.86% -0.1%
3JavaScript 9.17% +0.4%
4C# 7.62% +0.7%
5C/C++ 7.0% +0.4%
6PHP 5.36% -1.0%
7R 4.34% +0.5%
8Typescript 2.39% +0.7%
9Objective C 2.25% +0.0%
10Swift 2.05% +0.3%

What makes Python so charming?

  1. 🍰 It's easy to read and write.
  2. 🔨 It's open-source and free.
  3. 🚀 There are thousdands of packages available.
  4. 🦄 It's versatile. You can use Python for data science (machine learning, deep learning, AI), web development, desktop app development, low-level programs, game programming, and more.
  5. ⚔️ It has a massive community of users and supporters.
Multiple Choice Question

Loading

Question
Loading
Loading Options
Loading
Multiple Choice Question

Loading

Question
Loading
Loading Options
Loading

Python and Data Analytics

Why do we use Python?

Python is a great programming language for data analytics.

First, Python has a wide offering of data analysis and visualization libraries. Pandas is the de-facto standard library for data analysis which we will learn in later chapters. Libraries like TensorFlow and PyTorch are used for machine learning.

Second, Python offers a higher development speed, especially when it comes to working with data. Data analysts can quickly perform exploratory data analysis to obtain a high-level understanding of a dataset. They can also prototype a statistical model with well-maintained libraries.

Finally, Python is a great beginner's language. Recall that the Python syntax is human-readable. You can often guess what a block of code is doing even if you don't fully understand Python.

Take this block of Python code as an example. Can you guess what it does?

# Python code
lemonade_sales = [3, 9, 6, 3]
sales_target = 20

if sum(lemonade_sales) > sales_target:
  print("Exceeds sales target!")
else:
  print("Not quite there yet")

Compare that with another block of Java code. Would you believe me if this block of code does the exact same thing as the previous one?

// Java code
public class SalesTargetChecker {
    public static void main(String args[]) {
        int[] lemonade_sales = new int[] { 3, 9, 6, 3 };
        int sales_target = 20;
        int total_sales = 0;

        for (int sale: lemonade_sales) {
            total_sales += sale;
        }

        if (total_sales > sales_target) {
            System.out.println("Exceeds sales target!");
        } else {
            System.out.println("Not quite there yet");
        }
    }
}
Python Challenge

Loading

Running Python

Python Version

There are three major Python versions - 1, 2, and 3. Most tutorials you find nowadays are based on version 3. The previous versions are deprecated and no longer supported. We recommend you to use version 3 unless you have a very specific reason to use one of the older versions (e.g., using a package that only supports version 2). All materials on this site use version 3.

You can programmatically check the Python version using the code below.

import sys
print(sys.version)
# Output
3.9.5 (default, Feb 22 2022, 14:12:02)
[Clang 13.0.0]
Python Intrepreter

To run Python locally, you will need to install a Python interpreter. If your goal is to simply learn Python through this website, you do not install a Python interpreter locally.

Python Comments

When writing code, you may want to add notes for yourself or other humans. You can use comments to write text that are ignored by the Python interpreter. There are two types of comments:

  1. single-line
  2. multi-line
Single-line Comment

#

Lines that start with # are ignored by the Python interpreter.

lemonade_price = 3

# Sales tax in Champaign is 9%
sales_tax = 0.09

You can also use the hash character (#) after a line of code to add an inline comment.

lemonade_price = 3
sales_tax = 0.09    # Sales tax in Champaign is 9%
Multi-line Comment

"""

Any lines of code between a set of three single quotes (''') or three double quotes (""") are considered multi-line comments. The Python interpreter ignores those lines of code.

"""
We sell lemonade at $3 per cup.
Sales tax in Champaign is 9%. This consists of:
  state: 6.25%
  county: 1.25%
  city: 1.5%
"""
lemonade_price = 3
sales_tax = 0.09

Recall that you can use a set of three single quotes (''') as well.

'''
This is an example
of a multi-line comment
'''
Multiple Choice Question

Loading

Question
Loading
Loading Options
Loading