Featured Post

Python map() and lambda() Use Cases and Examples

Image
 In Python, map() and lambda functions are often used together for functional programming. Here are some examples to illustrate how they work. Python map and lambda top use cases 1. Using map() with lambda The map() function applies a given function to all items in an iterable (like a list) and returns a map object (which can be converted to a list). Example: Doubling Numbers numbers = [ 1 , 2 , 3 , 4 , 5 ] doubled = list ( map ( lambda x: x * 2 , numbers)) print (doubled) # Output: [2, 4, 6, 8, 10] 2. Using map() to Convert Data Types Example: Converting Strings to Integers string_numbers = [ "1" , "2" , "3" , "4" , "5" ] integers = list ( map ( lambda x: int (x), string_numbers)) print (integers) # Output: [1, 2, 3, 4, 5] 3. Using map() with Multiple Iterables You can also use map() with more than one iterable. The lambda function can take multiple arguments. Example: Adding Two Lists Element-wise list1 = [ 1 , 2 , 3 ]

How to Work with 'Pointers' in Python

Pointers denote an address (memory location). It has three identities - Name, Value, and Location (Address). Python doesn't support pointers as-is. You need to import 'ctypes' package to work with C Language.

Note: Pointer is popular in C, C++. The called module just uses the value of Pointer (not address).  Below is my detailed post on pointers.  


An article on how to work with pointers


How to work with Pointers

  • To pass a reference(address) to the C interface.
  • You can use C Language in Python by importing 'ctypes.' 

Pointer Notation

1. Value
2. Address
3. Name

Python Pointers


Python doesn't support pointers. C and C++ extensively support pointers. Pointer is nothing but an ADDRESS. It is immutable. That means you can't change the value. Python supports pointers for the purpose to interact with C Language.



Pointers

How to Import 'ctypes'

  1. Import 'ctypes' library for the purpose of working with C language. 
  2. Here's how to import 'ctypes' for windows and Linux.

How to denote Pointers

Here's the way to denote pointers in Python. Check out here Python Pointers.

 
from ctypes import * 
i = c_int(42) 
pi = pointer(i) 


Comments

Popular posts from this blog

How to Fix datetime Import Error in Python Quickly

SQL Query: 3 Methods for Calculating Cumulative SUM

Python placeholder '_' Perfect Way to Use it