Structure Keywords in Python : def, class, with, as, pass, lambda

Python def

def keyword is used to declare user defined functions.

def keyword in Python

The code defines a function named fun using the def keyword. When the function is called using fun(), it prints “Inside Function.” This code demonstrates the use of the def keyword to define and call a function in Python.

Python3




def fun():
    print("Inside Function")
 
 
fun()


Output

Inside Function



class in Python

class keyword is used to declare user defined classes.

Class Keyword in Python

This code defines a Python class named Dog with two class attributes, attr1 and attr2, and a method fun that prints these attributes. It creates an object Rodger from the Dog class, accesses the class attributes, and calls the method. When executed, it prints the values of attr1 and `attr2, and the method displays these values, resulting in the output shown.

Python3




class Dog:
    attr1 = "mammal"
    attr2 = "dog"
 
    def fun(self):
        print("I'm a", self.attr1)
        print("I'm a", self.attr2)
 
Rodger = Dog()
print(Rodger.attr1)
Rodger.fun()


Output

mammal
I'm a mammal
I'm a dog



Note: For more information, refer to our Python Classes and Objects Tutorial .

With in Python

with keyword is used to wrap the execution of block of code within methods defined by context manager. This keyword is not used much in day to day programming.

With Keyword in Python

This code demonstrates how to use the with statement to open a file named 'file_path' in write mode ('w'). It writes the text 'hello world !' to the file and automatically handles the opening and closing of the file. The with statement is used for better resource management and ensures that the file is properly closed after the block is executed.

Python3




# using with statement
with open('file_path', 'w') as file:
    file.write('hello world !')


as in Python

as keyword is used to create the alias for the module imported. i.e giving a new name to the imported module. E.g import math as mymath.

as Keyword In Python

This code uses the Python math module, which has been imported with the alias gfg. It calculates and prints the factorial of 5. The math.factorial() function is used to calculate the factorial of a number, and in this case, it calculates the factorial of 5, which is 120.

Python3




import math as gfg
 
print(gfg.factorial(5))


Output

120



pass in Python

pass is the null statement in python. Nothing happens when this is encountered. This is used to prevent indentation errors and used as a placeholder.

Pass Keyword in Python

The code contains a for loop that iterates 10 times with a placeholder statementpass', indicating no specific action is taken within the loop.

Python3




n = 10
for i in range(n):
 
    # pass can be used as placeholder
    # when code is to added later
    pass


Output

 



Lambda in Python

Lambda keyword is used to make inline returning functions with no statements allowed internally. 

Lambda Keyword in Python

The code defines a lambda function g that takes an argument x and returns x cubed. It then calls this lambda function with the argument 7 and prints the result. In this case, it calculates and prints the cube of 7, which is 343.

Python3




# Lambda keyword
g = lambda x: x*x*x
 
print(g(7))


Output

343



Python Keywords

Every language contains words and a set of rules that would make a sentence meaningful. Similarly, in Python programming language, there are a set of predefined words, called Keywords which along with Identifiers will form meaningful sentences when used together. Python keywords cannot be used as the names of variablesfunctions, and classes.

In this article, we will learn about Python keywords and how to use them to perform some tasks.

Similar Reads

Python Keywords

Keywords in Python are reserved words that can not be used as a variable name, function name, or any other identifier....

List of Keywords in Python

...

True, False, None Keyword in Python

...

and, or, not, in, is In Python

True: This keyword is used to represent a boolean true. If a statement is true, “True” is printed. False: This keyword is used to represent a boolean false. If a statement is false, “False” is printed.  None: This is a special constant used to denote a null value or a void. It’s important to remember, 0, any empty container(e.g. empty list) does not compute to None. It is an object of its datatype – NoneType. It is not possible to create multiple None objects and can assign them to variables....

Iteration Keywords – for, while, break, continue in Python

...

Conditional keywords in Python- if, else, elif

Python and Keyword...

Structure Keywords in Python : def, class, with, as, pass, lambda

...

Return Keywords in Python- Return, Yield

for: This keyword is used to control flow and for looping. while: Has a similar working like “for”, used to control flow and for looping. break: “break” is used to control the flow of the loop. The statement is used to break out of the loop and passes the control to the statement following immediately after loop. continue: “continue” is also used to control the flow of code. The keyword skips the current iteration of the loop but does not end the loop....

Import, From in Python

...

Exception Handling Keywords in Python – try, except, raise, finally, and assert

if : It is a control statement for decision making. Truth expression forces control to go in “if” statement block. else : It is a control statement for decision making. False expression forces control to go in “else” statement block. elif : It is a control statement for decision making. It is short for “else if“...

del in Python

...

Global, Nonlocal in Python

Python def...