Python syntax is designed for readability and uses indentation to define code blocks. Here's a breakdown of the basics:
Unlike many other languages that use braces {}
, Python uses indentation to define blocks of code. Consistent indentation (typically 4 spaces) is crucial.
Comments explain your code. Use #
for single-line comments and triple quotes ''' '''
or """ """
for multi-line comments.
Variables store data. Assignment is done using =
. Variable names are case-sensitive.
No explicit type declaration is needed (Python infers type).
The most common way to display information when programming is by printing the data directly onto your computer's output. In Python, this output capability is delivered with the print()
function which is prebuilt within all native installs of Python on your computer. To utilize this, you need only use parentheses alongside the word print.
Python supports several data types including:
int
): Whole numbers (e.g., 10, -5, 0).float
): Numbers with decimal points (e.g., 3.14, -2.5).str
): Sequences of characters enclosed in single or double quotes (e.g., "hello", 'Python').bool
): True
or False
values.list
): Ordered, mutable sequences of items (e.g., [1, 2, "three"]).tuple
): Ordered, immutable sequences of items (e.g., (1, 2, "three")).set
): Unordered collections of unique items (e.g., {1, 2, 3}).dict
): Collections of key-value pairs (e.g., {"name": "Maaz", "age": 21}).