Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
0 views

Unlock the Power of Python Keywords and Functions!

The document provides a comprehensive overview of Python keywords, categorized into various types such as control flow, data types, class and function definitions, and more. It includes a list of keywords along with their basic purposes and advanced usage examples. Additionally, it covers built-in functions and methods related to copying, sorting, reversing, removing duplicates, and merging lists.

Uploaded by

nirmalsuresh90
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Unlock the Power of Python Keywords and Functions!

The document provides a comprehensive overview of Python keywords, categorized into various types such as control flow, data types, class and function definitions, and more. It includes a list of keywords along with their basic purposes and advanced usage examples. Additionally, it covers built-in functions and methods related to copying, sorting, reversing, removing duplicates, and merging lists.

Uploaded by

nirmalsuresh90
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Basic To Advance

Level: Types Of Python


Keywords

Python keywords are reserved


words in Python that have special
meanings and are used to define
the syntax and structure of the
language. They cannot be used as
identifiers (variable names,
function names, etc.). Here is a list
of Python keywords as of Python
3.10 and their basic purposes:
Types Of Python Keywords

01. Control Flow Keywords


1. if - Conditional branching.
2. else - Alternative branch in a condition.
3. elif - Additional conditional branch.
4. for - Looping over an iterable.
5. while - Looping while a condition is true.
6. break - Exit the nearest enclosing loop.
7. continue - Skip the rest of the loop body and proceed with the next
iteration.
8. pass - Do nothing (a placeholder statement).
9. return - Exit a function and optionally return a value.
10. yield - Produce a value in a generator function.
11. try - Start a block of code to catch exceptions.
12. except - Handle exceptions.
13. finally - Block of code that always executes, even if an exception
occurs.
14. raise - Raise an exception manually.
15. assert - Assert that a condition is true (used for debugging).

02. Data Types and Definitions


1. True - Boolean value representing true.
2. False - Boolean value representing false.
3. None - Represents the absence of a value or a null value.
Types Of Python Keywords

03. Class and Function Keywords


1. class - Define a class.
2. def - Define a function or method.
3. lambda - Define an anonymous function.
4. async - Declare asynchronous functions or coroutines.
5. await - Wait for the result of an asynchronous operation.

04. Variable Scope and Namespace


1. global - Declare a global variable.
2. nonlocal - Refer to a variable in the nearest enclosing scope (not
global).

05. Logical and Membership Operators


1. and - Logical AND operator.
2. or - Logical OR operator.
3. not - Logical NOT operator.
4. in - Check membership in a collection.
5. is - Test for object identity.

06. Importing and Modules


1. import - Import a module.
2. from - Import specific parts of a module.
Types Of Python Keywords

07. Object-Oriented Programming


1. self - Refers to the current instance of a class (not a keyword but used
conventionally).
2. super - Access methods or attributes of a parent class.

08. Miscellaneous
1. del - Delete an object.
2. with - Context manager for resource management (e.g., file handling).
3. as - Alias for modules or within with statements.
4. print - Function for output (in Python 3.x, it's not a keyword but a
built-in function).
5. exec - Execute dynamically created Python code (rarely used in
Python 3).

09. Special Keywords


1. name - Special attribute to determine the name of the module being
executed.
2. match - Used for structural pattern matching.
3. case - Specifies patterns in match statements (introduced in Python
3.10).
Types Of Python Keywords

10. Control Flow Keywords


if, elif, else
Advanced usage includes complex conditional structures or one-
liners:

try, except, finally, raise, assert


Advanced error handling with custom exceptions and chaining.
Nested exception handling:
Types Of Python Keywords

11. Generators and Coroutines


yield and yield from
Creating generators that lazily produce items:

Delegating to another generator with yield from:

async and await


Writing asynchronous code with asyncio:
Types Of Python Keywords

12. Object-Oriented Programming


class and def
Metaclasses for controlling class creation:

super
Advanced super usage in multiple inheritances:
Types Of Python Keywords

13. Functional Programming


lambda
Combining with filter, map, and reduce:

14. Context Managers


with and as
Custom context managers with __enter__ and __exit__:
Types Of Python Keywords

15. Built-in functions, methods, and modules


Copying:
1. list.copy() - Creates a shallow copy of the list.
2. Slicing - Copies all elements using slicing.
3. copy.copy - Creates a shallow copy of any object.
4. copy.deepcopy - Creates a deep copy, including nested objects.

Sorting:
1. sorted() - Returns a new sorted list without modifying the original.
2. .sort() - Sorts the list in-place (modifies the original).
3. sorted() with key - Sorts using a custom key.
4. Sort in descending order - Use reverse=True with sorted().

Reversing:
1. reversed() - Returns an iterator for reversing a list.
2. Slicing - Reverse a list using slicing.

Removing Duplicates:
1. set() - Removes duplicates but doesn't preserve order.
2. OrderedDict.fromkeys() - Removes duplicates while maintaining
order.

Merging:
1. Merge lists - Combine two lists using +.
2. Extend lists - Add elements of one list to another.
Was it Like

helpful? Comment

follow for more!


Share

Chetan Shidling
@chetan_shidling_ Save

www.cselectricalandelectronics.com

You might also like