Keywords
This list contains reserved keywords in Python. These are special words that have predefined meanings in the language's syntax and structure. You cannot use these keywords as variable names, function names, or identifiers because they are part of the language's core.
Here's a breakdown of what they represent:
Boolean Values:
True
: Represents a true condition.
False
: Represents a false condition.
Null Value:
None
: Represents the absence of a value or a null-like object.
Control Flow Statements:
if
,elif
,else
: Used for conditional statements.
try
,except
,finally
: Used for exception handling.
try:
x > 3
except:
print("Something went wrong")
else:
print("Nothing went wrong")
finally:
print("The try...except block is finished")
for
,while
: Looping structures.
break
: Exits the closest enclosing loop.
continue
: Skips the rest of the code in the current loop iteration.
i = 0
while i < 9:
i += 1
if i == 3:
continue
print(i)
pass
: Placeholder statement (does nothing).
Functions and Classes:
def
: Defines a function.
return
: Returns a value from a function.
yield
: Used with generators to return a value without terminating the function.
def myFunc():
yield "Hello"
yield 51
yield "Good Bye"
x = myFunc()
for z in x:
print(z)
class
: Defines a class.
class Person:
name = "John"
age = 36
pl = Person()
print(pl.name)
lambda
: Creates anonymous functions (functions without a name).
x = lambda a, b : a * b
print(x(5, 2))
Exception Handling:
raise
: Raises an exception.
x = "hello"
if not type(x) is int:
raise TypeError("Only integers are allowed")
assert
: Used to check if a condition is met, raising an exception if it’s not.
x = "welcome"
#if condition returns False, AssertionError is raised:
assert x != "hello", "x should be 'hello'"
Asynchronous Programming:
async
: Defines a coroutine function.
await
: Pauses the execution of a coroutine until the awaited task is completed.
Logical Operators:
and
,or
,not
: Used for logical operations.
Membership and Identity Operators:
in
: Checks if a value exists within an iterable (like a list or a string).
is
: Checks if two objects refer to the same instance.
Scope and Namespace Management:
global
: Declares a global variable inside a function.
def myfunction():
global x
x = "hello"
#execute the function:
myfunction()
#x should now be global, and accessible in the global scope.
print(x)
nonlocal
: Refers to a variable in the nearest enclosing scope that’s not global.
def foo():
name = "geek" # Our local variable
def bar():
nonlocal name # Reference name in the upper scope
name = 'GeeksForGeeks' # Overwrite this variable
print(name)
# Calling inner function
bar()
# Printing local variable
print(name)
foo()
Modules and Imports:
import
: Imports a module.
from
: Imports specific parts of a module.
as
: Creates an alias for a module or assigns an exception to a variable.
Context Managers:
with
: Used for resource management (e.g., opening files).
Object Management:
del
: Deletes an object.
x = ["apple", "banana", "cherry"]
del x[0]
print(x)
These keywords are essential to Python's syntax. If you attempt to use them for anything other than their intended purpose, Python will throw a syntax error.
Last updated on November 2, 2024