Tutorial Series

How To Code in Python

How To Code in Python

Introduction

Python is a flexible and versatile programming language that can be leveraged for many use cases, with strengths in scripting, automation, data analysis, machine learning, and back-end development. It is a great tool for both new learners and experienced developers alike.

Prerequisites

You should have Python 3 installed and a programming environment set up on your computer or server. If you don’t have a programming environment set up, you can refer to the installation and setup guides for a local programming environment or for a programming environment on your server appropriate for your operating system (Ubuntu, CentOS, Debian, etc.)

Summary View
detailed View

Series Tutorials

Tutorial

This tutorial will walk you through writing a “Hello, World” program in Python 3. The “Hello, World!” program is a classic tradition in computer programming. Serving as a simple and complete first program for beginners, as well as a good program to test systems and programming environments, “Hello, World!” illustrates the basic syntax of programming languages.

Tutorial

This tutorial will go over how to work with the Python interactive console and leverage it as a programming tool. Providing access to all of Python’s built-in functions and any installed modules, command history, and auto-completion, the interactive console offers the opportunity to explore Python and the ability to paste code into programming files when you are ready.

Tutorial

How To Write Comments in Python 3

Updated on July 8, 2021

Comments are lines in computer programs that are ignored by compilers and interpreters. This tutorial will go over how to use comments in your Python program, making your projects more readable for humans and thus more open to collaboration.

Tutorial

How To Write Doctests in Python

Published on February 6, 2021

Python’s standard library comes equipped with a test framework module called doctest. The doctest module programmatically searches Python code for pieces of text within comments that look like interactive Python sessions. Then, the module executes those sessions to confirm that the code referenced by a doctest runs as expected.

Tutorial

Understanding Data Types in Python 3

Updated on August 20, 2021

In this tutorial, we will go over the important data types native to Python: integer, float, Boolean, string, list, tuple, and dictionary.

Tutorial

This Python tutorial will go over the basics of working with strings, including how to create and print strings, concatenate and replicate strings, and store strings in variables.

Tutorial

How To Format Text in Python 3

Updated on August 20, 2021

In this tutorial, we’ll go over some of the ways we can work with Python strings to make sure that all output text is formatted correctly. Topics we will cover include: quotes, apostrophes, multiple lines, escape characters, and raw strings.

Tutorial

Python has several built-in functions associated with the string data type. These functions let us easily modify and manipulate strings. In this tutorial, we’ll go over several different functions that we can use to work with strings in Python 3.

Tutorial

How To Index and Slice Strings in Python 3

Updated on December 15, 2021

The Python string data type is a sequence made up of one or more individual characters consisting of letters, numbers, whitespace characters, or symbols. Strings are sequences and can be accessed in the same ways as other sequence-based data types, through indexing and slicing. This tutorial will guide you through how to access strings through indexing and how to slice them through their character sequences; it will also cover counting and character location methods.

Tutorial

How To Convert Data Types in Python 3

Updated on August 20, 2021

This Python 3 tutorial will guide you through converting data types including numbers, strings, tuples and lists, as well as provide examples to help familiarize yourself with different use cases.

Tutorial

How To Use Variables in Python 3

Updated on August 20, 2021

This tutorial will cover some variable basics and how to best use them within the Python 3 programs you create. We’ll go through naming rules and conventions, reassigning variables, multiple assignment, and making local and global variables.

Tutorial

This tutorial will guide you through some of the common uses of string formatters in Python, which can help make your code and program more readable and user friendly.

Tutorial

This tutorial will go over operators that can be used with number data types in Python.

Tutorial

This tutorial will go through a few of the built-in functions that can be used with numeric data types in Python 3. Becoming familiar with these methods can give you more flexibility when programming. We’ll go over the following functions: abs() for absolute value, divmod() to find a quotient and remainder simultaneously, pow() to raise a number to a certain power, round() to round a number to a certain decimal point, sum() to calculate the sum of the items in an iterable data type.

Tutorial

Understanding Boolean Logic in Python 3

Updated on February 1, 2021

The Boolean data type can be one of two values, either True or False. We use Booleans in programming to make comparisons and to control the flow of the program. In this tutorial, we’ll go over the basics you’ll need to understand how Booleans work, including Boolean comparison and logical operators, and truth tables.

Tutorial

Understanding Lists in Python 3

Updated on August 20, 2021

This tutorial will go through some of the ways we can work with lists in Python. Lists are great to use when you want to work with many related values. They enable you to keep data together, condense your code, and perform the same methods and operations on multiple values at once. This tutorial will cover some basic processes, including indexing, slicing, modifying, and concatenating lists.

