0% found this document useful (0 votes)
11 views3 pages

Python Keywords Usage-1

This document serves as a usage guide for Python keywords, providing explanations and examples for each keyword's application in programming. It categorizes keywords into various use cases such as control flow, functions, error handling, and more. The guide is aimed at helping students understand the basic structure and logic of Python programming.

Uploaded by

aapk57622
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Python Keywords Usage-1

This document serves as a usage guide for Python keywords, providing explanations and examples for each keyword's application in programming. It categorizes keywords into various use cases such as control flow, functions, error handling, and more. The guide is aimed at helping students understand the basic structure and logic of Python programming.

Uploaded by

aapk57622
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python Keywords: Usage Guide

This document provides simple explanations for where and when each Python keyword is
used. It is a helpful guide for students to understand the basic structure and logic of
Python programming.

Keyword Where/When to Use


False Used to represent a false boolean value, often in
conditions.
await Used in async functions to wait for a coroutine to
complete.
else Used after if/for/while blocks to define alternate
behavior.
import Used to import external Python modules or
libraries.
pass Used as a placeholder where code will be written
later.
None Represents a null or no value; used as default
return.
break Exits a loop immediately.
except Handles exceptions that occur in try blocks.
in Checks membership or iterates over sequences.
raise Manually raises an exception.
True Represents a true boolean value.
class Used to define a new user-defined class.
finally Executes code after try/except blocks, regardless of
outcome.
is Tests object identity, not just equality.
return Used to return values from a function.
and Logical AND, used to combine conditions.
continue Skips current iteration and continues loop.
for Loop that iterates over a sequence.
lambda Defines an anonymous function in one line.
try Starts a block to test for errors.
as Used to create aliases for modules or exceptions.
def Defines a function.
from Imports specific items from a module.
nonlocal Refers to variables in the nearest enclosing scope.
while Loop that runs as long as a condition is true.
assert Tests if a condition is true for debugging.
del Deletes objects or elements.
global Declares a global variable inside a function.
not Logical NOT; inverts a boolean expression.
with Simplifies exception handling in resource
management.
async Declares a function as asynchronous.
elif Else if, used in conditional logic.
if Starts a conditional branch.
or Logical OR; true if at least one condition is true.
yield Returns a value from a generator and saves state.

Summary Use Cases:

 Control Flow → if, else, elif, while, for, break, continue, pass
 Functions → def, return, yield, lambda
 Error Handling → try, except, finally, raise, assert
 Modules → import, from, as
 Data/Logic → True, False, None, and, or, not, is, in
 OOP → class, global, nonlocal
 Async Programming → async, await
 File/Resource Handling → with

Below are usage of Keywords with example are as follow:

Keyword Example
False is_raining = False
await await [Link](1)(used in async functions)
else if x > 0: print("Positive") else: print("Non-positive")
import import math
pass deffunc(): pass(used as a placeholder)
None value = None
break for i in range(5): if i == 3: break
except try: x=1/0 except ZeroDivisionError: print("Cannot divide by 0")
in if 'a' in 'apple': print("Yes")
raise raise ValueError("Invalid input")
True is_hot = True
class class Dog: pass
finally try: x=1 finally: print("Always runs")
is x = None; if x is None: print("None")
return def add(): return 5 + 3
Keyword Example
and if x > 0 and x < 10: print("Valid")
continue for i in range(5): if i == 2: continue
for for i in range(3): print(i)
lambda square = lambda x: x*x
try try: print(5/0) except: print("Error")
as import math as m
def def greet(): print("Hello")
from from math import sqrt
nonlocal def outer(): x = "out" def inner(): nonlocal x; x = "in"
while while x < 5: x += 1
assert assert 2 + 2 == 4
del a = [1,2]; del a[0]
global global x; x = 10
not if not x: print("False")
with with open("[Link]") as f: print([Link]())
async asyncdef fetch(): pass
elif if x == 1: print("One") elif x == 2: print("Two")
if if x == 5: print("Yes")
or if x < 0 or x > 10: print("Out of range")
yield def gen(): yield 1; yield 2

You might also like