Python Keywords

What You'll Learn: In this tutorial, you'll discover various essential keywords used in Python programming. These keywords help you write conditional statements, loops, and handle errors in your programs.

Key Python Keywords

  1. and
    • Description: A logical operator used to combine conditional statements.
    • Example:

      python
      if a > 0 and b > 0:
          print("Both a and b are positive")
      
  2. as
    • Description: Used to create an alias.
    • Example:

      python
      import math as m
      print(m.sqrt(16))  # Output: 4.0
      
  3. assert
    • Description: Used for debugging.
    • Example:

      python
      assert x > 0, "x should be greater than 0"
      
  4. break
    • Description: Used to break out of a loop.
    • Example:

      python
      for i in range(10):
          if i == 5:
              break
          print(i)
      
  5. class
    • Description: Used to define a class.
    • Example:

      python
      class MyClass:
          x = 5
      
  6. continue
    • Description: Used to continue to the next iteration of a loop.
    • Example:

      python
      for i in range(10):
          if i % 2 == 0:
              continue
          print(i)
      
  7. def
    • Description: Used to define a function.
    • Example:

      python
      def my_function():
          print("Hello from a function")
      
  8. del
    • Description: Used to delete an object.
    • Example:

      python
      my_list = [1, 2, 3]
      del my_list[0]
      print(my_list)  # Output: [2, 3]
      
  9. elif
    • Description: Used in conditional statements, same as else if.
    • Example:

      python
      if x < 0:
          print("Negative")
      elif x == 0:
          print("Zero")
      else:
          print("Positive")
      
  10. else
    • Description: Used in conditional statements.
    • Example:

      python
      if x < 0:
          print("Negative")
      else:
          print("Non-negative")
      
  11. except
    • Description: Used with exceptions, to specify what to do when an exception occurs.
    • Example:

      python
      try:
          print(10 / 0)
      except ZeroDivisionError:
          print("You can't divide by zero!")
      
  12. False
    • Description: Boolean value, result of comparison operations.
    • Example:

      python
      print(False)  # Output: False
      
  13. finally
    • Description: Used with exceptions, a block of code that will be executed no matter if there is an exception or not.
    • Example:

      python
      try:
          print(10 / 0)
      except ZeroDivisionError:
          print("You can't divide by zero!")
      finally:
          print("This code runs no matter what")
      
  14. for
    • Description: Used to create a for loop.
    • Example:

      python
      for i in range(5):
          print(i)
      
  15. from
    • Description: Used to import specific parts of a module.
    • Example:

      python
      from math import sqrt
      print(sqrt(16))  # Output: 4.0
      
  16. global
    • Description: Used to declare a global variable.
    • Example:

      python
      def my_function():
          global x
          x = 10
      
  17. if
    • Description: Used to make a conditional statement.
    • Example:

      python
      if x > 0:
          print("Positive")
      
  18. import
    • Description: Used to import a module.
    • Example:

      python
      import math
      print(math.sqrt(16))  # Output: 4.0
      
  19. in
    • Description: Used to check if a value is present in a list, tuple, etc.
    • Example:

      python
      if 3 in [1, 2, 3]:
          print("3 is in the list")
      
  20. is
    • Description: Used to test if two variables are equal.
    • Example:

      python
      a = [1, 2, 3]
      b = a
      print(a is b)  # Output: True
      
  21. lambda
    • Description: Used to create an anonymous function.
    • Example:

      python
      x = lambda a: a + 10
      print(x(5))  # Output: 15
      
  22. None
    • Description: Represents a null value.
    • Example:

      python
      x = None
      print(x)  # Output: None
      
  23. nonlocal
    • Description: Used to declare a non-local variable.
    • Example:

      python
      def outer():
          x = "local"
          def inner():
              nonlocal x
              x = "nonlocal"
              print("inner:", x)
          inner()
          print("outer:", x)
      
  24. not
    • Description: A logical operator.
    • Example:

      python
      print(not True)  # Output: False
      
  25. or
    • Description: A logical operator.
    • Example:

      python
      if a > 0 or b > 0:
          print("At least one is positive")
      
  26. pass
    • Description: A null statement, a statement that will do nothing.
    • Example:

      python
      if x > 0:
          pass
      
  27. raise
    • Description: Used to raise an exception.
    • Example:

      python
      raise ValueError("A custom error message")
      
  28. return
    • Description: Used to exit a function and return a value.
    • Example:

      python
      def add(a, b):
          return a + b
      
  29. True
    • Description: Boolean value, result of comparison operations.
    • Example:

      python
      print(True)  # Output: True
      
  30. try
    • Description: Used to make a try...except statement.
    • Example:

      python
      try:
          print(10 / 0)
      except ZeroDivisionError:
          print("You can't divide by zero!")
      
  31. while
    • Description: Used to create a while loop.
    • Example:

      python
      i = 1
      while i < 6:
          print(i)
          i += 1
      
  32. with
    • Description: Used to simplify exception handling.
    • Example:

      python
      with open('file.txt', 'r') as file:
          content = file.read()
      
  33. yield
    • Description: Used to return a list of values from a generator.
    • Example:

      python
      def generator():
          yield 1
          yield 2
          yield 3
      

Try It Yourself: Fun Exercises

  1. Conditional Statements and Loops:
    • Write a Python program that uses if, elif, else, for, and while to control the flow of your program.
  2. Exception Handling:
    • Experiment with try, except, finally, and raise to handle and raise exceptions in your program.
  3. Function and Class Definitions:
    • Use def to define functions and class to define classes in a small project.

Summary:

In this Python tutorial, we explored various essential keywords used in Python programming. Understanding these keywords helps you write conditional statements, loops, handle errors, and more. Keep experimenting and have fun with Python!