Sitemap
Hema Kalyan Murapaka

Hema Kalyan Murapaka

A Data Scientist and Machine Learning Enthusiast

Keywords in Python — Complete Guide

13 min readJul 11, 2023

Hey Python enthusiasts, in this blog, we will explore each keyword in detail, unraveling its meaning and significance. From conditional statements and loops to functions, classes, and exception handling, we will delve into the functionalities offered by these keywords. With practical examples and concise explanations, you’ll gain a solid understanding of how to leverage these keywords to build robust and efficient Python applications.

Press enter or click to view image in full size

Python Programming Language, known for its user-friendly and easy-to-read syntax, relies on a set of essential keywords that form the foundation of the language. In this blog, we will unravel the meaning and significance of these keywords, uncovering their role in shaping Python programs. Whether you’re a beginner or a seasoned developer, understanding these keywords will enable you to write more effective and organized Python code.

Let's understand what is meant by a Keyword. A keyword is a reserved word that cannot be used as an ordinary identifier. They are used to define the syntax and structure of the Python programming language.

Generally, keywords are immutable. This means their meaning and definition can't be altered, it has to be conveyed the same, also they are Case-Sensitive which means the uppercase and lowercase letters are treated as distinct. For example, True is a valid keyword but not true . As of Python 3.11, there are a total of 35 keywords. Would you like to explore a code snippet that reveals the complete list of keywords available in your Python installation?

Code snippet which displays all keywords

The above code snippet gives the output as this: [‘False’, ‘None’, ‘True’, ‘and’, ‘as’, ‘assert’, ‘async’, ‘await’, ‘break’, ‘class’, ‘continue’, ‘def’, ‘del’, ‘elif’, ‘else’, ‘except’, ‘finally’, ‘for’, ‘from’, ‘global’, ‘if’, ‘import’, ‘in’, ‘is’, ‘lambda’, ‘nonlocal’, ‘not’, ‘or’, ‘pass’, ‘raise’, ‘return’, ‘try’, ‘while’, ‘with’, ‘yield’]. Let's know about the functionality of every keyword.

  1. and: A logical operator used for combining two or more conditions in an if statement or a loop. It returns true if all the conditions are true.

The code snippet takes an integer as user input and checks the conditions num%2==0 and num>0. If the number satisfies both conditions, it prints “Even”. Otherwise, it prints “Odd”.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

2. as: It is used in the import statement to create an alias or a shorter name for a module, making it easier to reference.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

3. assert: It is used to test if a given condition is true, the program/code runs as usual but if the condition is false, an Assertion error is raised, which can be used for debugging and testing purposes.

The provided code snippet takes an integer input x from the user and checks if it is greater than 0. If the condition is false, an exception is raised with the error message "x should be greater than 0".

Press enter or click to view image in full size

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — 4. break: It is used to terminate the execution of a loop (such as for or while) prematurely. When encountered, it immediately exits the loop, and the program continues with the next statement after the loop.

In the given code snippet, the loop will print the numbers 1 and 2 since the break statement is encountered when num equals 3. After breaking out of the loop, the code continues execution with the statement print(“Loop Ended”). It will give output like this

Press enter or click to view image in full size

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

5. class: It is used to define a new class in Python which is a blueprint for creating objects that encapsulate data and methods.

It is a core component of Python’s object-oriented paradigm, enabling the creation of structured and maintainable code. It promotes code reusability, encapsulation, and modularity, making it easier to build complex applications and systems

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

6. continue: It is used to skip the current iteration of a loop and go to the next iteration.

The code starts by defining a for loop that iterates from 1 to 10. Then, it uses an if statement to check if the current value of i is an odd number. If it is, the continue statement is executed, which skips the current iteration of the loop and goes to the next one. If the value of i is even, the print statement is executed. The output of the code is a list of even numbers from 1 to 10.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

7. def: It is used to define a new function in Python. It is followed by the function name, parentheses containing optional parameters, and a colon. The indented block of code following the colon defines the function's body.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

8. del: It is used to delete an object or a variable from memory, freeing up resources.

The above code snippet defines a list of numbers and assigns it the values 1, 2, 3, 4, and 5. Then, it prints the list and the next line uses the del keyword to delete the element at index 2 in the list. This means that the value 3 is no longer stored in the list which displays the output like this

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

9. elif: It is short for "else if" and is used in an if statement to check for additional conditions if the previous conditions were false.

10. else: It is used in an if statement to define a block of code that should be executed if none of the previous conditions were true and is also used in exception handling. The code snippet would be a great example for this one also.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

11. except: It is used in a try-except block to catch and handle exceptions. It is followed by the type of exception to catch and an indented block of code that specifies the exception handling.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

