Keywords: Python has a set of reserved words called keywords which have special meanings. Python has a total of 33 keywords compared to around 50 keywords in Java. The Python keywords are:
and | assert | class | continue |
break | def | del | elif |
else | except | finally | for |
from | global | if | import |
lambda | nonlocal | not | or |
pass | raise | return | try |
while | with | yield |
False | None | True | |
as | is |
So in summary, Python has 33 keywords which are categorized into control flow statements, class and object keywords, data type keywords and other keywords. The keywords have a specific purpose and functionality in the Python language.
Statements: Python programs consist of statements. Some important types of statements in Python are:
- Simple statements: Assign a value to a variable.
a = 5
- Block statements: Grouped within colon(:) and indentation.
if a > 5:
print("a is greater than 5")
- Indentation: Block statements use indentation to group statements. Spaces or tabs can be used for indentation but mixing tabs and spaces is not recommended.
if a > 5:
print("a is greater than 5")
print(a)
So in summary, Python programs consist of keywords, simple statements, block statements and proper indentation which makes Python code very readable.
A Python script is simply a Python program saved in a .py file. It can be executed like any other program. A Python script usually starts with #! comment. #! is known as shebang. It tells the operating system what interpreter to use to execute the file.
The shebang line at the top of a Python script tells the operating system to execute the script with the Python interpreter. The shebang line for Python scripts is:
#! /usr/bin/python
or
#! /usr/bin/env python
This tells the OS to run the script using the Python interpreter located at /usr/bin/python or /usr/bin/env python.
So in summary:
#! is called shebang
It specifies the interpreter to use to execute the file
The shebang line for Python is #!/usr/bin/python or #!/usr/bin/env python
This tells the OS to use the Python interpreter to run the script
Files with .py extension containing Python code are called Python scripts
So the shebang line, along with the .py extension, makes a simple text file a executable Python script.
Later we will learn that scripts can be connected together to create a large application in Python. This is an advanced concept we will investigate at the end of this course. For now, we must stay focus learning all the keywords and statements we can use to create a script.
Disclaim: I have used AI (Rix) to create this article. Only the last paragraph belong to me. If you find errors, comment below and blame Rix for it. Learn and prosper ๐