Tutorialspoint

Binary to decimal in Python

print("*****************************************************")
print(" DECIMAL TO BINARY AND BINARY TO DECIMAL CONVERSION")
print("*****************************************************")
print(" For Decimal to Binary...Press 1.")
print(" For Binary to Decimal... Press 2")
print("*****************************************************")
my_choice=int(input("Enter your choice: "))
if my_choice==1:
   i=1
   s=0
   my_dec=int(input("Enter decimal to be converted: "))
   while my_dec>0:
      rem=int(my_dec%2)
      s=s+(i*rem)
      my_dec=int(my_dec/2)
      i=i*10
      print ("The binary of the given number is ",s,'.')
else:
   my_bin=input ('Enter binary to be converted: ')
   n=len(my_bin)
   res=0
   for i in range(1,n+1):
      res=res+ int(my_bin[i-1])*2**(n-i)
      print ("The decimal of the given binary is ",res,'.')
      print("******************************************************")

Python using contains() function

import operator
my_str1 = 'Python Programming'
my_substr = 'thon'
print(operator.contains(my_str1, my_substr))

Python using the concat method

import operator
list1 = [10, 20, 30, 40]
list2 = [90, 80, 70, 60]
print(operator.concat(list1, list2))

operator functions and arguments in Python

import operator
my_list = list('Python_Programming')
print(my_list)

#Set the item WXYZ in the place 8-11
operator.setitem(my_list, slice(8,12), list('WXYZ'))
print(my_list)

#Get the items in range (5, 10)
item = operator.getitem(my_list, slice(5, 11))
print(item)

#Delete the item from position 6 to 9
operator.delitem(my_list, slice(6, 10))
print(my_list)

Container Operations in Python

import operator
my_list = list('Python_Programming')
print(my_list)

#Set the item X into the position 5 
operator.setitem(my_list, 5, 'X')
print(my_list)

#Get the item from position 9 and print it 
item = operator.getitem(my_list, 9)
print(item)

#Delete the item from position 10 and print the list 
operator.delitem(my_list, 10)
print(my_list)

Bitwise Operations in Python

import operator
a = 12 # 01100 in Binary
b = 9 #  01010 in Binary
print('Bitwise AND: ' + str(operator.and_(a, b)))
print('Bitwise OR: ' + str(operator.or_(a, b)))
print('Bitwise XOR: ' + str(operator.xor(a, b)))
print('Invert: ' + str(operator.invert(a)))

Quine in Python

a='a=%r;print (a%%a)';print (a%a)

Python using defaultdict

import collections as col

#set the default factory with the string 'key not present'
country_dict = col.defaultdict(lambda: 'Key Not Present')
country_dict['India'] = 'IN'
country_dict['Australia'] = 'AU'
country_dict['Brazil'] = 'BR'
print(country_dict['Australia'])
print(country_dict['Canada'])

Python Using setdefault() Method to handle KeyError

country_dict = {'India' : 'IN', 'Australia' : 'AU', 'Brazil' : 'BR'}
country_dict.setdefault('Canada', 'Not Present') #Set a default value for Canada
print(country_dict['Australia'])
print(country_dict['Canada'])

Using get() Method to handle KeyError in Python

country_dict = {'India' : 'IN', 'Australia' : 'AU', 'Brazil' : 'BR'}
print(country_dict.get('Australia', 'Not Found'))
print(country_dict.get('Canada', 'Not Found'))

1 2 3 4 5 6 7 ... 1883 Next
Advertisements
Loading...