Introduction to Basic Python Syntax
Ohidur Rahman Bappy
MAR 22, 2025
Execution Modes
Python can be executed in two primary modes:
- Interactive Mode
- Scripting Mode
Interactive Mode
In interactive mode, you can type python
in the command window and execute Python code line by line.
Scripting Mode
For scripting mode, write your Python code in a file with .py
extension and run it using:
python filename.py
For Linux-based systems, make the file executable to run it directly:
chmod +x filename.py
./filename.py
The print()
Function
The print()
function outputs characters and strings to the standard screen:
print("Hello World") # output: Hello World
print("Hello", "World") # output: Hello World
print("Hello", end="") # no new line
Python Identifiers
Identifiers are names given to variables, functions, classes, modules, and other objects. They:
- Start with a letter (A-Z, a-z) or an underscore (_), followed by letters, underscores, or digits (0-9).
- Cannot include punctuation such as @, $, and %.
- Are case sensitive. For example,
Manpower
andmanpower
are distinct names.
Naming Conventions
- Class names start with an uppercase letter.
- Identifiers starting with a single underscore are private.
- Identifiers with two leading underscores denote a strong private identifier.
- Identifiers ending with two trailing underscores are language-defined special names.
Reserved Words
Python has reserved words that cannot be used as identifiers:
| | | | |-------|--------|-------| |and |exec |not | |as |finally |or | |assert |for |pass | |break |from |print | |class |global |raise | |continue|if |return | |def |import |try | |del |in |while | |elif |is |with | |else |lambda |yield |
Lines and Indentation
Python uses indentation to define blocks of code instead of braces ({}
). The amount of indentation is flexible but must be consistent within a block.
if True:
print("True")
else:
print("False")
Multi-Line Statements
Typically, statements end with a new line. Use the backslash (\
) for line continuation:
total = item_one + \
item_two + \
item_three
In brackets ([]
, {}
, ()
), you don’t need the line continuation character:
days = ['Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday']
Quotation in Python
Python supports single ('
), double ("
), and triple ('''
or """
) quotes for string literals:
word = 'word'
sentence = "This is a sentence."
paragraph = '''This is a paragraph. It is
made up of multiple lines.'''
Comments in Python
Use a hash sign (#
) for comments. Python ignores everything after #
on a line:
#!/usr/bin/python3
# This is a comment
print("Hello, Python!") # This is another comment
For multiple lines, comment each line individually:
# Comment 1
# Comment 2
# Comment 3
Using Blank Lines
Blank lines, possibly with comments, are ignored by Python. In interactive mode, enter an empty physical line to terminate a multiline statement.
Waiting for the User
Use input()
to wait for user input. This example pauses until the Enter key is pressed:
#!/usr/bin/python3
input("\n\nPress the enter key to exit.")
Multiple Statements on a Single Line
Use a semicolon (;
) to separate multiple statements on a single line:
import sys; x = 'foo'; sys.stdout.write(x + '\n')
Multiple Statement Groups as Suites
A suite is a group of statements making up a single code block in Python. Use a colon (:
) to denote the start of suites in compound statements:
if expression:
suite
elif expression:
suite
else:
suite
Command Line Arguments
Python supports command line arguments, accessible with the -h
option:
$ python -h
This command returns usage information and exits.