28 Dec 2016

Leisure Activity: Python3 welcomes upcoming new year..!

Code:


 '''  
 Created on 28-Dec-2016  
   
 @author: linuxbeginner  
 '''  
 from time import sleep  
   
 def anim():  
   sleep(0.10)  
   
 welcome = ['W','E','L','C','O','M','E']  
 twok = ['T','w','o','*','*','T','h','o','u','s','a','n','d']  
 seventeen = ['S','e','v','e','n','t','e','e','n']  
 welcome_counter = 0  
 twok_counter = 0  
 seventeen_counter = 0  
   
 for i in range(0,5):  
   for sp in range(0,7):  
     print(' ',end="")  
   for j in range(4,i,-1):  
     print(' ',end="")  
   for k in range(0,i):  
     if i == 4:  
       print('{0}'.format(welcome[welcome_counter]),end="")  
       welcome_counter += 1  
     else:  
       print('*',end="")  
   for l in range(1,i):  
     if i == 4:  
       print('{0}'.format(welcome[welcome_counter]),end="")  
       welcome_counter += 1  
     else:  
       print('*',end="")  
   print(end="\n")  
   anim()  
     
 for i in range (0,3):  
   for j in range(0,i):  
     print(' ',end="")  
   for k in range(3,i,-1):  
     print('*',end="")  
   for l in range(0,15):  
     if i == 0 and l in range(1,14):  
       print('{0}'.format(twok[twok_counter]),end="")  
       twok_counter += 1  
     elif i == 1 and l in range(3,12):  
       print('{0}'.format(seventeen[seventeen_counter]),end="")  
       seventeen_counter += 1  
     else:  
       print('*',end="")  
   for m in range(3,i,-1):  
     print('*',end="")  
   print(end="\n")  
   anim()  
     
 print(end="\n")  

Output is shown in the following GIF image:



12 Dec 2016

Leisure Activity: Python3 wishes a good bye to 2016..!

Code:


 '''  
 Created on 12-Dec-2016  
   
 @author: linuxbeginner  
 '''  
 bye = ['B','Y','E']  
 two = ['T','W','O']  
 thousand = ['T','H','O','U','S','A','N','D']  
 sixteen = ['S','I','X','T','E','E','N']  
 num = ['2','0','1','6']  
 two_counter = 0  
 thousand_counter = 0  
 sixteen_counter = 0  
 num_counter = 0  
   
 print(end="\n")  
 for i in range(0,13):  
   for j in range(13,i,-1):  
     print(' ',end="")  
   for k in range(0,i+1):  
     if i == 0:  
       print('{0} '.format(bye[0]),end="")  
     elif i == 1:  
       print('{0} '.format(bye[1]),end="")  
     elif i == 2 and (k == 0 or k == 2):  
       print('{0} '.format(bye[2]),end="")  
     elif i == 4 and (k in range(1,4)):  
       print('{0} '.format(two[two_counter]),end="")  
       two_counter += 1  
     elif i == 7:  
       print('{0} '.format(thousand[thousand_counter]),end="")  
       thousand_counter += 1  
     elif i == 10 and (k in range(2,9)):  
       print('{0} '.format(sixteen[sixteen_counter]),end="")  
       sixteen_counter += 1  
     elif (i == 12 and (k in range(2,6))) or (i == 12 and (k in range(7,11))):  
       print('{0} '.format(num[num_counter]),end="")  
       num_counter += 1  
       if k == 5:  
         num_counter = 0  
     else:  
       print('* ',end="")  
   print(end="\n")  
 print(end="\n")  

Output:



8 Dec 2016

Recursion and File Handling demonstration using Python

The following program of Fibonacci series computes using recursive function and saves the output to a text file. The code is as shown below:

 # A program demonstrating recursion and file handling  
   
 x = 0 ; y = 1   
   
 def fibonacci(x,y,lim):  
      if lim != 0:  
           z = x + y  
           f.write('{0} '.format(z))  
           print('{0}'.format(z),end="\t")  
           x = y  
           y = z  
           lim -= 1  
           fibonacci(x,y,lim)  
        
 lim = int(input('Enter the size of series:'))            
 f = open('output.txt','w')  
 fibonacci(x,y,lim)  
 f.close()  
 print(end="\n")  

The output is shown in the following GIF image: