Posts

Sets In Python

  # with no duplicate elements. # Basic uses include membership testing # and eliminating duplicate entries. # Set objects also support mathematical operations # like union, intersection, difference, # and symmetric difference. s = set () print ( type ( s )) # Output : <class 'set'> l = [ 1 , 2 , 3 , 4 ] s_from_list = set ( l ) print ( s_from_list ) # Output : {1, 2, 3, 4} print ( type ( s_from_list )) # Output : <class 'set'> s . add ( 1 ) # 1 will be added to empty set s print ( s ) # Output : {1} ...

Modules & Pip.py

  # Sometimes we have to use someone else’s code(Module) in our program # because it saves our lot of time and off-course it is legal and free. #### Modules #### # Module – Module or library is the file which contain definitions of several functions, # classes, variables, etc. which are written by someone else for free use. #### Pip #### # Pip – Pip is a package manager in python # i.e. pip command is used to download any external module in python. # It is something like which helps us to get something from somewhere # and automatically save packages at suitable location for futher use. ### We can install any...

Complete-Python-Tutorial-and-Notes

  # Python Programming language was developed by Guido Van Rossum in February 1991. # It is an interpreted, high-level, general-purpose programming language. # Programming helps human to reduce manual efforts and # work which take hours to complete can be accomplished by computer in Seconds. # To write any language’s code we need a platform where # we can write the code and can execute it. # For this we use IDE’s. # IDE – IDE (Integrated Development Environment) # is a software application which provides many comprehensive facilities # to programmers for software or application development. # You can use Pycharm and Visual Studio Code as IDE ############################################################################# ####### Inst...

Recursions: Recursive Vs Iterative Approach

# n! = n * n-1 * n-2 * n-3.......1 # n! = n * (n-1)! def factorial_iterative (n): """ :param n: Integer :return: n * n-1 * n-2 * n-3.......1 """ fac = 1 for i in range (n): fac = fac * (i + 1 ) return fac # def factorial_recursive (n): """ :param n: Integer :return: n * n-1 * n-2 * n-3.......1 """ if n == 1 : return 1 else : return n * factorial_recursive(n - 1 ) # 5 * factorial_recursive(4) # 5 * 4 * factorial_recursive(3) # 5 * 4 * 3 * factorial_recursive(2) # 5 * 4 * 3 * 2 * factorial_recursive(1) # 5 * 4 * 3 * 2 * 1 = 120 # 0 1 1 2 3 5 8 13 def fibonacci (n): if n == 1 : return 0 elif n == 2 : return 1 else : return fibonacci(n - 1 ) + fibonacci(n - 2 ) number = int ( input ( "Enter then number \n " )) # print("Factorial Using Iterative Method", factorial_it...

Using With Block To Open Python Files

if we use with block to read a file line and than open file simply to read file lines So whether the file lines will open simply or not...?? # SOLUTION # f = open("raaz.txt", "rt") # # f.close() # Above open close syntax can be replaced by single statement # open file using with block # Everything else(means functions like readlines,seek,etc.) on f are same # first create a text file "raaz" then run this program. # with open("raaz.txt") as f: # a = f.read(7) # print(a) # Output : Stud # with open("raaz.txt") as f: # a = f.readlines() # Give List of lines # print(a) # Output : # ['Students should use LinkedIn to get connected with Corporate and Research world.\n', # 'It will be very helpful for students.'] # Question of the day - # Question 1: # Can we do readlines outside with block? # Yes or No and why? # Answer : # No, because with block closes the files once we get out of with block. # Question 2: # f...

Seek(), tell() & More On Python Files

## Set Up Steps for this tutorial :=> # Make a File "28. Tutorial.txt" # Add few lines of text in it. f = open ( "28. Tutorial.txt" ) # f.tell() tells us where is file pointer currently in file # first charcter is at 0 index in file print ( f . tell ()) # Output : 0 print ( f . readline ()) # Print First Line in the File # Output : Can We build Aatmnirbhar Districts in India ? # f.seek() is used to reset file pointer f . seek ( 7 ) # move file pointer to 5th index print ( f . readline ()) # Print First Line in the File from index 7 # Output : build Aatmnirbhar Districts in India ? print (...

Exercise

# Question : ## Pattern Printing ## # Input : # Integer n # Boolean = True or False # Expected Output : # Given: True n=4 # Output: # * # ** # *** # **** # Given: False n=4 # Output: # **** # *** # ** # * # Solve Here : r = int ( input ( "Enter Row: \n " )) n = int ( input ( "Enter Boolean 1/0: \n " )) b = bool (n) if b: for i in range (r): print ( "*" * i) else : for a in range (r): print ((r-a) * "*" )