Skip to content

Latest commit

 

History

History
57 lines (29 loc) · 3.12 KB

File metadata and controls

57 lines (29 loc) · 3.12 KB

Keywords in Python:

Keywords are reserved words in Python that have predefined meanings and cannot be used as variable names or identifiers. These words are used to define the structure and logic of the program. They are an integral part of the Python language and are case-sensitive, which means you must use them exactly as specified.

Here are some important Python keywords:

  1. and: It is a logical operator that returns True if both operands are true.

  2. or: It is a logical operator that returns True if at least one of the operands is true.

  3. not: It is a logical operator that returns the opposite of the operand's truth value.

  4. if: It is used to start a conditional statement and is followed by a condition that determines whether the code block is executed.

  5. else: It is used in conjunction with if to define an alternative code block to execute when the if condition is False.

  6. elif: Short for "else if," it is used to check additional conditions after an if statement and is used in combination with if and else.

  7. while: It is used to create a loop that repeatedly executes a block of code as long as a specified condition is true.

  8. for: It is used to create a loop that iterates over a sequence (such as a list, tuple, or string) and executes a block of code for each item in the sequence.

  9. in: Used with for, it checks if a value is present in a sequence.

  10. try: It is the beginning of a block of code that is subject to exception handling. It is followed by except to catch and handle exceptions.

  11. except: Used with try, it defines a block of code to execute when an exception is raised in the corresponding try block.

  12. finally: Used with try, it defines a block of code that is always executed, whether an exception is raised or not.

  13. def: It is used to define a function in Python.

  14. return: It is used within a function to specify the value that the function should return.

  15. class: It is used to define a class, which is a blueprint for creating objects in object-oriented programming.

  16. import: It is used to import modules or libraries to access their functions, classes, or variables.

  17. from: Used with import to specify which specific components from a module should be imported.

  18. as: Used with import to create an alias for a module, making it easier to reference in the code.

  19. True: It represents a boolean value for "true."

  20. False: It represents a boolean value for "false."

  21. None: It represents a special null value or absence of value.

  22. is: It is used for identity comparison, checking if two variables refer to the same object in memory.

  23. lambda: It is used to create small, anonymous functions (lambda functions).

  24. with: It is used for context management, ensuring that certain operations are performed before and after a block of code.

  25. global: It is used to declare a global variable within a function's scope.

  26. nonlocal: It is used to declare a variable as nonlocal, which allows modifying a variable in an enclosing (but non-global) scope.