How To Code in Python


Python banner image


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.


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.)

  • 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.
  • 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.
  • 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.
  • 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.
  • In this tutorial, we will go over the important data types native to Python: integer, float, Boolean, string, list, tuple, and dictionary.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • This tutorial will go over operators that can be used with number data types in Python.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • This tutorial will guide you through writing Python modules for you or others to use within your program files.
  • This tutorial will take you through writing conditional statements in the Python programming language.
  • 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.
  • In computer programming, loops allow us to automate and repeat similar tasks multiple times. In this tutorial, we’ll be covering how for loops work in Python and how to construct them.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.