Python Tutorial for Beginners
Get hands-on and learn by building in this tutorial.

This walkthrough is made for beginners. It carries you from zero knowledge of Python all the way to tackling bite-sized Python exercises by the end of it.
There's zero Python background required to jump in. You can play around with real code and watch instant results appear without ever leaving your browser.
Pour yourself a coffee, kick back, and enjoy!
Table of Contents
# Strings
Any group of characters (letters, numbers, symbols) is called a string. Strings have to open and close with quotation marks. You can pick single ' or double ", as long as the same one shows up at both the start and the end.
xxxxxxxxxx"praxide.com"xxxxxxxxxx'praxide.com'Those quotes are there to signal that the wrapped text should be read as a value
# Variables
The label "variable" means "can change"; we use them to hold values that vary over time.
Think of a variable as a box with a sticky note attached to it. Reading the name written on that sticky note lets you reach whatever sits inside the box (variable). Much like the x and y variables you'd meet in mathematics, they're just a handy name that stands in for the data we want to point to.
Let's lean on Python to declare (i.e. create) a variable called animal:
xxxxxxxxxxanimal = "Fox"Within Python, there's no special command needed to declare a variable; it springs into being the instant you assign it a value.
= means place the value from the right-hand side into the variable on the left-hand side. Feel free to call a variable almost anything, but it can't include spaces.After the equals = sign, type the string "praxide.com". Keep in mind that every string has to be wrapped in quotes " ".
xxxxxxxxxxwebsite = Different from your old math class, the = sign here is an "assignment" operator, not an "equal to" operator. That would seem odd in algebra:
xxxxxxxxxxx = x + 1In Python, it works out the value of x + 1 and then stores that result in c. Put simply, the value of x gets bumped up by 1.
Let's build a brand-new variable together.
- Set up the variable food.
- Give it the value "pizza".
xxxxxxxxxx# enter code below this line# Numbers
Just like strings, numbers are values too, but they aren't wrapped in quotes. You can write them with or without decimals, and they may be positive or negative.
xxxxxxxxxxtemperature = -7.5When a number has no decimal point at all, we call it an integer.
Create a variable called day and give it the value 19.
xxxxxxxxxx# enter code below this line# Operators
Operators are the symbols placed between values that let you perform actions such as addition +, subtraction -, multiplication *, division /, and so on. Python ships with dozens of operators, but for now we'll stick to arithmetic operators, since those are the ones you'll reach for most as a beginner.
Split 100 by 2.
xxxxxxxxxxdivision = Naturally, there are plenty more operators, such as modulus %, exponentiation **, addition assignment +=, subtraction assignment -=, and beyond.
Use the + operator to join (concatenate) two separate strings into one.
- Set up the variable name.
- Join first_name together with last_name.
xxxxxxxxxxfirst_name = "Luke "last_name = "Skywalker"# enter code below this line# Functions
Functions are reusable chunks of code you can name and call again. They take in some data, do something specific with it, and return a result.
Here's what a simple function looks like:
xxxxxxxxxxdef add_two_numbers(num1, num2): return num1 + num2Can you figure out what it does? Let's break it down:
- add_two_numbers is this function's given name.
- num1 and num2 are parameters (that is, variables that hold the input data).
- return is the keyword that ends the function and sends back a value (output).
Making use of the return keyword really matters. Whenever you write a function, you have to return your answer, or the code simply won't run.
The function below accepts two parameters, each one holding a number.
- Multiply the first parameter num1 with the second parameter num2.
- Don't forget to use the return keyword, just like in the example above.
xxxxxxxxxxdef multiply_two_numbers(num1, num2): Functions will often accept parameters, though they aren't strictly required to.
xxxxxxxxxxdef hello(): return "Hello World!"Once the function hello() runs above, it will print out "Hello World!" for you.
# Lists
Lists, true to their name, hold a collection of items. In Python, lists behave much like arrays do in other languages. Here's how they look:
xxxxxxxxxxfruit = ["apple", "banana", "orange", "mango", "tomato"]Every fruit sitting in that list is known as an element. Even though each element here happens to be a string, elements can hold any value at all:
xxxxxxxxxxstuff = [True, 1976, None, "hey"]# Indexes
Every element in a list carries an index that begins at 0. Going back to our first fruit example, "apple" sits at index 0, "banana" at index 1, "orange" at 2, and this pattern runs to the end.
To grab one particular element from a list, we do this:
xxxxxxxxxxfruit[2]And that's the index for "orange".
Give back the element "Donatello".
- Keep in mind the first element in a list is 0.
- Don't forget to return it!
xxxxxxxxxxturtles = ["Raphael", "Michelangelo", "Leonardo", "Donatello"]def turtle_power(turtles): # Mutability
Items inside lists are mutable, meaning you're free to change them. Say, for instance, we have a list holding some numbers:
xxxxxxxxxxnumbers = [14, 56, 78]To swap the value 14 (found at index 0), we do this:
xxxxxxxxxxnumbers[0] = 35Given a list of numbers, update the value stored at index 1 so it becomes 88.
xxxxxxxxxxnumbers = [1, 4, 6, 8, 0]# enter code below this line# len()
Most of the time, you won't know a list's exact length up front. In other words, you can't tell how many elements it holds. That's precisely where the.length function steps in.
Imagine we've got a list of movie titles:
xxxxxxxxxxmovies = ["The Matrix", "Se7en", "The Wizard of Oz", "Marry Poppins"]To find the exact length of the movies list, we'd reach for the len() function.
xxxxxxxxxxlen(movies)That hands you 4, since there are four elements inside the movies list.
Given a list lst of unknown length, return how long it is.
xxxxxxxxxxdef get_length(lst): # Dictionaries
Lists work great for straightforward collections, but for other jobs they get awkward. Picture a list of names:
xxxxxxxxxxfirst_names = ["Tyrion", "Jon", "Jorah", "Arya", "Joffrey"]last_names = ["Lannister", "Snow", "Mormont", "Stark", "Baratheon"]Suppose you have a last name in mind (say it's Snow) and you'd like to find his first name. Using lists, that turns into a real chore, because "Jon" lives in one list while "Snow" (his last name) sits in a completely separate one.
This quickly gets messy, because the moment we add a new person to the first_name list, we also have to update the last_name list. And once we track more than just a first and last name, it grows complicated.
Dictionaries give you a tidier way to store and keep track of information, like this:
xxxxxxxxxxperson1 = { "first_name": "Joffery", "last_name": "Baratheon", "email": "joff@widowswail.com"}person2 = { "first_name": "Jon", "last_name": "Snow", "email": "brooding@thewall.com"}person3 = { "first_name": "Tyrion", "last_name": "Lannister", "email": "tyrion@pourmeanother.com"}Now each person gets their own variable, letting us fetch their values in a way that's much more maintainable and readable.
# Key-Value Pairs
Think of a dictionary as a wall of labeled lockers, where every label points to one locker that can hold whatever you tuck away inside. Because each locker is clearly named, you can reach in and grab exactly what you need in a snap.
xxxxxxxxxxperson2 = { "first_name": "Jon", "last_name": "Snow", "email": "brooding@thewall.com"}Whatever sits to the left of the: is a key, and whatever sits to the right is a value. Each of these matched sets is what we call key-value
Add the following key-value pairs to person4 below.
- Set first_name to the value "Daenerys".
- Set last_name to the value "Targaryen".
- Set email to the value "dragonlady@gmail.com".
xxxxxxxxxxperson4 = { }