Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Getting Started with Python

LS100 — Module 00B · Python Fundamentals

Harvard University

Welcome to Python: A Beginner’s Guide

Souvik Mandal, Ph.D., Linkedin ID: souvik-mandal-phd

Project Leader & Instructor, Computational Behavioral Sciences, LS100, FAS, Harvard University

Introduction: What is Python?

Python is an open-source, high-level programming language known for its simplicity and versatility. It is a general-purpose language, meaning you can use it for almost any kind of programming – from developing software and web applications to data operations, scientific computing, to flight control and industrial applications [1]. First released in 1991, Python has become one of the most popular and recommended languages for beginners and experts alike. Its syntax is designed to be highly readable (often resembling plain English), which makes it easy to learn and beginner-friendly [2]. Python’s popularity is backed by a large, active community and a rich ecosystem of libraries for various domains like data science, machine learning, computer vision, automation, software & web development, etc.) [3]. Most, if not all, top tech companies and research labs use Python in their technology stacks [4], and Python consistently ranks among the most-loved and in-demand programming technologies [5]. In short, pick up your favorite thing that can be benefitted from computing / coding / programming, learn Python from that perspective, and the Python skills you will develop can open the doors to many other areas of computing.

Using Python: From Simple to Advanced Workflows

When starting out, there are multiple ways to set up and work with Python, each with its own pros and cons. Below we examine several approaches, from the simplest (no installation) to more complex development workflows:

Online (Cloud) Python Notebooks

One of the easiest ways to start coding in Python is through Notebooks on online Python environments like Google Colab, and Kaggle. We can write and run Python code in these notebooks through web browsers, just like any other website, with no installation on our computer.

When to use: Online notebooks are great for initial learning, short projects, or when you can’t install software on your device. Educators often use them so learners can focus on coding rather than environment setup. If you have internet access and want to start right away, this is the simplest route.

2. Standard Installation on Your Computer

