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
filtered_numbers = filter(lambda x: x % 3 == 0, numbers)
print filtered_numbers
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
# convert binary to decimal print 0b1 # output 1 print 0b10 # output :2 print 0b11 # output: 3 print 0b10 + 0b01 # output: 3
print oct(15)
print oct(14)
# converting string from base 10 number
print int(“10”)
print int(“10”, 2)
print int(“cc”, 16)
# bitwise AND
print bin(0b101 & 0b011)
print bin(0b101 | 0b011)
print bin(0b101 ^ 0b011)
print bin(~0b101)
def check_bit5(number): if number & 0b10000 > 0: return ‘on’ else: return ‘off’
print check_bit5(0b111101)
print check_bit5(0b101101)
# will flip the nth bit in the number def flip_bit(number, n): mask = 1 « (n - 1) result = number ^ mask return bin(result)
print flip_bit(0b100000, 3)