12. finally: It is used in a try-except block to specify a block of code that should be executed regardless of whether an exception was raised or not.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

13. for: It is used to create a loop that iterates over a sequence, such as a list, tuple, or string. It assigns each element of the sequence to a variable and executes the indented block of code for each iteration.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

14. from: It is used in the import statement to specify the module or package from which you want to import specific objects or functions.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

15. global: It is used inside a function to indicate that a variable defined within the function should be treated as a global variable, accessible outside the function.

In this code snippet, we have a global variable named count initialized with a value of 0. Inside the increment function, the global keyword is used to indicate that the variable count is a global variable rather than a local variable specific to the function's scope. This allows us to modify the value of the global variable from within the function. After calling the function, we print the value of the count, in this case, it would print 1.

If you try to execute the code without using the global keyword in the function, you will encounter an error. Specifically, you will encounter an unboundLocalError.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Get Hema Kalyan Murapaka’s stories in your inbox

Join Medium for free to get updates from this writer.

16. if: It is used to define a conditional statement in Python. It evaluates a condition and executes a block of code if the condition is true.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

17. import: It is used to import a module or a specific object from a module into the current Python script, allowing you to use the functions, classes, or variables defined in the module.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

18. in: is used to test if a value is present in a sequence, such as a list, tuple, or string. It returns True if the value is found and False otherwise.

19. not: It is a Logical operator used to negate a condition. It returns True if the condition is false and False if the condition is true. The following code snippet would be a great example of this both keywords

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

20. is: It is used to test if two objects are referring to the same memory location. It returns True if the objects are the same and False otherwise.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

21. lambda: It is used to define anonymous functions, also known as lambda functions. These functions can take any number of arguments but can only have a single expression in their body.

The structure of a lambda function in Python follows the syntax:

lambda arguments: expression

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

22. or: It is a logical operator used for combining two or more conditions in an if statement or a loop. It returns True if at least one of the conditions is true.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

23. pass: Itis a placeholder statement used when a statement is required syntactically but you don't want to execute any code. It is often used as a placeholder for code that will be implemented later.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

24. return: It is used to exit a function and return a value. It can be used to pass a value back to the caller of the function.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

25. try: It is used to start a try-except block, which is used for exception handling. It is followed by a block of code where exceptions might occur.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

26. while: It is used to create a loop that executes a block of code repeatedly as long as a given condition is true.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

27. with: It is used to define a block of code within a context manager. It ensures that the necessary setup and cleanup actions are performed, even if exceptions occur within the block.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

28. yield: It is used in generator functions to define a sequence of values to be generated. It allows the function to produce a series of values one at a time, rather than returning a single value. When a yield statement is encountered, the current value is returned, and the function's state is saved. The function can be resumed later to continue execution from where it left off.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

29. False: It is a boolean value that represents false or the absence of truth. It is one of the two possible boolean values in Python, along with True. It is often used in conditional statements and logical operations.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

30. True: It is also a boolean value that represents true or the presence of truth. It is the counterpart of False and is used in conditional statements and logical operations.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

31. None: It is a special value in Python that represents the absence of a value or the null value. It is often used to indicate the absence of a return value or to initialize variables that may later hold a value.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

32. async: It is a keyword used to define an asynchronous function. An asynchronous function can run concurrently with other functions and allows for non-blocking I/O operations. It is used in combination with await to control the execution flow.

33. await: It is used inside an asynchronous function to wait for the completion of another asynchronous task. It allows the function to pause its execution until the awaited task is complete while allowing other tasks to continue running. The below code will be a great example of these keywords.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

34. nonlocal: It is used within nested functions to indicate that a variable is not local to the current function, but rather to an enclosing function. It allows accessing and modifying variables in the nearest enclosing scope that are neither global nor local to the current function.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

35. raise: It is used to raise an exception explicitly in Python. It is followed by an exception class or instance that represents the type of error or exceptional condition encountered during program execution.

Understanding and utilizing these keywords effectively allows developers to write efficient, readable, and maintainable Python code. By leveraging the functionalities provided by Python keywords, you can express your intentions clearly and concisely in your programs.

So, make sure to familiarize yourself with these keywords and use them appropriately in your Python projects to unleash the full potential of the language. Happy coding!

If you learned something new or enjoyed reading this article, please clap it up 👏 and share it so that others will see it. Feel free to leave a comment too.

Contact: LinkedIn

Visit my Portfolio

--

--

Hema Kalyan Murapaka
Hema Kalyan Murapaka

Written by Hema Kalyan Murapaka

A Data Scientist and Machine Learning Enthusiast