bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/Python

Python

Foundations Practice

Exercises, quizzes, examples, and short drills that reinforce foundations.

Lesson 1

Python Getting Started

Get Started With Python At W3Schools, you can try Python without installing anything. Our Online Python Editor runs directly in your browser, and shows both the code and the result: Example print("He…

3 min
Read lesson →
Lesson 2

Python Syntax Code Challenge

Challenge: Statements Test your understanding of Python statements by completing a small coding challenge.

2 min
Read lesson →
Lesson 3

Python Output / Print

Print Text You have already learned that you can use the print() function to display text or output values: Example print("Hello World!") You can use the print() function as many times as you want. E…

2 min
Read lesson →
Lesson 4

Python Output Numbers

Print Numbers You can also use the print() function to display numbers: However, unlike text, we don't put numbers inside double quotes: Example print(3) print(358) print(50000) You can also do math…

2 min
Read lesson →
Lesson 5

Python Output Code Challenge

Challenge: Output / Print Test your understanding of Python output by completing a small coding challenge.

2 min
Read lesson →
Lesson 6

Python Comments Code Challenge

Challenge: Comments Test your understanding of Python comments by completing a small coding challenge.

2 min
Read lesson →
Lesson 7

Python Variables - Assign Multiple Values

Many Values to Multiple Variables Python allows you to assign values to multiple variables in one line: Example x, y, z = "Orange", "Banana", "Cherry" print(x) print(y) print(z) Note: Make sure the n…

2 min
Read lesson →
Lesson 8

Python - Output Variables

Output Variables The print() function is often used to output variables. Example x = "Python is awesome" print(x) In the print() function, you output multiple variables, separated by a comma: Example…

2 min
Read lesson →
Lesson 9

Python - Global Variables

Global Variables Variables that are created outside of a function (as in all of the examples in the previous pages) are known as global variables. Global variables can be used by everyone, both insid…

2 min
Read lesson →
Lesson 10

Python - Variable Exercises

Python - Variable Exercises

2 min
Read lesson →
Lesson 11

Python Variables Code Challenge

Challenge: Variables Test your understanding of Python variables by completing a small coding challenge.

2 min
Read lesson →
Lesson 12

Python Data Types Code Challenge

Challenge: Data Types Test your understanding of Python data types by completing a small coding challenge.

2 min
Read lesson →
Lesson 13visual

Python Numbers

Python Numbers There are three numeric types in Python: int float complex Variables of numeric types are created when you assign a value to them: Example x = 1 # int y = 2.8 # float z = 1j # complex…

2 min
Read lesson →
Lesson 14

Python Numbers Code Challenge

Challenge: Numbers Test your understanding of Python number types by completing a small coding challenge.

2 min
Read lesson →
Lesson 15

Python Casting Code Challenge

Challenge: Casting Test your understanding of Python type casting by completing a small coding challenge.

2 min
Read lesson →
Lesson 16

Python - Slicing Strings

Slicing You can return a range of characters by using the slice syntax. Specify the start index and the end index, separated by a colon, to return a part of the string. Example Get the characters fro…

2 min
Read lesson →
Lesson 17visual

Python - Modify Strings

Python has a set of built-in methods that you can use on strings. Upper Case Example The upper() method returns the string in upper case: a = "Hello, World!" print(a.upper()) Lower Case Example The l…

2 min
Read lesson →
Lesson 18

Python - String Concatenation

String Concatenation To concatenate, or combine, two strings you can use the + operator. Example Merge variable a with variable b into variable c a = "Hello" b = "World" c = a + b print(c) Example To…

2 min
Read lesson →
Lesson 19

Python - Format - Strings

String Format As we learned in the Python Variables chapter, we cannot combine strings and numbers like this: Example age = 36 #This will produce an error: txt = "My name is John, I am " + age print(…

2 min
Read lesson →
Lesson 20

Python - Escape Characters

Escape Character To insert characters that are illegal in a string, use an escape character. An escape character is a backslash \ followed by the character you want to insert. An example of an illega…

2 min
Read lesson →
Lesson 21

Python - String Exercises

Test Yourself With Exercises Now you have learned a lot about Strings, and how to use them in Python. Are you ready for a test? Test your Python String skills with exercises from all categories: Stri…

2 min
Read lesson →
Lesson 22

Python Strings Code Challenge

Challenge: Strings Basics Test your understanding of creating, slicing, modifying, and formatting strings.

2 min
Read lesson →
Lesson 23

Python Booleans Code Challenge

Challenge: Booleans Test your understanding of Python booleans by completing a small coding challenge.

2 min
Read lesson →
Lesson 24

Python Operators

Python Operators Operators are used to perform operations on variables and values. In the example below, we use the + operator to add together two values: Example print(10 + 5) Although the + operato…

2 min
Read lesson →
Lesson 25

Python Arithmetic Operators

Arithmetic Operators Arithmetic operators are used with numeric values to perform common mathematical operations: Operator Name Example Try it + Addition x + y Try it » Subtraction x - y Try it » * M…

2 min
Read lesson →
Lesson 26

Python Assignment Operators

Assignment Operators Assignment operators are used to assign values to variables: Operator Example Same As Try it = x = 5 x = 5 Try it » += x += 3 x = x + 3 Try it » -= x -= 3 x = x - 3 Try it » *= x…

2 min
Read lesson →
Lesson 27

Python Comparison Operators

Comparison Operators Comparison operators are used to compare two values: Operator Name Example Try it == Equal x == y Try it » != Not equal x != y Try it » > Greater than x > y Try it » < Less than…

2 min
Read lesson →
Lesson 28

Python Logical Operators

Logical Operators Logical operators are used to combine conditional statements: Operator Description Example Try it and Returns True if both statements are true x < 5 and x < 10 Try it » or Returns T…

2 min
Read lesson →
Lesson 29

Python Identity Operators

Identity Operators 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: Operator Description Example Try…

2 min
Read lesson →
Lesson 30

Python Membership Operators

Membership Operators Membership operators are used to test if a sequence is presented in an object: Operator Description Example Try it in Returns True if a sequence with the specified value is prese…

2 min
Read lesson →
Lesson 31

Python Bitwise Operators

Bitwise Operators Bitwise operators are used to compare (binary) numbers: Operator Name Description Example Try it & AND Sets each bit to 1 if both bits are 1 x & y Try it » | OR Sets each bit to 1 i…

2 min
Read lesson →
Lesson 32

Python Operator Precedence

Operator Precedence Operator precedence describes the order in which operations are performed. Example Parentheses has the highest precedence, meaning that expressions inside parentheses must be eval…

2 min
Read lesson →
Lesson 33

Python Operators Code Challenge

Challenge: Operators Basics Test your understanding of arithmetic, assignment, and comparison operators.

2 min
Read lesson →
Lesson 34

Python - Access List Items

Access Items List items are indexed and you can access them by referring to the index number: Example Print the second item of the list: thislist = ["apple", "banana", "cherry"] print(thislist[1]) No…

2 min
Read lesson →
Lesson 35

Python - Change List Items

Change Item Value To change the value of a specific item, refer to the index number: Example Change the second item: thislist = ["apple", "banana", "cherry"] thislist[1] = "blackcurrant" print(thisli…

2 min
Read lesson →
Lesson 36visual

Python - Add List Items

Append Items To add an item to the end of the list, use the append() method: Example Using the append() method to append an item: thislist = ["apple", "banana", "cherry"] thislist.append("orange") pr…

2 min
Read lesson →
Lesson 37

Python - Remove List Items

Remove Specified Item The method removes the specified item. Example Remove "banana": thislist = ["apple", "banana", "cherry"] thislist.remove("banana") print(thislist) If there are more than one ite…

2 min
Read lesson →
Lesson 38

Python - Loop Lists

Loop Through a List You can loop through the list items by using a for loop: Example Print all items in the list, one by one: thislist = ["apple", "banana", "cherry"] for x in thislist: print(x) Lear…

2 min
Read lesson →
Lesson 39

Python - List Comprehension

List Comprehension List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list. Example: Based on a list of fruits, you want a new list, cont…

2 min
Read lesson →
Lesson 40

Python - Sort Lists

Sort List Alphanumerically List objects have a sort() method that will sort the list alphanumerically, ascending, by default: Example Sort the list alphabetically: thislist = ["orange", "mango", "kiw…

2 min
Read lesson →
Lesson 41

Python - Join Lists

Join Two Lists There are several ways to join, or concatenate, two or more lists in Python. One of the easiest ways are by using the + operator. Example Join two list: list1 = ["a", "b", "c"] list2 =…

2 min
Read lesson →
Lesson 42

Python List Exercises

Python List Exercises

2 min
Read lesson →
Lesson 43

Python Lists Code Challenge

Challenge: Lists Basics Test your understanding of creating, accessing, changing, adding, and removing list items.

2 min
Read lesson →
Lesson 44

Python - Access Tuple Items

Access Tuple Items You can access tuple items by referring to the index number, inside square brackets: Example Print the second item in the tuple: thistuple = ("apple", "banana", "cherry") print(thi…

2 min
Read lesson →
Lesson 45

Python - Unpack Tuples

Unpacking a Tuple When we create a tuple, we normally assign values to it. This is called "packing" a tuple: Example Packing a tuple: fruits = ("apple", "banana", "cherry") But, in Python, we are als…

2 min
Read lesson →
Lesson 46

Python - Loop Tuples

Loop Through a Tuple You can loop through the tuple items by using a for loop. Example Iterate through the items and print the values: thistuple = ("apple", "banana", "cherry") for x in thistuple: pr…

2 min
Read lesson →
Lesson 47

Python - Join Tuples

Join Two Tuples To join two or more tuples you can use the + operator: Example Join two tuples: tuple1 = ("a", "b" , "c") tuple2 = (1, 2, 3) tuple3 = tuple1 + tuple2 print(tuple3) Multiply Tuples If…

2 min
Read lesson →
Lesson 48

Python - Tuple Exercises

Python - Tuple Exercises

2 min
Read lesson →
Lesson 49

Python Tuples Code Challenge

Challenge: Tuples Basics Test your understanding of creating, accessing, and unpacking tuples.

2 min
Read lesson →
Lesson 50visual

Python - Add Set Items

Add Items Once a set is created, you cannot change its items, but you can add new items. To add one item to a set use the add() method. Example Add an item to a set, using the add() method: thisset =…

2 min
Read lesson →
Lesson 51

Python - Remove Set Items

Remove Item To remove an item in a set, use the , or the discard() method. Example Remove "banana" by using the method: thisset = {"apple", "banana", "cherry"} thisset.remove("banana") print(thisset)…

2 min
Read lesson →
Lesson 52

Python - Loop Sets

Loop Items You can loop through the set items by using a for loop: Example Loop through the set, and print the values: thisset = {"apple", "banana", "cherry"} for x in thisset: print(x)

2 min
Read lesson →
Lesson 53

Python - Set Exercises

Python - Set Exercises

2 min
Read lesson →
Lesson 54

Python Sets Code Challenge

Challenge: Sets Basics Test your understanding of creating, adding, removing, and checking set items.

2 min
Read lesson →
Lesson 55visual

Python - Access Dictionary Items

Accessing Items You can access the items of a dictionary by referring to its key name, inside square brackets: Example Get the value of the "model" key: thisdict = { "brand": "Ford", "model": "Mustan…

3 min
Read lesson →
Lesson 56visual

Python - Change Dictionary Items

Change Values You can change the value of a specific item by referring to its key name: Example Change the "year" to 2018: thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } thisdict["y…

2 min
Read lesson →
Lesson 57visual

Python - Add Dictionary Items

Adding Items Adding an item to the dictionary is done by using a new index key and assigning a value to it: Example thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } thisdict["color"]…

2 min
Read lesson →
Lesson 58visual

Python - Remove Dictionary Items

Removing Items There are several methods to remove items from a dictionary: Example The pop() method removes the item with the specified key name: thisdict = { "brand": "Ford", "model": "Mustang", "y…

2 min
Read lesson →
Lesson 59visual

Python - Nested Dictionaries

Nested Dictionaries A dictionary can contain dictionaries, this is called nested dictionaries. Example Create a dictionary that contain three dictionaries: myfamily = { "child1" : { "name" : "Emil",…

2 min
Read lesson →
Lesson 60visual

Python Dictionary Exercises

Python Dictionary Exercises

2 min
Read lesson →
Lesson 61visual

Python Dictionaries Code Challenge

Challenge: Dictionaries Basics Test your understanding of creating, accessing, changing, adding, and removing dictionary items.

2 min
Read lesson →
Lesson 62

Python Shorthand If

Short Hand If If you have only one statement to execute, you can put it on the same line as the if statement. Example One-line if statement: a = 5 b = 2 if a > b: print("a is greater than b") Note: Y…

2 min
Read lesson →
Lesson 63

Python Nested If

Nested If Statements You can have if statements inside if statements. This is called nested if statements. Example x = 41 if x > 10: print("Above ten,") if x > 20: print("and also above 20!") else: p…

2 min
Read lesson →
Lesson 64

Python Pass Statement

The pass Statement 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. Example a = 33 b = 200 if b > a…

2 min
Read lesson →
Lesson 65

Python If...Else Code Challenge

Challenge: If...Else Basics Test your understanding of if, elif, else, and shorthand if statements.

2 min
Read lesson →
Lesson 66

Python Match Code Challenge

Challenge: Match Test your understanding of Python match by completing a small coding challenge.

2 min
Read lesson →
Lesson 67

Python While Loops

Python Loops Python has two primitive loop commands: while loops for loops The while Loop With the while loop we can execute a set of statements as long as a condition is true. Example Print i as lon…

2 min
Read lesson →
Lesson 68

Python While Loops Code Challenge

Challenge: While Loops Test your understanding of Python while loops by completing a small coding challenge.

2 min
Read lesson →
Lesson 69

Python For Loops Code Challenge

Challenge: For Loops Test your understanding of Python for loops by completing a small coding challenge.

2 min
Read lesson →
Lesson 70

Python Functions Code Challenge

Challenge: Functions Basics Test your understanding of creating functions, parameters, return values, and *args.

2 min
Read lesson →
Lesson 71

Python Range Code Challenge

Challenge: Range Test your understanding of Python range by completing a small coding challenge.

2 min
Read lesson →
Lesson 72

Python Arrays Code Challenge

Challenge: Arrays Test your understanding of Python arrays by completing a small coding challenge.

2 min
Read lesson →
Lesson 73

Python Iterators Code Challenge

Challenge: Iterators Test your understanding of Python iterators by completing a small coding challenge.

2 min
Read lesson →
Lesson 74

Python Modules Code Challenge

Challenge: Modules Test your understanding of Python modules by completing a small coding challenge.

2 min
Read lesson →
Lesson 75visual

Python Datetime

Python Dates 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. Example Import the datetime module and display the current d…

3 min
Read lesson →
Lesson 76

Python Dates Code Challenge

Challenge: Dates Test your understanding of Python dates by completing a small coding challenge.

2 min
Read lesson →
Lesson 77

Python Math Code Challenge

Challenge: Math Test your understanding of Python math by completing a small coding challenge.

2 min
Read lesson →
Lesson 78

Python JSON Code Challenge

Challenge: JSON Test your understanding of Python json by completing a small coding challenge.

2 min
Read lesson →
Lesson 79

Python RegEx Code Challenge

Challenge: RegEx Test your understanding of Python regex by completing a small coding challenge.

2 min
Read lesson →
Lesson 80

Python Try...Except Code Challenge

Challenge: Try...Except Test your understanding of Python try...except by completing a small coding challenge.

2 min
Read lesson →
Lesson 81

Python String Formatting Code Challenge

Challenge: String Formatting Test your understanding of Python string formatting by completing a small coding challenge.

2 min
Read lesson →
Lesson 82

Python None Code Challenge

Challenge: None Test your understanding of Python's None value by completing a small coding challenge.

2 min
Read lesson →
Lesson 83

Python User Input

User Input Python allows for user input. That means we are able to ask the user for input. The following example asks for your name, and when you enter a name, it gets printed on the screen: Example…

2 min
Read lesson →