Category Archives: Python

Linear Search using Python

”’ Created on Oct 14, 2018 Linear Search using Python @author: asharda ”’ arr = [10, 20, 30] for x in arr: if(x == 10): print(“Number is found”) break else: print(“Number is not found”) Advertisements

Posted in Python, Uncategorized | Leave a comment

File Handling using Python

file_name=input(“Enter file name”) file_n=open(file_name,”r”) strings=file_n.read() print(strings) file_n.close()

Posted in Python, Uncategorized | Leave a comment

GCD using Python

”’ Created on Oct 12, 2018 @author: asharda ”’ from fractions import gcd gcd_p=gcd(12,8) print(gcd_p)

Posted in Data Structures, Python, Uncategorized | Leave a comment

Factorial in Python

”’ Created on Oct 12, 2018 @author: asharda ”’ def factorial(n): if(n==1): return 1 return factorial(n-1)*n print(factorial(6))

Posted in Python, Uncategorized | Leave a comment

Using default dictionary

”’ Created on Sep 22, 2018 @author: asharda ”’ from collections import defaultdict sentence=”Cats hate dogs and dogs hate CATS” word_dic=defaultdict(int) for word in sentence.split(): word_dic[word]+=1 print(word_dic)

Posted in Python, Uncategorized | Leave a comment

Using named tuple Python

”’ Created on Sep 21, 2018 @author: asharda ”’ from collections import namedtuple vector=namedtuple(‘Dimensions’,’x y z’) vec_1=vector(1,1,1) vec_2=vector(2,2,2) tot=vec_1.x+vec_2.x print(“Total seen is”,tot)

Posted in Python, Uncategorized | Leave a comment

Count occurence of words in a Sentence using Python

sentence=”Peter Piper climbed a wall .Then Peter saw another wall” words_dict={} for words in sentence.split(): if words not in words_dict: words_dict[words]=1 else: words_dict[words]+=1 print(words_dict)

Posted in Data Structures, Python, Uncategorized | Leave a comment