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
and- Description: A logical operator used to combine conditional statements.
Example:
pythonif a > 0 and b > 0: print("Both a and b are positive")
as- Description: Used to create an alias.
Example:
pythonimport math as m print(m.sqrt(16)) # Output: 4.0
assert- Description: Used for debugging.
Example:
pythonassert x > 0, "x should be greater than 0"
break- Description: Used to break out of a loop.
Example:
pythonfor i in range(10): if i == 5: break print(i)
class- Description: Used to define a class.
Example:
pythonclass MyClass: x = 5
continue- Description: Used to continue to the next iteration of a loop.
Example:
pythonfor i in range(10): if i % 2 == 0: continue print(i)
def- Description: Used to define a function.
Example:
pythondef my_function(): print("Hello from a function")
del- Description: Used to delete an object.
Example:
pythonmy_list = [1, 2, 3] del my_list[0] print(my_list) # Output: [2, 3]
elif- Description: Used in conditional statements, same as
else if. Example:
pythonif x < 0: print("Negative") elif x == 0: print("Zero") else: print("Positive")
- Description: Used in conditional statements, same as
else- Description: Used in conditional statements.
Example:
pythonif x < 0: print("Negative") else: print("Non-negative")
except- Description: Used with exceptions, to specify what to do when an exception occurs.
Example:
pythontry: print(10 / 0) except ZeroDivisionError: print("You can't divide by zero!")
False- Description: Boolean value, result of comparison operations.
Example:
pythonprint(False) # Output: False
finally- Description: Used with exceptions, a block of code that will be executed no matter if there is an exception or not.
Example:
pythontry: print(10 / 0) except ZeroDivisionError: print("You can't divide by zero!") finally: print("This code runs no matter what")
for- Description: Used to create a
forloop. Example:
pythonfor i in range(5): print(i)
- Description: Used to create a
from- Description: Used to import specific parts of a module.
Example:
pythonfrom math import sqrt print(sqrt(16)) # Output: 4.0
global- Description: Used to declare a global variable.
Example:
pythondef my_function(): global x x = 10
if- Description: Used to make a conditional statement.
Example:
pythonif x > 0: print("Positive")
import- Description: Used to import a module.
Example:
pythonimport math print(math.sqrt(16)) # Output: 4.0
in- Description: Used to check if a value is present in a list, tuple, etc.
Example:
pythonif 3 in [1, 2, 3]: print("3 is in the list")
is- Description: Used to test if two variables are equal.
Example:
pythona = [1, 2, 3] b = a print(a is b) # Output: True
lambda- Description: Used to create an anonymous function.
Example:
pythonx = lambda a: a + 10 print(x(5)) # Output: 15
None- Description: Represents a null value.
Example:
pythonx = None print(x) # Output: None
nonlocal- Description: Used to declare a non-local variable.
Example:
pythondef outer(): x = "local" def inner(): nonlocal x x = "nonlocal" print("inner:", x) inner() print("outer:", x)
not- Description: A logical operator.
Example:
pythonprint(not True) # Output: False
or- Description: A logical operator.
Example:
pythonif a > 0 or b > 0: print("At least one is positive")
pass- Description: A null statement, a statement that will do nothing.
Example:
pythonif x > 0: pass
raise- Description: Used to raise an exception.
Example:
pythonraise ValueError("A custom error message")
return- Description: Used to exit a function and return a value.
Example:
pythondef add(a, b): return a + b
True- Description: Boolean value, result of comparison operations.
Example:
pythonprint(True) # Output: True
try- Description: Used to make a
try...exceptstatement. Example:
pythontry: print(10 / 0) except ZeroDivisionError: print("You can't divide by zero!")
- Description: Used to make a
while- Description: Used to create a
whileloop. Example:
pythoni = 1 while i < 6: print(i) i += 1
- Description: Used to create a
with- Description: Used to simplify exception handling.
Example:
pythonwith open('file.txt', 'r') as file: content = file.read()
yield- Description: Used to return a list of values from a generator.
Example:
pythondef generator(): yield 1 yield 2 yield 3
Try It Yourself: Fun Exercises
- Conditional Statements and Loops:
- Write a Python program that uses
if,elif,else,for, andwhileto control the flow of your program.
- Write a Python program that uses
- Exception Handling:
- Experiment with
try,except,finally, andraiseto handle and raise exceptions in your program.
- Experiment with
- Function and Class Definitions:
- Use
defto define functions andclassto define classes in a small project.
- Use
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!