Course Content
What is Python?
Introduction of Python and Its setup
0/2
Control Statement
Control statements are used to control the flow of execution depending upon the specified condition/logic.
0/5
File Handling
File handling is an important component of any application. Python has multiple functions for creating, reading, updating, and deleting files.
0/2
Python
    About Lesson

    Python keywords are reserved words with predefined meanings and purposes in the Python programming language. These words form the syntax and structure of Python programs and cannot be used as identifiers (like variable names, function names, etc.).

    What Are Python Keywords?

    Keywords are case-sensitive and are always written in lowercase. Python 3.11 currently has 36 keywords, though this number may vary with future updates. Examples include if, else, for, while, def, return, etc.

     

    andexecnot
    asfinallyor
    assertforpass
    continueifreturn
    breakfromprint
    classglobalraise
    defimporttry
    classglobalraise
    delinwhile
    elifiswith
    elselambdayield
    except 

     

    You can view all Python keywords in your environment using the following command:

    import keyword
    print(keyword.kwlist)

    Output

    ['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']

     

    Python Keywords with Examples

    False, True

    These represent Boolean values.

    is_active = True
    is_logged_in = False
    print(is_active and not is_logged_in) # Output: True

    None

    Represents the absence of a value.

    def greet():
        pass # No implementation yet
    
    result = greet()
    print(result) # Output: None

     

    and, or, not

    Logical operators.

    x = 10
    y = 20
    
    if x > 5 and y > 15:
        print("Both conditions are True")
    
    if x > 15 or y > 15:
        print("At least one condition is True")
    
    if not (x > 15):
        print("Condition is False")

    as

    Used for creating an alias while importing modules.

    import math as m
    print(m.sqrt(16)) # Output: 4.0

     

    assert

    Used for debugging.

    x = 10
    assert x > 5, "x must be greater than 5"
    assert x < 5, "x must be less than 5" # This will raise an AssertionError

     

    async, await

    Used for asynchronous programming.

    import asyncio
    
    async def main():
        print("Hello")
        await asyncio.sleep(1)
        print("World")
    
    asyncio.run(main())

     

    break, continue

    Control flow keywords in loops.

    for i in range(5):
        if i == 3:
            break
        print(i) # Output: 0, 1, 2
    
    for i in range(5):
        if i == 3:
            continue
        print(i) # Output: 0, 1, 2, 4

     

    class

    Defines a class.

    class Person:
        def __init__(self, name):
            self.name = name
    
    person = Person("Infovistar")
    print(person.name) # Output: Infovistar

     

    def

    Defines a function.

    def add(a, b):
        return a + b
    
    print(add(2, 3)) # Output: 5

     

    if, elif, else

    Conditional statements.

    x = 10
    if x > 15:
        print("Greater than 15")
    elif x == 10:
        print("Equal to 10")
    else:
        print("Less than 10")

     

    for, while

    Loops for iteration.

    # for loop
    for i in range(3):
        print(i) # Output: 0, 1, 2
    
    # while loop
    count = 0
    while count < 3:
        print(count)
        count += 1 # Output: 0, 1, 2

     

    Python keywords form the foundation of any Python program. By understanding and using them effectively, you can write clear and efficient Python code. Make sure to stay updated with changes in the Python language to keep your knowledge fresh.