Loading lesson path
Python
Start with Python syntax, variables, control flow, and the core pieces every later lesson depends on.
Python is a popular programming language.
Python is a popular programming language. It was created by Guido van Rossum, and released in 1991.
However, if you want to run Python on your own computer, follow the instructions below.
Indentation refers to the spaces at the beginning of a code line.
A computer program is a list of "instructions" to be "executed" by a computer.
You have already learned that you can use the print() function to display text or output values:
You can also use the print() function to display numbers:
Comments can be used to explain Python code.
Variables are containers for storing data values.
A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).
Python allows you to assign values to multiple variables in one line:
The print() function is often used to output variables.
Variables that are created outside of a function (as in all of the examples in the previous pages) are known as global variables.
In programming, data type is an important concept.
There are three numeric types in Python:
There may be times when you want to specify a type on to a variable. This can be done with casting. Python is an object-orientated language, and as such it uses classes to define data types, includin…
Strings in python are surrounded by either single quotation marks, or double quotation marks.
You can return a range of characters by using the slice syntax.
Python has a set of built-in methods that you can use on strings.
To concatenate, or combine, two strings you can use the + operator.
But we can combine strings and numbers by using f-strings or the format() method!
To insert characters that are illegal in a string, use an escape character.
Python has a set of built-in methods that you can use on strings.
Booleans represent one of two values: True or False .
Operators are used to perform operations on variables and values.
Arithmetic operators are used with numeric values to perform common mathematical operations:
Assignment operators are used to assign values to variables:
Comparison operators are used to compare two values:
Logical operators are used to combine conditional statements:
Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location:
Membership operators are used to test if a sequence is presented in an object:
Bitwise operators are used to compare (binary) numbers:
Operator precedence describes the order in which operations are performed.
Lists are used to store multiple items in a single variable.
List items are indexed and you can access them by referring to the index number:
To change the value of a specific item, refer to the index number:
To add an item to the end of the list, use the append() method:
The remove() method removes the specified item.
You can loop through the list items by using a for loop:
List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list.
List objects have a sort() method that will sort the list alphanumerically, ascending, by default:
You cannot copy a list simply by typing list2 = list1 , because: list2 will only be a reference to list1 , and changes made in list1 will automatically also be made in list2 .
There are several ways to join, or concatenate, two or more lists in Python.
Python has a set of built-in methods that you can use on lists.
Tuples are used to store multiple items in a single variable.
You can access tuple items by referring to the index number, inside square brackets:
Tuples are unchangeable, meaning that you cannot change, add, or remove items once the tuple is created.
When we create a tuple, we normally assign values to it. This is called "packing" a tuple:
You can loop through the tuple items by using a for loop.
To join two or more tuples you can use the + operator:
Python has two built-in methods that you can use on tuples.
Sets are used to store multiple items in a single variable.
You cannot access items in a set by referring to an index or a key.
Once a set is created, you cannot change its items, but you can add new items.
To remove an item in a set, use the remove() , or the discard() method.
You can loop through the set items by using a for loop:
There are several ways to join two or more sets in Python.
frozenset is an immutable version of a set.
Python has a set of built-in methods that you can use on sets.
Dictionaries are used to store data values in key:value pairs.
You can access the items of a dictionary by referring to its key name, inside square brackets:
You can change the value of a specific item by referring to its key name:
Adding an item to the dictionary is done by using a new index key and assigning a value to it:
There are several methods to remove items from a dictionary:
You can loop through a dictionary by using a for loop.
You cannot copy a dictionary simply by typing dict2 = dict1 , because: dict2 will only be a reference to dict1 , and changes made in dict1 will automatically also be made in dict2 .
A dictionary can contain dictionaries, this is called nested dictionaries.
Python has a set of built-in methods that you can use on dictionaries.
Python supports the usual logical conditions from mathematics:
The elif keyword is Python's way of saying "if the previous conditions were not true, then try this condition".
The else keyword catches anything which isn't caught by the preceding conditions.
If you have only one statement to execute, you can put it on the same line as the if statement.
Logical operators are used to combine conditional statements. Python has three logical operators:
You can have if statements inside if statements. This is called nested if statements.
if statements cannot be empty, but if you for some reason have an if statement with no content, put in the pass statement to avoid getting an error.
The match statement is used to perform different actions based on different conditions.
Python has two primitive loop commands:
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
A function is a block of code which only runs when it is called.
Information can be passed into functions as arguments.
By default, a function must be called with the correct number of arguments.
A variable is only available from inside the region it is created. This is called scope .
Decorators let you add extra behavior to a function, without changing the function's code.
A lambda function is a small anonymous function.
Recursion is when a function calls itself.
Generators are functions that can pause and resume their execution.
The built-in range() function returns an immutable sequence of numbers, commonly used for looping a specific number of times.
Note: Python does not have built-in support for Arrays, but Python Lists can be used instead.
An iterator is an object that contains a countable number of values.
Consider a module to be the same as a code library.
A date in Python is not a data type of its own, but we can import a module named datetime to work with dates as date objects.
Python has a set of built-in math functions, including an extensive math module, that allows you to perform mathematical tasks on numbers.
JSON is a syntax for storing and exchanging data.
A RegEx, or Regular Expression, is a sequence of characters that forms a search pattern.
PIP is a package manager for Python packages, or modules if you like.
The try block lets you test a block of code for errors.
F-String was introduced in Python 3.6, and is now the preferred way of formatting strings.
None is a special constant in Python that represents the absence of a value.
Python allows for user input.
A virtual environment in Python is an isolated environment on your computer, where you can run and test your Python projects.