0 day streak
Code library

Beginner Python exercises

Read these Beginner Python snippets line by line — each one comes with a written breakdown of what the code does and why.

Python · Beginner

Conditional Logic

If-statements allow your code to make decisions. The elif (else if) and else clauses provide alternative paths based on different conditions.

Control Flow
Open snippet →
Python · Beginner

Basic Exception Handling

Exception handling prevents your program from crashing when errors occur. The try block contains code that might fail, while the except block handles the error.

Error Handling
Open snippet →
Python · Beginner

Defining Functions

Functions are reusable blocks of code. They can take inputs (parameters) and return a result using the return keyword.

Functions
Open snippet →
Python · Beginner

List Operations

Lists are ordered collections of items. You can add items using the .append() method and access them using their zero-based index.

Data TypesArrays & Lists
Open snippet →
Python · Beginner

Variable Assignment and f-Strings

In Python, variables are created by assigning a value. f-strings (formatted string literals) allow you to embed variables directly inside strings using curly braces.

SyntaxData Types
Open snippet →
Python · Beginner

Python Dictionaries

Dictionaries store data in key-value pairs, allowing you to look up information using a specific label instead of an index.

SyntaxData Types
Open snippet →
Python · Beginner

The While Loop

A while loop repeatedly executes a block of code as long as a specified condition remains true.

SyntaxControl Flow
Open snippet →
Python · Beginner

Importing Modules

The import statement allows you to use code from external libraries or modules, such as the built-in math module for calculations.

SyntaxBest Practices
Open snippet →
Python · Beginner

String Slicing

Slicing allows you to extract a specific portion of a string by defining a start and end index.

SyntaxData Types
Open snippet →
Python · Beginner

Unique Items with Sets

Sets are collections of unique elements; they automatically remove any duplicate values provided during initialization.

SyntaxData Types
Open snippet →
Python · Beginner

List Comprehensions

List comprehensions offer a shorter syntax when you want to create a new list based on the values of an existing list.

SyntaxData Types
Open snippet →
Python · Beginner

Basic Exception Handling

The try-except block is used to catch and handle exceptions, preventing the program from crashing when an error occurs.

Error HandlingControl Flow
Open snippet →
Python · Beginner

Dictionary Key-Value Access

Dictionaries store data in key-value pairs. Using .get() is safer than square brackets because it returns a default value if the key is missing.

Data Types
Open snippet →
Python · Beginner

Python Tuples

Tuples are ordered, immutable collections of items. Once created, their contents cannot be changed, making them useful for fixed data structures.

Data TypesSyntax
Open snippet →
Python · Beginner

Working with Sets

Sets are unordered collections of unique elements. They automatically remove any duplicate values you try to add.

Data Types
Open snippet →
Python · Beginner

Using Enumerate

The enumerate() function adds a counter to an iterable and returns it, allowing you to track the index while looping.

Control FlowSyntax
Open snippet →
Python · Beginner

The Zip Function

Zip allows you to iterate over multiple lists at the same time by pairing their elements together.

Control Flow
Open snippet →
Python · Beginner

While Loops

A while loop repeats a block of code as long as a specified condition evaluates to True.

Control FlowSyntax
Open snippet →
Python · Beginner

List Slicing

Slicing allows you to extract a specific portion of a list using start and end indices.

Arrays & ListsSyntax
Open snippet →
Python · Beginner

Default Arguments

You can assign default values to parameters in a function definition, making them optional when calling the function.

Functions
Open snippet →
Python · Beginner

Type Conversion

Type conversion (casting) is used to change a value from one data type to another, such as converting a string to an integer.

Data TypesSyntax
Open snippet →
Python · Beginner

Getting User Input

The input() function allows a program to pause and wait for the user to type something. It always returns the input as a string.

SyntaxData Types
Open snippet →
Python · Beginner

The Global Keyword

The 'global' keyword allows you to modify a variable that is defined outside the current function's scope.

FunctionsSyntax
Open snippet →
Python · Beginner

Logical Operators: and & or

Logical operators allow you to check multiple conditions at once. 'and' requires both to be true, while 'or' requires at least one to be true.

Control FlowSyntax
Open snippet →