Tutorial

How To Use List Methods in Python 3

Updated on August 20, 2021

In this tutorial, we’ll go through the built-in methods that you can use to work with the list data structure in Python. We’ll go through adding items to and removing items from lists, extending lists, reversing and sorting lists, and more.

Tutorial

List comprehensions offer a succinct way to create lists based on existing lists. In this tutorial, we will cover the syntax of list comprehension, which will be an important tool in creating efficient code.

Tutorial

Understanding Tuples in Python 3

Updated on August 20, 2021

A tuple is a data structure that consists of an immutable ordered sequence of elements. Because tuples are immutable, their values cannot be modified. In this tutorial, we will cover some basic processes, including indexing, slicing and concatenating tuples, and the built-in functions that are available when working with these data structures.

Tutorial

Understanding Dictionaries in Python 3

Updated on August 20, 2021

The dictionary is Python’s built-in mapping type. Dictionaries map keys to values, making key-value pairs that can then store data. In this tutorial, we will go over the dictionary data structure in Python.

Tutorial

How To Import Modules in Python 3

Updated on August 20, 2021

This tutorial will walk you through installing modules, importing modules, and aliasing modules. Modules are Python .py files that consist of Python code. They can create function definitions and statements that you can reference in other Python .py files or via the Python command line interpreter. In Python, modules are accessed by using the import statement, which tells the current program to bring in the definitions and statements of the other relevant file(s) for its own use.

Tutorial

How To Write Modules in Python 3

Updated on August 20, 2021

This tutorial will guide you through writing Python modules for you or others to use within your program files.

Tutorial

This tutorial will take you through writing conditional statements in the Python programming language.

Tutorial

A while loop implements the repeated execution of code based on a given Boolean condition. The code that is in a while block will execute as long as the while statement evaluates to True. In this tutorial, we will go over how while loops work and how to construct them.

Tutorial

Python for loop

Updated on March 14, 2024

Let’s explore the Python for loop in detail and learn to iterate over different sequences including lists, tuples, and more.

Tutorial

In this tutorial, we will go over the break, continue, and pass statements in Python, which will allow you to use for and while loops more effectively in your code.

Tutorial

How To Define Functions in Python 3

Updated on August 20, 2021

A function is a block of instructions that, once defined, both performs an action once the function is called and makes that action available for later use. Functions make code more modular, allowing you to use the same code over and over again. In this tutorial, we’ll go over how to define your own functions to use in your coding projects.

Tutorial

In this tutorial, we will cover the syntax of working with *args and **kwargs as parameters within functions to pass a variable number of arguments to a given function. Both can be used improve readability and convenience, and are best for situations where the number of inputs within the argument list will remain relatively small.

Tutorial

In this tutorial, we’ll go through creating classes, instantiating objects, initializing attributes with the constructor method, and working with more than one object of the same class.

Tutorial

Object-oriented programming allows for variables to be used at the class or instance level. This tutorial will demonstrate the use of both class and instance variables in object-oriented programming in Python.

Tutorial

This tutorial will go through some of the major aspects of inheritance in Python, including how parent classes and child classes work, how to override methods and attributes, how to use the super() function, and how to make use of multiple inheritance.

Tutorial

Polymorphism allows for flexibility and loose coupling so that code can be extended and easily maintained over time.

This tutorial will go through applying polymorphism to classes in Python.

Tutorial

How To Use the Python Debugger

Updated on August 20, 2021

In software development, debugging is the process of looking for and resolving issues that prevent computer software from running correctly. The Python debugger pdb provides a debugging environment for Python programs. In this tutorial, we will go over how to work with pdb to implement an interactive debugging environment that you can use with any of your programs written in Python.

Tutorial

The Python code module is a useful and quick tool for debugging because it can be used to emulate the interactive interpreter. This tutorial will cover how to work with this module to examine your code.

Tutorial

How To Use Logging in Python 3

Updated on August 20, 2021

The logging module is part of the standard Python library and provides tracking for events that occur while software runs. You can add logging calls to your code to indicate what events have happened. In this tutorial, we will go over how to work with logging calls to understand the various events that occur from running your program over time.

Book

DigitalOcean eBook: How To Code in Python

Published on February 2, 2018

The free How To Code in Python eBook can be used as an Open Educational Resource and an alternative to a textbook in the classroom, as well as be made available for the wider public through libraries.

Check out all our Tutorial Series

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.