Tutorialspoint

Python3 Programming Examples

Python dynamically typed example

## assigning a value to a variable
x = [1, 2, 3]
## x is a list here
print(type(x))
## reassigning a value to the 'x'
x = True
## x is a bool here
print(type(x))
## we can also redefine 'x' as many times as we want

Python Queue

#Adding elements to queue at the rear end
def enqueue(data):
   queue.insert(0,data)
    
#Removing the front element from the queue
def dequeue():
   if len(queue)>0:
       return queue.pop()
   return ("Queue Empty!")

#To display the elements of the queue
def display():
   print("Elements on queue are:");
   for i in range(len(queue)):
       print(queue[i])

# executable code
if __name__=="__main__":
   queue=[]
   enqueue(5)
   enqueue(6)
   enqueue(9)
   enqueue(5)
   enqueue(3)
   print("Popped Element is: "+str(dequeue()))
   display()

Python WHILE LOOP WITH KNOWN LENGTH

genre = ("Python","C","C++","Java")
print("The topic available on Tutorial's Point are:")
i = 0
while (i < len(genre)):
   print (genre[i])
   i += 1 

Python FOR LOOP WITH KNOWN LENGTH

genre = ("Python","C","C++","Java")
print("The topic available on Tutorial's Point are:")
for i in range(len(genre)):
   print (genre[i])

Python FOR LOOP WITHOUT LENGTH

genre = ("Python","C","C++","Java")
print("The topic available on Tutorial's Point are:")
for i in genre:
   print (i)

Python Enumerate Data type

genre = ("Python","C","C++","Java")
iterator = enumerate(genre)
print("The topic available on Tutorial's Point are:")
for i,v in iterator:
   print (v,end="\t")

Python Zip Function

genre = ("Python","C","C++","Java")
extras = ["C#","Dart","Erlang","Go"]
print("The topic available on Tutorial's Point are:")
for i, j in zip(genre,extras):
   print (i, j,sep="\t") 

Python Vars() Function Ex3

class test():
   # Python __repr__() function returns the representation of the object.
   # It may contain any valid expression such as tuples,  list, dictionary, string, set  etc
   def __repr__(self):
      return "Tutorial's point"
   def localvariables(self):
      number = 4
      return locals()
    
if __name__ == "__main__":
   obj = test()
   print (obj.localvariables())

Python Vars() Function Ex2

class sample:
   company = "Tutorial's Point "
   Number = 4
   Topic = "Python 3.x."
obj = vars(sample)
print(obj)

Python Vars() Function Ex1

class test:
   def __init__(self, integer_1=555, integer_2=787):
      self.integer_1 = integer_1
      self.integer_2 = integer_2
obj_test = test()
print(vars(obj_test))

1 2 3 4 5 6 7 ... 206 Next
Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.