Python Cheat Sheet for Beginners: Syntax With Examples
Quick answer
A one-page Python 3 reference checked against the official docs: print, input, strings, if, for, range, lists, tuples, dictionaries and functions, with runnable code.
A Python cheat sheet is a one-page reference for the syntax you use daily: print and input, variables, if and else, for loops with range, lists, tuples, dictionaries and functions. This one targets Python 3, is checked against the official Python 3.15 documentation, and pairs every rule with a short program you can paste and run.
Key takeaways
- Indentation groups statements in Python, so consistent spacing is part of the syntax.
- Strings and tuples are immutable, while lists and dictionaries can be changed.
- The
/operator always returns a float, so8 / 5gives 1.6. - A function with no
returngives backNone.
On this page:
- Print, input and variables
- Numbers and strings
- If, for and range
- Lists, tuples and dictionaries
- Functions and None
- Common mistakes
- Frequently asked questions
| Type | Syntax | Changeable? | Access by |
|---|---|---|---|
| String | "Python" | No, immutable | Index or slice |
| List | [72, 85, 64] | Yes | Index or slice |
| Tuple | (1, 2) | No, immutable | Index or unpacking |
| Dictionary | {"name": "Asha"} | Yes | Unique key |
The version note: the official documentation pages used here show the Python 3.15 series, and the Python Developer's Guide lists 3.15 and 3.14 as bugfix versions as of late November 2026. Everything below is Python 3 syntax, with no Python 2 forms. We ran the examples on Python 3.15. Two blocks end with a line that raises a TypeError on purpose, marked in the comment. If your course names a version, follow it. Our guide to what is new in Python 3.15 covers changes.
Print, input and variables
Use print() to show output, input() to read from the keyboard, and = to store a value in a variable. According to the official Python tutorial, print() writes the value of the arguments it is given, strings print without quotes, and a space is inserted between items.
name = input("Name: ")
print("Hello,", name)
print(type(name))Type Asha when prompted and the output is Hello, Asha followed by <class 'str'>. That last line shows that input() gave back a string, even if you type digits. Wrap it in int() or float() when you need a number.
Numbers and strings
Arithmetic works as you expect, with one detail to remember. The tutorial says division with / always returns a float. Use // for floor division.
print(8 / 5) # 1.6
print(8 // 5) # 1
marks = [72, 85, 64]
print(sum(marks) / len(marks)) # 73.66666666666667The marks are made up for illustration. The average prints with many digits, so use round(value, 1) to tidy it.
Strings can be sliced. The tutorial states that the start of a slice is always included and the end is always excluded, so word[0:2] on "Python" gives Py and word[2:5] gives tho. It also says Python strings cannot be changed, because they are immutable.
word = "Python"
print(word[0:2], word[2:5]) # Py tho
word[0] = "J" # TypeError: 'str' object does not support item assignmentTo "change" a string, build a new one: "J" + word[1:] gives Jython.
If, for and range
An if statement chooses a path, and a for loop repeats. The control flow chapter says there can be zero or more elif parts and the else part is optional. The tutorial's introduction chapter says indentation is Python's way of grouping statements, so the body of each branch must be indented the same amount.
x = 7
if x > 10:
print("big")
elif x > 5:
print("medium")
else:
print("small") # prints medium for x = 7
for i in range(5):
print(i) # prints 0, 1, 2, 3 and 4The same chapter says a for statement iterates over the items of any sequence, such as a list or a string, in the order they appear, and that range() generates arithmetic progressions. The end value is excluded, so range(5) stops at 4. Our posts on a 30-day Python plan and programming for beginners show how these pieces build up.
Lists, tuples and dictionaries
Use a list for an ordered collection you will change, a tuple for a fixed group, and a dictionary for key and value pairs. The data structures chapter says list objects have methods including append, extend, insert, remove, pop, index, count, sort and reverse.
marks = [72, 85, 64]
marks.append(90)
marks.sort()
print(marks) # [64, 72, 85, 90]
student = {"name": "Asha", "city": "Pune"}
print(student["city"]) # Pune
for key, value in student.items():
print(key, value)
t = (1, 2)
t[0] = 5 # TypeError: 'tuple' object does not support item assignmentThe tutorial describes a dictionary as a set of key and value pairs, with the requirement that keys are unique within one dictionary. It says tuples are immutable and usually hold a heterogeneous sequence of elements accessed by unpacking or indexing. When you need to change data, choose a list or a dictionary.
Functions and None
Define a function with def. The control flow chapter says the keyword def introduces a function definition, and that falling off the end of a function also returns None.
def greet(name):
print("Hi", name)
result = greet("Ravi") # prints Hi Ravi
print(result) # NoneThe function prints, but it does not return anything, so result holds None. If you want a value back, use return, as in return sum(marks) / len(marks). Keep functions short and give them one job. For projects that use these pieces, see Python projects for beginners.
What are the common mistakes?
This list is our own, drawn from the rules above.
- Mixed indentation. Indentation groups statements, so inconsistent spaces change meaning or raise an error.
- Treating input as a number.
input()returns a string. Convert it before arithmetic. - Trying to edit a string or tuple. Both are immutable and raise a TypeError.
- Forgetting range excludes the end.
range(1, 5)gives 1 to 4. - Printing instead of returning. A function that only prints gives back None.
One caution on scope. The official tutorial introduction says it is designed for programmers new to Python, not for beginners new to programming. Use this sheet beside practice, and read the essential Python developer skills when you want to know what to learn next. For data work, the Python for data science beginners guide is the natural follow-up, and our HTML tags cheat sheet helps if you are also building web pages.
Next step
Paste each block into a file, run it and change one value before running it again. Then try Python interview questions to test what stuck. Keep the official tutorial at docs.python.org open as the definitive reference.
Frequently asked questions
What is the basic syntax of Python?
Python uses indentation to group statements, plain keywords such as if, for and def, and a colon at the end of block headers. Variables are created by assignment, with no type declaration.
How do I take input from the user in Python?
Call input() with a prompt, for example name = input('Name: '). It returns a string. Convert with int() or float() if you need a number for arithmetic.
What is the difference between a list, a tuple and a dictionary?
A list is an ordered, changeable collection. A tuple is ordered but immutable. A dictionary stores key and value pairs, and its keys must be unique within one dictionary.
Why does 8 / 5 give 1.6?
The Python tutorial says division with the / operator always returns a float. Use // for floor division, so 8 // 5 gives 1.
What is None in Python?
None is Python's no-value object. A function that ends without a return statement gives back None, so assigning the result of a print-only function to a variable stores None.
How do I loop over numbers from 0 to 4?
Use for i in range(5): and print i. The range function generates an arithmetic progression and excludes the end value, so it stops at 4.