# Lambda functions

Using lambda functions we can create functions in runtime, and use them as we go. Here’s an example of lambda function and how it is used to filter the array. filter() function takes the lambda function as the first parameter, passes items of the list sequentially and filters the list to retain the items which pass the filtration function passed as first argument.

# will assign a range of numbers from 0 to 15 to numbers list numbers = range(16)

print numbers

# filtering only numbers which can be divided by 3

filtered_numbers = filter(lambda x: x % 3 == 0, numbers)

# output: [0, 3, 6, 9, 12, 15]

print filtered_numbers

# Bitwise Operators

Bitwise operators directly manipulate bits. Python provides certain set of operatos. Example:

print 5 » 4 # Right Shift print 5 « 1 # Left Shift print 8 & 5 # Bitwise AND print 9 | 4 # Bitwise OR print 12 ^ 42 # Bitwise XOR print ~88 # Bitwise NOT

# output: -89

### Base 2 number system

# convert binary to decimal print 0b1 # output 1 print 0b10 # output :2 print 0b11 # output: 3 print 0b10 + 0b01 # output: 3

print oct(15)

# output: 0xe

print oct(14)

### Converting string to number

# converting string from base 10 number

print int(“10”)

# output: 2

print int(“10”, 2)

# output: 204

print int(“cc”, 16)

# bitwise AND

# output: 0b001

print bin(0b101 & 0b011)

# output: 0b111

print bin(0b101 | 0b011)

# output: 0b110

print bin(0b101 ^ 0b011)

# output: -0b110

print bin(~0b101)

### Check if the bit #5 is on or off

def check_bit5(number): if number & 0b10000 > 0: return ‘on’ else: return ‘off’

# output: on

print check_bit5(0b111101)

# output: off

print check_bit5(0b101101)

### Flipping the nth bit in a number

# will flip the nth bit in the number def flip_bit(number, n): mask = 1 « (n - 1) result = number ^ mask return bin(result)

# output: 0b100100

print flip_bit(0b100000, 3)