Python Code Examples - Practical Programs for Learning
A collection of ready-to-use Python code examples for beginners and intermediate programmers. Run every program directly in your browser using our editor - no installation required.
You'll find practical scripts here: from simple calculators and unit converters, through GUI applications, to simulations using the Turtle library. All examples include clear comments and explanations.
Copy, modify, break, and learn from working code. This is the fastest way to master Python - instead of reading theory, you immediately see how things work in practice.
Check Python Version
Simple way to check installed Python version on your system. First step for every programmer.
First Program - Hello Space
Classic first program in any programming language. Your first step into Python space.
Import System Modules
Learning to import modules in Python. Modules extend your program capabilities.
Comments in Python Code
How to write comments in Python. Comments help other programmers understand your code.
Print Function Basics
The print() function is the basic tool for displaying data in Python console.
Text Formatting with f-strings
F-strings are a modern way to format text in Python. Insert variables directly into text.
Print Parameters sep and end
Parameters sep and end let you control separator and line ending in print function.
Special Characters in Print
Special characters like \n and \t allow formatting text with new lines and tabs.
Creating Variables in Python
Variables store data in your program. Learning to create and name variables in Python.
Basic Data Types
Python recognizes different data types automatically. Learn int, float, str and bool.
Type Conversion
Changing data types using int(), float() and str() functions. Converting between types.
Dynamic Typing in Python
Python allows changing variable type during program execution. This is dynamic typing.
Basic Math Operations
Addition, subtraction, multiplication and division in Python. Basic arithmetic operations.
Floor Division and Modulo
Operator // for floor division and % for remainder (modulo) in Python.
Exponentiation and Roots
Operator ** for exponentiation and calculating roots in Python.
Order of Operations
Order of operations in Python. Parentheses, exponents, multiplication, division, addition.
Creating and Joining Strings
How to create text and join it in Python. Basics of working with strings.
String Indexing and Slicing
Accessing individual characters and text fragments. Zero-based indexing in Python.
String Methods - upper, lower, strip
Useful methods for working with text: changing case, removing whitespace.
Methods find, replace and split
Searching, replacing and splitting text. Advanced string manipulation methods.
Basic if Statement
Conditional if statement allows executing code only when condition is met.
If-else Statement
The else block executes when if condition is not met. Two possible paths.
If-elif-else Statement
Multiple conditions with elif. Checking several possibilities in sequence.
Logical Operators and, or, not
Combining conditions with logical operators and, or and not.
Creating Lists in Python
Lists store multiple elements in a specific order. Basics of working with lists.
List Indexing and Modification
Accessing list elements by index. Changing and removing elements.
List Methods - append, insert, pop
Adding and removing elements from list. Methods append, insert, pop and remove.
Sorting and Reversing Lists
Methods sort() and reverse() for ordering list elements.
Creating Tuples in Python
Tuples are immutable lists. Once created, they cannot be modified.
Tuple Unpacking
Tuple unpacking assigns its elements to separate variables simultaneously.
Tuples vs Lists - Differences
When to use tuples instead of lists? Comparison of properties and applications.
Tuple Methods count and index
Tuples have only two methods: count() for counting and index() for searching.
Creating Dictionaries in Python
Dictionaries store data as key-value pairs. Fast access to data by key.
Modifying Dictionaries
Adding, changing and removing dictionary elements in Python.
Iterating Over Dictionary
Looping through keys, values or key-value pairs of a dictionary.
Nested Dictionaries
Dictionaries can contain other dictionaries. Useful for complex data structures.
Creating Sets in Python
Sets store only unique elements without a specific order.
Set Operations
Adding and removing elements from a set. Basic set operations.
Mathematical Set Operations
Union, intersection and difference of sets. Mathematical operations from set theory.
Comparing Sets
Checking if a set is a subset or superset of another set.
For Loop Basics
For loop iterates through sequence elements. Basic iteration over lists.
For Loop with enumerate and range
Functions enumerate() and range() extend for loop capabilities in Python.
While Loop Basics
While loop executes as long as condition is true. Controlling iteration with condition.
Break and Continue in Loops
Break (exit) and continue (skip) statements for flow control in loops.
Creating Simple Functions
Functions group code for reuse. Defining functions with def keyword.
Functions with Parameters
Parameters allow passing data to functions. Functions become more flexible.
Default Parameter Values
Parameters can have default values. Used when argument is not provided.
Documenting Functions
Docstrings describe what a function does. Good programming practice.
Positional and Keyword Arguments
Difference between positional and keyword arguments. Flexible function calling.
Variable Number of Arguments *args
Operator * allows accepting any number of positional arguments.
Variable Keyword Arguments **kwargs
Operator ** allows accepting any number of keyword arguments.
Combining Different Argument Types
Combining regular parameters, *args and **kwargs in one function.
Return Statement Basics
Return statement returns a value from function. Function can perform calculations and give result.
Returning Multiple Values
Function can return multiple values as a tuple. Unpacking on assignment.
Early Exit from Function
Return can be used to exit function early when a condition is met.
Returning Dictionary or List
Functions can return complex data structures like dictionaries or lists.
Local and Global Variables
Local variables exist only inside functions. Global ones are accessible everywhere.
The global Keyword
The global keyword allows modifying a global variable inside a function.
Scope in Nested Functions
The nonlocal keyword gives access to variables in the enclosing function.
Separating Namespaces
Namespaces isolate variables. Modules and functions create their own namespaces.