Top 5 py tricks..

Top 5 py tricks..

·

1 min read

1.FACTORIAL OF A NUMBER To find the factorial of any number don't need of four to five lines of code in python as of c,c++,java.Just import math module then follow the code as

  import math
  print(math.factorial(5))
  #output   120

2.REVERSE THE STRING To reverse the string just use slicing concept .

  text="python"
  print(text[::-1])
  #output   nohtyp

3.UNIQUE VALUES IN THE LIST To find the unique values in the list[] or tuple() just change the list or tuple to set{},because the set contains only unique values and just made type casting back to list.

      a=[1,2,3,1,2,5,6,1,3,2,9]
      print(set(a))
      print(list(a))
      #output    {1,2,3,5,6,9}

4.SWAPPING OF TWO NUMBERS To swap make a three line code..

      x,y = 11, 34
      print x
      print y
      x,y = y,x
      print x
      print y
      #output 11
              34
              34
              11

5.PRINT N TIMES A STRING Use * operator to print the string N number of times.

       str ="Point"
       print(str * 3)
       #output  PointPointPoint