Posts

Function in Python that counts the number of times a character is repeated in a given string

def countxo(string): x_count = f"{string}".count("x") o_count = f"{string}".count("o") if x_count == o_count: print(f"Number of x={x_count} and Number of o={o_count}. Hence", True) else: print(f"Number of x={x_count} and Number of o={o_count}. Hence", False) countxo("xoxox")

Function in Python that returns masked credit card number

  def hideCardNum(g,h,i,j): return f"******{g}{h}{i}{j}" a,b,c,d,e,f,g,h,i,j = input("Plz enter your 10 digits Credit card number: ") print(hideCardNum(g,h,i,j)) def hideCardNum(string): return f"******{string[6:10]}" number = input("Plz enter your 10 digits Credit card number: ") print(hideCardNum(number))

Function in Python that counts the number of vowels in a string

vowels = [] def vowelscount ( string ):     if "a" in string :         vowels . append ( "a" )     if "e" in string :         vowels . append ( "e" )     if "i" in string :         vowels . append ( "i" )     if "o" in string :         vowels . append ( "o" )     if "u" in string :         vowels . append ( "u" )     return len ( vowels )         print ( vowelscount ( "aeiouaeiou" ))

Function that convert a decimal number into binary in Python

  def d2b ( decimalN ):     return bin ( decimalN ). replace ( "0b" , "" ) print ( d2b ( 10 )) # bonus: One liner ninja technique print ( bin ( 10 )[ 2 :])

A function in Python that sorts a list

  def sortlist ( listname , string ):     if string == "asc" :         return sorted ( listname )     elif string == "desc" :         return sorted ( listname , reverse = True )     elif string == "none" :         return listname     else :         print ( "Wrong Input!" )         list1 = [ 4 , 8 , 6 , 1 , 9 , 56 , 33 , 19 , 2 , 55 , 14 ] list2 = [ "z" , "v" , "c" , "r" , "h" , "a" , "s" , "m" , "f" ] print ( sortlist ( list2 , "none" ))

Radians to Degrees and Degrees to Radians conversion in Python

Image
 Radians and Degrees are measures of angles. Method # 1 import math def rad2deg ( radians ):     degrees = math . degrees ( radians )     print ( f "The { radians } radian(s) is(are) equal to { degrees } degrees." )     def deg2rad ( degrees ):     radians = math . radians ( degrees )     print ( f "The { degrees } degrees are equal to { radians } radians." )     rad2deg ( 1 ) deg2rad ( 57.296 ) Method # 2 import math def rad2deg ( radians ):     degrees = radians * ( 180 / math . pi )     print ( f "The { radians } radian(s) is(are) equal to { degrees } degrees." )     def deg2rad ( degrees ):     radians = degrees * ( math . pi / 180 )     print ( f "The { degrees } degrees are equal to { radians } radians." )     rad2deg ( 1 ) deg2rad ( 57.296 )   Mathematical Notation:

printing local time in Python

  import time print ( time . asctime ( time . localtime ( time . time ())))