Computer Science
Grade 7-12
Python Syntax Quick Reference Cheat Sheet
A printable reference covering variables, data types, conditionals, loops, functions, lists, dictionaries, and common Python syntax for grades 7-12.
Related Tools
Related Labs
Related Worksheets
Related Infographics
Python syntax is the set of rules for writing Python programs so the computer can understand them. This cheat sheet covers the core commands and patterns students use when starting to code in Python. It is useful for quickly checking how to write variables, loops, functions, lists, dictionaries, and basic input or output. Students in grades 7-12 can use it while practicing coding, debugging, or preparing for class projects.
Key Facts
- Use a variable name, an equals sign, and a value to store data, such as score = 10.
- Common Python data types include int for whole numbers, float for decimals, str for text, and bool for True or False values.
- Use print(value) to display output and input("Prompt: ") to collect text input from the user.
- Use if, elif, and else with indented code blocks to make decisions, such as if score >= 90: print("A").
- Use a for loop to repeat over a sequence, such as for i in range(5): print(i).
- Use a while loop to repeat while a condition is True, such as while count < 3: count += 1.
- Define a function with def name(parameters): and return a value with return result.
- Access list items by index starting at 0, such as items[0], and add an item with items.append(value).
Vocabulary
- Variable
- A named storage location that holds a value, such as age = 14.
- Data Type
- A category of value in Python, such as int, float, str, bool, list, or dict.
- Condition
- An expression that evaluates to True or False and controls decisions in code.
- Loop
- A structure that repeats a block of code multiple times.
- Function
- A reusable block of code defined with def that can take inputs and return an output.
- Indentation
- Spaces at the start of a line that show which statements belong inside a code block.
Common Mistakes to Avoid
- Forgetting the colon after if, elif, else, for, while, or def is wrong because Python uses the colon to begin an indented code block.
- Mixing tabs and spaces for indentation is wrong because Python may not recognize the block structure correctly and can raise an IndentationError.
- Using = instead of == in a condition is wrong because = assigns a value, while == compares two values.
- Trying to add a string and a number directly, such as "Score: " + 10, is wrong because Python needs the number converted with str(10).
- Using the wrong list index is wrong because Python starts counting at 0, so the first item is list_name[0], not list_name[1].
Practice Questions
- 1 Write Python code that stores the number 25 in a variable named temperature and prints it.
- 2 What is printed by this code: total = 3 + 4 * 2; print(total)?
- 3 Write a for loop that prints the numbers 1 through 5.
- 4 Explain why indentation matters in Python when writing an if statement or a loop.