Skip to content
SoftwareSolutions.net
Cheat sheet

Python Basics Syntax Cheat Sheet

Core Python syntax for variables, types, lists, dictionaries, loops and functions, with short examples you can run.

Python uses indentation (four spaces) to group code. Run a file with python3 file.py, or type commands in the interactive prompt.

Values and text

  • x = 5 stores a number
  • name = "Ana" stores text
  • print(f"Hello {name}") prints with a variable inside
  • type(x) shows the data type
  • len(name) counts characters

Lists and dictionaries

  • items = [1, 2, 3] is a list
  • items.append(4) adds to the end
  • scores = {"Ana": 90} is a dictionary
  • scores["Ana"] reads a value by key

Control and functions

  • if x > 3: then an indented block
  • for i in range(5): loops 0 to 4
  • def add(a, b): return a + b defines a function
  • # starts a comment