Learn the Basic Syntax for Python Programming

Indentation in Python
In python programming, indentation is the spaces at the beginning (left-hand side) of a line of code.
For Example:
if (1 + 1) > 5:
print ("Yes")
else:
print ("No")
The space before the print()
statement in the code block above is the indentation.
Indentations are important in python programming syntax. It serves as a logic separator used to indicate blocks of code for class and function definitions or flow of program logic.
Unlike other programming languages that use indentation for code readability, the indentation in python serves a similar function as the curly braces ({}) in programming languages like PHP.
Python does not use braces({}). As a result, it is enforced where it's needed. If omitted, the python program may produce a syntax error.
For Example, the code below will cause a syntax error due to no indentation:
if (1 + 1) > 5:
print ("Yes")
else:
print ("No")
The number of spaces in the indentation is up to the developer, however, it should be at least one space, and all statements within a code block must be indented using the same number of spaces.
The popular convention in python programming is to use four spaces for each line of code. We will follow that convention throughout this tutorial series.
For Example, the code below will produce an error due to inconsistent indentation:
if (1 + 1) > 5:
print ("Yes")
else:
print ("No")
The first print()
statement is indented with two spaces while the second print()
statement is indented with four spaces leading to a syntax error.
Identifiers in Python Programming
Identifiers in python programming are naming paradigms used to identify a variable, function, class, module, or object.
An identifier can begin with any uppercase letters (A to Z), lowercase letters (a to z), or an underscore (_). It may be followed by zero or more uppercase and lowercase letters, underscores, and digits (0 to 9).
The use of characters and symbols such as ., @, $, %, etc. in identifiers are not permitted in python programming and will cause a syntax error.
Also, names of identifiers are case-sensitive in the python programming language. As a result, Sikademy and sikademy will be seen as two different identifiers in Python.
Here are some conventions to follow when naming Python identifiers:
Class names must begin with an uppercase letter. Every letter of a constant must be in uppercase. All other identifiers begin with a lowercase letter.
An identifier that begins with a single underscore signifies a private identifier e.g
_ses
.An identifier that begins with two underscores signifies a very private identifier e.g
__user
.An identifier that begins with two underscores and ends with two underscores signifies a language-defined special name in python programming e.g
__main__
.
Reserved Words in Python Programming
Reserved words are words that cannot be used as names of identifiers except combined with other words.
The table below shows the various reserved words in python programming:
and | as | assert | break | class |
continue | def | del | elif | else |
except | exec | finally | for | from |
global | if | import | in | is |
lambda | not | or | pass | |
raise | return | try | while | with |
yield |
Quotations in Python
You can use single ('), double (") and triple (''' or """) quotes to enclose stringsin python programing as long as the same quote starts and ends the string.
For example:
single_quoted_sentence = 'Welcome to Sikademy'
double_quoted_sentence = "Welcome to Sikademy"
triple_quoted_sentence = """Welcome to Sikademy"""
Words containing triple quotes can be broken into multiple lines for use in longer strings such as paragraphs.
triple_quoted_sentence = """Welcome
to
Sikademy"""
Using One Statement in Multiple Lines
Sometimes you may find yourself writing a long single-line statement in Python.
Python allows the use of the line continuation character (\) to denote that the statement should continue on another line rather than the same.
Example:
total_salary = january_salary + february_salary + \
march_salary + april_salary + \
may_salary + june_salary + \
july_salary + august_salary + \
september_salary + october_salary + \
november_salary + december_salary
Statements within [], {}, or () brackets do not need the line continuation character.
Example:
total_salary = [january_salary + february_salary + march_salary + april_salary + may_salary + june_salary + july_salary + august_salary + september_salary + october_salary + november_salary + december_salary]
Using Multiple Statements in One Line
The use of a semicolon (;) allows you to use multiple statements on a single line as long as none of the statements begin a new code block.
import sys; name = 'Archangel'; sys.stdout.write(x + '\n')
Wrap Off
If you run into errors or unable to complete this tutorial, feel free to contact us anytime, and we will instantly resolve it. You can also request clarification, download this tutorial as a pdf, or report bugs using the buttons below.