Python Tutorials

Closures in Python with Examples 0

Closures in Python with Examples

Program 1 def first(): def second(): a=700 return a return second() # Driver Main print(first()) # def first(): # print(“Hello Friends”) # # Main Driver # myfun=first # myfun() # #print(myfun()) # Nested function...

Python Thread Synchronization in Multithreading 0

Python Thread Synchronization in Multithreading

Program 1 import time from threading import * l=Lock() def printable(n): l.acquire() for i in range(1,11): print(n*i) time.sleep(1) l.release() # Calling (Main Thread) T1=Thread(target=printable,args=(5,),name=”First”) T2=Thread(target=printable,args=(7,),name=”Second”) T3=Thread(target=printable,args=(9,),name=”Third”) start=time.time() T1.start() T2.start() T3.start() T1.join() T2.join() T3.join() end=time.time()...

Race Condition in Python 0

Race Condition in Python

Program 1 # Program for Racing condition import time from threading import * def printable(n): for i in range (1,11): print(n*i) print() time.sleep(1) #Calling (Main Thread) T1=Thread(target=printable,args=(5,),name=”First ” ) T2=Thread(target=printable,args=(7,),name=”Second ” ) T3=Thread(target=printable,args=(9,),name=”Second “...

How to Create a Thread in Python 0

How to Create a Thread in Python

Program 1 import time from threading import * def display(): for i in range(1,11): print(i) time.sleep(1) def show(): for i in range(100,111): print(i) time.sleep(1) # Calling (Main Thread) T1=Thread(target=display) # New Born T2=Thread(target=show) #...

Lambda with reduce() Function in Python 0

Lambda with reduce() Function in Python

Program 1 # Program for reduce function import functools # def myfunction(a,b): # return a+b # mylist=[2,4,6,8] # add=functools.reduce(myfunction,mylist) # print(add) #Reduce with Lamda function mylist=[2,4,6,8,6,7,8,12,55] value=functools.reduce(lambda a,b:(a+b),mylist) print(value) mylist=[2,3,4,5,6,8] mylist1=list(map(lambda x:(x*x),mylist)) print(mylist1)  

How to Read Data from CSV File in Python 0

How to Read Data from CSV File in Python

Program 1 # how to read data from CSV File import csv,os,sys try: if(os.path.isfile(“c://myfile/emp.csv”)): f=open(“c://myfile/emp.csv”,”r”) obj=csv.reader(f) mylist=list(obj) for row in mylist: for col in row: print(col,end=” “) print() else: print(“File Not Found……”) except Exception...

Escape Sequence in Python 0

Escape Sequence in Python

Program 1 #print(“wel come \n python course \n we are student of python”) #print(“Id\t Name\t Qty \t Amount”) # print(“\”Hello Friends\””) # print(“\’Hello Friends\'”) # print(“Wel Come “) # print(“Wel \bCome “) # print(“\\n-New...

Difference Between List and Tuple in Python 0

Difference Between List and Tuple in Python

Program 1 import sys mylist=[100,”Hello”,True,12.44,15+5j,”Bhopal”,100,500,100] # Mutable # print(mylist) # mylist.append(“Bye Bye”) # print(mylist) mytuple=(100,”Hello”,True,12.44,15+5j,”Bhopal”) # Immutable # print(“Size of List”,sys.getsizeof(mylist)) # print(“Size of Tuple”,sys.getsizeof(mytuple)) #mytuple[1]=”Indore City” # print(mylist) # mylist[1]=”Indore City” # print(mylist)...

Palindrome and Armstrong Number in Python 0

Palindrome and Armstrong Number in Python

Program 1 # Number Palindrome or not n=int(input(“Enter a number”)) m=n s=0 while(n!=0): r=n%10 s=s*10+r n=n//10 #print(“Reverse is :”,s) print(“No is palimdrome”) if(m==s) else print(“No is not palimdrome”) Program 2 # n=int(input(“Enter a number”))...

How to Create Function in Python 0

How to Create Function in Python

Program 1 def display(): print(“Welcome in python function”) def addition(): a=int(input(“Enter first number”)) b=int(input(“Enter second number”)) c=a+b print(“Addition is : “,c) def sub(): a=int(input(“Enter first number”)) b=int(input(“Enter second number”)) c=a-b print(“Addition is : “,c)...