Glossary
Definitions of common terms used in Python and this course.
Introduction
- Python REPL (a.k.a. “Python shell”)
The Read-Evaluate-Print Loop in Python. This is where you can enter Python statements and it gives you the result immediately. It operates by reading your Python command, evaluates it, prints the result (if any), and loops back for the next statement.
- string
The computer representation of characters (letters, numbers, etc). For example, “a”, or ‘cat’. You can use either a single quote
'or double quote"The string""is a special case that is an empty string.- integer
A whole number with no fractional values. For example, 1, 42, 0, -12.
- float (a.k.a. “floating point number”)
A number that possibly has fractional value. For example, 3.14159, -12.6, 3.0.
- variable
An object that holds a value or values. Variables have a type, like string, integer, float, and other types.
- object (a.k.a. “value”)
In Python, variables are objects. Methods and functions are also objects.
- type
Every object in Python has a type. There are strings, integers, lists, and a number of other types.
- statement
A command in Python to the computer. For example, in the REPL, the commands you enter are usually called statements.
- function
A collection of reusable statements that we can use so we don’t have to keep typing the same code over and over.
- method
A special function that belongs to a specific object or object type is called a method.
- expression
A statement or part of a statement that the computer can evaluate to get a result. For example,
2 + 2is an expression that evaluates to4.- boolean
A data type that can be either True or False.
- boolean expression
An expression that can only be True or False. For example,
2 > 3is a boolean expression that evaluates to False.- comparison
To compare things, as in comparing two numbers or strings to see if they are equal. This is done with the comparison operators
==,!=,<,<=,>,>=,<.
Modules & Functions
- module
A file containing a Python program or Python statements is a module.
- import
When you want to use code from a different module, you can import the module.
- namespace
A namespace is a mapping from names to objects. You can think of it as a “space” where names live. When you see
name1.name2, this accessesname2within the namespace ofname1.- scope
A place where actual variables live. When Python looks up a variable name, Python first checks the local scope, then the enclosing scopes, then the global scope, and finally the built-ins scope (this name search is sometimes called LEGB). The terms namespace and scope are closely related (scopes rely on namespaces to work)
- standard library
A library consists of specialty modules that you might want to use to do special things. The Python standard library is a library that you can access directly from Python.
- script
A Python script is a python program that can be run from the command line. For example, a script named
myscript.pycan be run from the command line by enteringpython myscript.py.- call
We call a function or method when we want it to execute.
- arguments
When we call a function, sometimes we give arguments as input to the function. These are put inside the parentheses.
- keyword arguments (a.k.a. “named arguments”)
Some functions allow you to use a variable number of arguments, where you give specific names for the ones you are using. The names are defined by the functions and are called keywords.
- comment
A comment in Python begins with a
#sign. Everything from the#sign to the end of the line is ignored by Python. Comments are useful to explain what a program is doing.
Lists & Looping
- list
In Python, a list is an ordered collection of things.
- index
A list index is the location of an item in a list. In Python, the first item in a list is at index 0, not 1. This is a computer science thing.
- iterable
An iterable is a variable type that can looped over in a for loop.
- loop
When you want to repeat sections of code, you can use a loop.
Data Structures
- lexicographical
An ordering we use for computing. It’s just like alphabetical, but it includes all the characters that are not letters, and upper case and lower case letters are distinct.
- truthy
An object that isn’t a boolean expression is truthy if Python treats it as True when it is used inside a boolean expression. For example, a string-type variable containing ‘cat’ is truthy, so Python will treat it as True.
- falsey
An object that isn’t a boolean expression is falsey if Python treats it as False when it is used inside a boolean expression. For example, a string-type variable containing the empty string
""is falsey, so Python will treat it as False.- truthiness
The truthiness of an object is whether it is truthy or falsey.
- dictionary
A dictionary in Python is a special variable type that contains key-value pairs of things. Dictionaries are ordered based on each pair’s creation order. Dictionaries are also referred to as mappings, because they map the keys to the values.
- key-value pairs
In a key-value pair of a dictionary, the key is used to look up the value. The key acts in a similar manner to an index of a list, except the key may be something like a string.
- sequence
A sequence object is one that supports element access via in index. These are strings, lists, tuples.
- tuple
A tuple is an immutable sequence. Tuples act like a list, but it can’t be modified.
- mutable vs. immutable objects
A mutable object is one that can be directly modified. For example, a list is mutable because we can modify it without changing its memory location. On the other hand, strings and tuples are immutable and cannot be directly modified. If you perform an operation where you think you have modified a string, in reality, Python has created a new string in a new memory location.
Files
- file object
An object that represents a connection to a file or file-like object. A more detailed explanation is that it is an object exposing a file-oriented API (with methods such as
read()orwrite()) to an underlying resource. Depending on the way it was created, a file object can be connected to to a real on-disk file or to another type of storage or communication device (for example standard input/output, in-memory buffers, sockets, pipes, etc.). File objects are also sometimes referred to as “streams”.- context manager
An object which controls the environment seen in a
withstatement block. Under the hood, this object has__enter__and__exit__methods such that the__enter__is executed at the start of thewithblock, and the__exit__method is guaranteed to be executed when the block is exited. For example, when using thiswithwhen opening a file (with open()…) guarantees that the file will be closed when leaving thewithblock.- CSV Files
A CSV file is a special kind of text file used to store tabular data. “CSV” stands for Comma Separated Values. The convention is to use commas, but in reality, files often have other “separators” such as tabs or the pipe symbol (
|).
Comprehensions
- comprehension
A comprehension uses special syntax for building a new iterable (list, set, or dictionary) out of any existing iterable, while either filtering items down based on a condition or modifying each item based on a mapping expression. It is denoted by
resultant-expression loop-expression conditional-expressionwhere theconditional-expressionis optional. This is enclosed inside either brackets or braces, depending on the type of the comprehension.- list comprehension
A list comprehension is denoted by the use of
[and]square brackets around the comprehension declaration.- set comprehension
A set comprehension is denoted by the use of
{and}curly brackets around the comprehension declaration.- dictionary comprehension
A dictionary comprehension is denoted by the use of
{and}curly brackets around the comprehension declaration, andkey: valuein theresultant-expressionlocation.- generator expressions
A generator expression has a similar format to a comprehension, but is used to make a “lazy iterable”, namely an iterable that does not retrieve or calculate the next item until it is requested by the code.
- lambda functions
A lambda function is an anonymous inline function consisting of a single expression which is evaluated when the function is called. The syntax to create a lambda function is
lambda [parameters]: expression.
Exceptions
- exceptions
An exception is a condition that happens when the code is executing and an error occurs. When we are writing our own code and we find something went wrong, we can raise an exception.
- try-except block
Two related blocks of code: The first one is the try block, where we put some code that might raise an exception. The second except block allows us to grab the execution if certain errors are detected.
Classes
- class
A class is a way to couple data and functionality together. Classes store data within each instance of that class (within the attributes). The functionality of a class comes from its methods. You can make a new class instance by calling the class. “Class” is synonymous with “type”, as all objects in Python are a class.
- class instance
An instance is an object of a specific class/type. It is created by calling the class. It may have attributes of data unique to the instance as defined by the class definition.
- class method
A class method is a function that operates on a class instance. The first argument in every method is usually called
self, by convention.- dunder methods
A dunder method is a method whose name starts and begins with a double underscore. For example,
__init__is called “dunder init”. This is much easier than saying “double underscore init double underscore”. Most special class methods are named this way, and the use of the term “dunder” is a conversational convenience. Python also has special dunder variables.- duck typing
In Python, we often do not care what type an object is, but whether it implements a particular functionality. From “If it looks like a duck and quacks like a duck, then we assume it is a duck”.
- inheritance
When we create a class, it is possible to have the class inherit from another existing class. Our new class will have all the attributes and behaviors of the class it is inheriting from.
Pythonic Code
- PEP 8
Python Enhancement Proposal #8, which is a guideline for coding style in Python.
- LBYL vs. EAFP
“LBYL” stands for “Look Before You Leap”, namely checking and validating things before using them or operating on them. “EAFP” stands for “Easier to Ask Forgiveness than Permission”, namely assuming things are fine and catching exceptions when they aren’t. Python generally uses EAFP.
- Zen of Python
PEP 20 defines the Zen of Python, the guiding principles for Python’s design. They are 20 aphorisms, only 19 of which are written. They can be accessed in the REPL with the statement
import this.