+91 81900 98289

Python Syntax

This Python Syntax lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.

Python uses indentation, not braces, to show which lines belong together. Four spaces is the usual indent.

A colon ends the line that starts a block, such as a function or a condition. The next lines in that block sit further to the right.

Example 1: Start here

Mixing tabs and spaces in one file is a common syntax error.

name = "Meera" # text if name: # run the indented lines only when this is true print("Hello", name) # a comma prints each value, with a space between

Example 2: Indent an if

The print line sits four spaces to the right because it belongs to the if.

city = "Chennai" # text if city: # run the indented lines only when this is true print(city)

Example 3: Two lines in one block

Both indented lines run when the test is true.

score = 82 # a whole number if score >= 80: # run the indented lines only when this is true print("Pass") print(score)

Example 4: A function block

The colon starts the block. The call greet() is not indented, so it runs after the function is defined.

def greet(): # define the function; it runs when you call it print("Hello") greet() # call the function
Mixing tabs and spaces in one file is a common syntax error.
Call Now 81900 98289