Loading lesson path
Python
Start with Python syntax, variables, control flow, and the core pieces every later lesson depends on.
Python Tutorial
Python Introduction
Execute Python Syntax As we learned in the previous page, Python syntax can be executed by writing directly in the Command Line: >>> print("Hello, World!") Hello, World! Or by creating a python file…
Statements A computer program is a list of "instructions" to be "executed" by a computer. In a programming language, these programming instructions are called statements. The following statement prin…
Comments can be used to explain Python code. Comments can be used to make the code more readable. Comments can be used to prevent execution when testing code. Creating a Comment Comments starts with…
Python Variables
Variable Names A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for Python variables: A variable name must start with a letter or the und…
Built-in Data Types In programming, data type is an important concept. Variables can store data of different types, and different types can do different things. Python has the following data types bu…
Specify a Variable Type 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 def…
Strings Strings in python are surrounded by either single quotation marks, or double quotation marks. 'hello' is the same as "hello". You can display a string literal with the print() function: Examp…
String Methods Python has a set of built-in methods that you can use on strings. Note: All string methods return new values. They do not change the original string. Method Description capitalize() Co…
Booleans represent one of two values: True or False. Boolean Values In programming you often need to know if an expression is True or False. You can evaluate any expression in Python, and get one of…
mylist = ["apple", "banana", "cherry"] List Lists are used to store multiple items in a single variable. Lists are one of 4 built-in data types in Python used to store collections of data, the other…
Copy a List You cannot copy a list simply by typing list2 = list1, because: list2 will only be a to list1, and changes made in list1 will automatically also be made in list2. Use the copy() method Yo…
List Methods Python has a set of built-in methods that you can use on lists. Method Description append() Adds an element at the end of the list clear() Removes all the elements from the list copy() R…
mytuple = ("apple", "banana", "cherry") Tuple Tuples are used to store multiple items in a single variable. Tuple is one of 4 built-in data types in Python used to store collections of data, the othe…
Tuples are unchangeable, meaning that you cannot change, add, or remove items once the tuple is created. But there are some workarounds. Change Tuple Values Once a tuple is created, you cannot change…
Tuple Methods Python has two built-in methods that you can use on tuples. Method Description count() Returns the number of times a specified value occurs in a tuple index() Searches the tuple for a s…
myset = {"apple", "banana", "cherry"} Set Sets are used to store multiple items in a single variable. Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are…
Access Items You cannot access items in a set by referring to an index or a key. But you can loop through the set items using a for loop, or ask if a specified value is present in a set, by using the…
Join Sets There are several ways to join two or more sets in Python. The union() and update() methods joins all items from both sets. The intersection() method keeps ONLY the duplicates. The differen…
Python frozenset frozenset is an immutable version of a set. Like sets, it contains unique, unordered, unchangeable elements. Unlike sets, elements cannot be added or removed from a frozenset. Creati…
Set Methods Python has a set of built-in methods that you can use on sets. Method Shortcut Description add() Adds an element to the set clear() Removes all the elements from the set copy() Returns a…
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } Dictionary Dictionaries are used to store data values in key:value pairs. A dictionary is a collection which is ordered*, changeable a…
Loop Through a Dictionary You can loop through a dictionary by using a for loop. When looping through a dictionary, the return value are the keys of the dictionary, but there are methods to return th…
Copy a Dictionary You cannot copy a dictionary simply by typing dict2 = dict1, because: dict2 will only be a to dict1, and changes made in dict1 will automatically also be made in dict2. There are wa…
Dictionary Methods Python has a set of built-in methods that you can use on dictionaries. Method Description clear() Removes all the elements from the dictionary copy() Returns a copy of the dictiona…
Python Conditions and If statements Python supports the usual logical conditions from mathematics: Equals: a == b Not Equals: a != b Less than: a < b Less than or equal to: a <= b Greater than: a > b…
The Elif Keyword The elif keyword is Python's way of saying "if the previous conditions were not true, then try this condition". The elif keyword allows you to check multiple expressions for True and…
The Else Keyword The else keyword catches anything which isn't caught by the preceding conditions. The else statement is executed when the if condition (and any elif conditions) evaluate to False. Ex…
Python Logical Operators Logical operators are used to combine conditional statements. Python has three logical operators: and - Returns True if both statements are true or - Returns True if one of t…
The match statement is used to perform different actions based on different conditions. The Python Match Statement Instead of writing many if..else statements, you can use the match statement. The ma…
Python For Loops A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). This is less like the for keyword in other programming languages,…
Python Functions A function is a block of code which only runs when it is called. A function can return data as a result. A function helps avoiding code repetition. Creating a Function In Python, a f…
Arguments Information can be passed into functions as arguments. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them…
*args and **kwargs By default, a function must be called with the correct number of arguments. However, sometimes you may not know how many arguments that will be passed into your function. *args and…
Scope A variable is only available from inside the region it is created. This is called scope. Local Scope A variable created inside a function belongs to the local scope of that function, and can on…
Decorators let you add extra behavior to a function, without changing the function's code. A decorator is a function that takes another function as input and returns a new function. Basic Decorator D…
Lambda Functions A lambda function is a small anonymous function. A lambda function can take any number of arguments, but can only have one expression. Syntax lambda arguments expression The expressi…
Recursion Recursion is when a function calls itself. Recursion is a common mathematical and programming concept. It means that a function calls itself. This has the benefit of meaning that you can lo…
Generators Generators are functions that can pause and resume their execution. When a generator function is called, it returns a generator object, which is an iterator. The code inside the function i…
Python range The built-in range() function returns an immutable sequence of numbers, commonly used for looping a specific number of times. This set of numbers has its own data type called range. Note…
Note: Python does not have built-in support for Arrays, but Python Lists can be used instead. Arrays Note: This page shows you how to use LISTS as ARRAYS, however, to work with arrays in Python you w…
Python Iterators An iterator is an object that contains a countable number of values. An iterator is an object that can be iterated upon, meaning that you can traverse through all the values. Technic…
Python Modules
Python has a set of built-in math functions, including an extensive math module, that allows you to perform mathematical tasks on numbers. Built-in Math Functions The min() and max() functions can be…
JSON is a syntax for storing and exchanging data. JSON is text, written with JavaScript object notation. JSON in Python Python has a built-in package called json, which can be used to work with JSON…
A RegEx, or Regular Expression, is a sequence of characters that forms a search pattern. RegEx can be used to check if a string contains the specified search pattern. RegEx Module Python has a built-…
Python PIP
The try block lets you test a block of code for errors. The except block lets you handle the error. The else block lets you execute code when there is no error. The finally block lets you execute cod…
F-String was introduced in Python 3.6, and is now the preferred way of formatting strings. Before Python 3.6 we had to use the format() method. F-Strings F-string allows you to format selected parts…
Python None None is a special constant in Python that represents the absence of a value. Its data type is NoneType, and None is the only instance of a NoneType object. NoneType Variables can be assig…
Python Virtual Environment