Skip to main content
Practice

Reserved Words with Special Meaning, Keywords

When you start learning programming, you will often encounter the term Keyword. What exactly is a keyword?

A keyword is a predefined word in a programming language that holds a special meaning.

These words are designed to perform specific actions in Python and cannot be used as names for variables or functions.


What are examples of keywords?

For instance, in Python, the word if is used as a keyword to define a conditional statement that executes code based on a specific condition.

Using this keyword, you can instruct the computer to "execute the following code if the condition is met."

Example of using if keyword
# Compare a and b, and print "a is greater than b" if a is greater than b
if a > b:
print("a is greater than b")

Note: Here, a statement refers to a single instruction. A statement can consist of a single line or multiple lines.


What are the key keywords in Python?

As of Python version 3.10, there are about 30-40 keywords in use.

Among these, the major keywords that are frequently used in programming include the following.


Keywords for Conditions

Conditional statements are used to evaluate whether a specific condition is true or false, and are composed of the keywords if, elif, and else.

if is executed if the condition is true, elif (else if) is executed if the previous conditions are false, and else is executed in all other cases.

At the end of the line where these keywords are used, a colon (:) is added to indicate the start of the code block that implements the conditional statement.

if, elif, else keywords
a = 3
b = 3

if a > b: # If a is greater than b
print("a is greater than b")
elif a == b: # If a and b are equal
print("a and b are equal")
else: # In all other cases
print("a is less than b")

In the code above, since a and b are equal, a and b are equal will be printed.


Keywords for Looping

Loops are used to repeatedly execute code as long as a specific condition is true and are composed of the keywords for and while.

At the end of the line where these keywords are used, a colon (:) is added to indicate the start of the code block that implements the loop.

You can create loops using for and while keywords.

for loop
# range(5) generates numbers from 0 to 4
for i in range(5):
# Print numbers from 0 to 4
print(i)
while loop
count = 0

# Repeat until count is less than 5
while count < 5:
print(count)
# Increment count by 1
count += 1

Functions are used to perform specific tasks and return results, enhancing code reusability.

Functions are defined using the def keyword, and results are returned using the return keyword.

At the end of the line where the def keyword is used, a colon (:) is added to indicate the start of the code block that implements the function.

Function definition
# Function that returns the value of a plus 1
def add(a):
return a + 1

In the above add function definition, the function returns the value of the input (a) plus 1.

The variable used inside the function to perform its logic, such as a, is called a parameter.

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.