Another approach is to install Python on your computer (computer is also called “machine” in this context) and run code locally (which means Python running on your computer). The official Python distribution can be downloaded from Python’s website (https://www.python.org/downloads) for Windows, macOS, or Linux. This approach gives you a persistent Python setup on your machine.

When to use: Installing Python locally is recommended if you plan to do substantial work on your own machine, want the ability to save files locally, or if you have offline requirements. It’s the next step once you outgrow the simplicity of online notebooks and need more control or want to build larger projects.

3. Installing Locally using a Python Distribution (Anaconda for Data Science)

For learners interested in data science or scientific computing, an all-in-one distribution like Anaconda is a popular choice. Anaconda bundles Python with a collection of commonly used libraries (NumPy, Pandas, matplotlib, etc.) and includes tools like Jupyter Notebook and JupyterLab by default.

When to use: If your primary goal is data science or you want a hassle-free way to get a scientific Python stack, Anaconda is a good choice. It’s often used in academic settings for its convenience. However, for general programming or if you prefer a lean setup, the standard Python installation plus manual package installs might be preferable.

4. Integrated Development Environments (IDEs) and Code Editors

As you progress, you may want to use a more powerful code editor or IDE (Integrated Development Environment) for writing Python code. Tools like Visual Studio Code (VS Code), PyCharm, or Spyder offer rich features like syntax highlighting, auto-completion, debugging, and project management.

When to use: If you start working on multi-file projects, need to debug code, or just prefer a more feature-rich environment, an IDE is appropriate. Many beginners start with IDLE or simple text editors and then move to VS Code or PyCharm as they become more comfortable. Visual Studio Code in particular is a friendly stepping stone – you can use it just as a text editor initially, and gradually make use of its IDE features as needed.

5. Advanced Workflows: Virtual Environments and Cloud Deployment

For completeness, it’s worth mentioning what more advanced Python workflows look like (even if it’s beyond the initial learning phase):

In summary, beginners should start with the approach that gets them coding as easily as possible – an online notebook or a basic local install – and gradually adopt more sophisticated tools as needed. Python’s strength is that it scales with you: you can start with a single notebook cell and, down the road, end up building a complex cloud-deployed application using the same core language.

Installing Python

On macOS (Apple M Silicon)

If you are using a Mac with an Apple Silicon chip (M1, M2, etc.), installing Python is straightforward. Newer versions of macOS (since macOS Catalina) do not come with a usable Python 3 installation by default [19], so you will need to install Python 3 yourself. There are a couple of methods:

Method 1: Install via Homebrew (Recommended for Developers). If you’re comfortable with the Terminal, you can use Homebrew, a package manager for macOS, to get Python. First, install Homebrew (if not already installed) by running the official install script in Terminal [20]. Then install Python by typing:

brew install python3

This will fetch the latest Python 3 version. Homebrew makes it easy to update Python later (brew upgrade python) and manages the installation for you [21][22].

Method 2: Download the Official Python Installer. The Python Software Foundation provides macOS installers on the official Python website. Visit the downloads page and click the Python 3 download button for macOS [23]. Make sure to choose the universal installer package, which supports both Intel and Apple Silicon Macs (modern Python installers include native support for Apple’s M1/M2 chips) [24]. Download the .pkg file and run it, then follow the prompts through the installation wizard (the default settings are usually fine) [25]. You may be asked for your Mac login password to approve the install.

Apple Silicon Note: The universal installer will install an optimized version of Python for your M1/M2 Mac. In the past, there were workarounds using Rosetta or specific builds, but nowadays the official Python 3.11+ installer works natively on Apple Silicon Macs [24], so you get full performance out of Python.

After installation completes, you can verify it was successful. One way is to open the Terminal (find it in Applications > Utilities) and type python3 --version (or python3 to enter the interactive shell) [26]. Another way is to use IDLE, the Integrated Development Environment that comes with Python. Find IDLE in your Applications (it might be in a folder called “Python 3.x”). Launch IDLE, and it will open a window with the Python prompt >>>. You can try typing a simple command like print("Hello, Python!") and see the output. If everything is installed correctly, IDLE will show the Python shell waiting for your commands, as illustrated below:

After installing Python on macOS, you can launch IDLE. This is the Python 3 shell on a Mac, indicating a successful installation. [12]

(If the version number in IDLE or the Terminal is displayed (e.g. “Python 3.11.4 ...”), you have Python set up on your Mac!)

Method 3: Install via Anaconda Distribution.
Anaconda is a popular all-in-one Python distribution designed for data science and machine learning. It comes with Python plus hundreds of commonly used libraries and tools already installed, including Jupyter Notebook and JupyterLab.

Pros:

Cons:

Installing Python on Windows

Installing Python on Windows is also easy, with two main options available. You can either use the Microsoft Store app or install Python from the official website. We’ll cover both:

Option 1: Install from the Microsoft Store (Windows 10/11). Microsoft provides Python via the Windows Store, which is a quick way to get Python 3 set up without dealing with PATH settings or admin permissions. Simply open the Microsoft Store, search for Python 3. (look for the one published by the Python Software Foundation), and click Get/Install[27]. This will install Python for your user account. The Store installation automatically configures your PATH, so you can immediately use the python command in PowerShell or Command Prompt[28]. It also will automatically update Python when new versions are released. This method is recommended for beginners by Microsoft’s documentation because it’s simple and doesn’t require extra configuration[29]. After installation, you can verify by opening a new PowerShell (or Command Prompt) and typing python --version to see the installed version[30]. You should also find IDLE in your Start menu (e.g. “IDLE (Python 3.x)”) which you can launch to bring up the Python shell.

Option 2: Install using the Official Installer from Python.org. Go to python.org/downloads and download the latest Windows installer (choose the 64-bit installer for modern PCs)[31]. Run the installer .exe file. Important: On the installer’s first screen, check the box that says “Add Python to PATH” (and optionally “Install launcher for all users”) before clicking “Install Now”[13]. This ensures that the Python executable is added to your system PATH, which means you can use the python command from any terminal window. The installer will then copy Python onto your system. After a few minutes, you should see a success message[32].

During the Python installation on Windows, make sure to check the option to Add Python to PATH (as shown above). This will let you run python from the command line without extra steps.[13]

Once installed, verify it by opening the Command Prompt (cmd.exe) and running python --version, which should display the Python 3 version. You can also launch the IDLE app (search for “IDLE” in Start) – if it opens and shows a Python shell window, the installation was successful[33]. From the command line, the python (or python3) command will start the interactive interpreter, where you’ll see the >>> prompt. Type exit() or press Ctrl-Z then Enter to quit the interactive mode.

Option 3: (Alternative) Anaconda Distribution. If you prefer using Anaconda on Windows, download the Anaconda installer from anaconda.com (it’s a large .exe file). Running it will install Python and a suite of tools. Anaconda has its own Start menu entry (Anaconda Prompt, Jupyter Notebook, etc.). This is an alternative to the above, mainly for those focusing on scientific computing.

After installing via any method, you are ready to write and run Python code on Windows. If you plan to use a code editor like VS Code, be sure to install it and the Python extension as mentioned earlier. Visual Studio Code can detect your Python installation automatically, especially if Python is in the PATH.

Working with Python: Running Scripts and Notebooks

With Python installed (or an online environment ready), you can now start running Python code. There are a few common ways to use Python interactively or run programs:

If you installed Python via Homebrew or the official .pkg installer, Jupyter Notebook is not included by default. You need to install it separately:

  1. Open Terminal.

  2. Upgrade pip (optional but recommended):

python3 -m pip install --upgrade pip

  1. Install the Jupyter Notebook package:

python3 -m pip install notebook

  1. To launch Jupyter, navigate to your project folder in Terminal and type:

jupyter notebook

This will open a browser window at http://localhost:8888, where you can create and run notebooks.

When to use notebooks vs scripts: For learning and experimentation, notebooks are fantastic because you can run pieces of code in isolation and see results (graphs, data tables, etc.) immediately. Our guide will next have you use a Jupyter Notebook to practice Python concepts. On the other hand, if you’re writing a program that will be used as an application or needs to be packaged, you’d typically organize it into script files or modules. It’s common to use notebooks for exploration and then move code into .py files for deployment or larger projects.

Now that your environment is set up and you know how to run Python, let’s start our first exploration with Python.

Hands-On Practice: Using a Jupyter Notebook to Explore Python

At this point, you have Python set up and know the basic concepts. A great way to solidify your understanding is to play with code in a Jupyter Notebook. You can either use an online notebook (e.g. Colab) or run one locally. Here’s a suggested plan for a notebook session:

  1. Basic operations: In a new notebook, try using Python like a calculator. For example, compute 5 + 3*2, see how operator precedence works. Try assigning variables like x = 10, then do x * 2 and x = "Hello" to see dynamic typing.

  2. Using print and input: Write a cell that asks for input and then prints a formatted message. (Note: In Colab, input() works, but in some notebook environments (like JupyterLab) it will prompt in the console. In any case, you can practice by just setting a variable instead of truly interactive input if needed.) For example:

  1. Control flow: Practice with if statements and loops. For instance, write a cell with an if-elif-else chain that prints different messages based on a numeric variable. Or use a for-loop to iterate over a list of names and greet each.

  2. Define functions: Try writing a simple function, like def square(n): return n*n. Then call it with a few arguments (e.g. square(5)). Maybe write a function that given a temperature in Celsius returns it in Fahrenheit, to practice using a formula and return.

  3. Use data structures: Create a list of some items (e.g. favorite foods), then write a loop to print each item in uppercase. Create a dictionary mapping some keys to values (e.g. fruits to colors) and retrieve a value by its key. Modify the dictionary by adding a new entry. Experiment with a set by making a list with duplicate values and converting it to a set to see duplicates removed.

  4. Explore a library: Use the math module or another simple library to see how importing works. For example:

By playing with these in a notebook, you’ll reinforce the terminology above. Remember, the notebook lets you run pieces of code one at a time, so you can modify a variable in one cell and use it in the next, see errors and adjust, etc., in an interactive way.

Next Steps: Loading Data and Doing Useful Work

One of the powerful things about Python is the ability to work with various types of data, including numerical tables and other data files, images, audios, and videos. To start your hands-on journey, open the “LS100_00_Python-Fundamentals” folder of the course repo, and start exploring the notebooks in numeric order.

Conclusion: From Basics to Building Full Pipelines

Congratulations on making it through this introduction to Python! You’ve learned what Python is, set up an environment, and covered fundamental programming concepts from variables and functions to loops and classes. At this stage, you should be able to write simple programs and use notebooks to experiment with code. You even saw how to bring in external libraries to work with real-world data like audio and video.

Learning programming is a bit like learning a language – at first, you learn words and grammar (we covered the “words” like variables, and “grammar” like syntax/loops/functions). The next step is to practice by actually “speaking” – writing programs to solve problems or automate tasks. Start with small projects or exercises: for example, write a program to convert units (temperature, currency), or a simple number guessing game, or analyze a small dataset.

As you become comfortable with the basics, you can explore more advanced topics or domains:

Keep in mind that Python was created to be an easy-to-understand language, and it has a very supportive community. There are countless resources and forums (like Stack Overflow, Reddit’s r/learnpython, etc.) where you can seek help and advice.

With the guide and the hands-on practice in the notebook, you’re well on your way. Happy coding, and welcome to the world of Python programming! As you continue, you’ll discover more cool reasons to learn and use Python, including the ability to quickly turn ideas into working code[18]. All the best for your journey to using Python for your research. Python’s versatility and power will be your ally every step of the way. Enjoy!