Posts

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) * "*" )

Writing Appending to a File

# f is better to say file handle which open() return # instead saying it pointer but commonly f also known as file pointer # Opening file in write,text mode # Only 'w' mentioned below because t-text mode is by default # So 'w' is same as 'wt' # IF file "26. Tutorial.txt" not exist then it will create that. # If alrady exist then it will replace all the content with what we will write in the file # f = open("26. Tutorial1.txt", "w") # f.write("Rural Development Party - A party that contests only gram panchayat elections and has the vision to make World Class Villages with limited resources.\n") # f.close() # If we again do f.write on same file it just replace content # but in most cases we want to append the content in the file at end # for that open file in append mode -'a' # f = open("26. Tutorial1.txt", "a") # due to newline character "\n" at the last of previous write # this content...

Readline function to read one line a time, Readlines Function to get list of lines.

# Readline function to read one line a time # f = open("24. Tutorial.txt", "rt") # print(f.readline()) # f.close() # Output : # We are on a mission to transform and Optimize World with Indian Technologies. # f = open("24. Tutorial.txt", "rt") # print(f.readline()) # print(f.readline()) # print(f.readline()) # f.close() # Output : (new line in file already + one line gap due to print's by default backslash) # We are on a mission to transform and Optimize World with Indian Technologies. # We will work hard. # We will win. ########## Readlines Function to get list of lines. # f = open("24. Tutorial.txt", "rt") # print(f.readlines()) # f.close() # Output : # ['We are on a mission to transform and Optimize World with Indian Technologies.\n', # 'We will work hard.\n', 'We will win.'] ###################################################### # d = open("raaz", "rt") # print(d.readlines()) ...

Python File IO Basics

# Two types of memory # 1. Volatile Memory : Data cleared as System Shut Down # Example : RAM # 2. Non-Volatile Memory : Data remains saved always. # Example : Hard Disk # File : In Non-Volatile Memory we store Data as File # File may be text file,binary file(Ex - Image,mp3),etc. # Different Modes of Opening files in Python """ "r" - Open file for reading - Default mode "w" - Open a file for writing "x" - Create file if not exists "a" - Add more content to a file/ append at end in file "t" - open file in text mode - Default mode "b" - open file in binary mode "+" - for update (read and write both) """ # Question of the tutorial: # How to print docstring of func1() # Answer : print(func1.__doc__) ######### INOpen(), Read() & Readline() For Reading File ########## ############ Open(), Read() & Readline() For Reading File ######### # file pointer(here f) returned by open function is...