Tuesday, December 29, 2020

Tuples

Tuples in python

Tuples are more like a list, with a main difference that tuples are immutable.

We denote tuple elements enclosed in brackets.

my_tuple=(1,2,3,4,5) is same as

my_tuple= 1,2,3,4,5

whenever the elements are comma separated it is taken as a tuple.

Doing the simple calculation program using function and tuple.

This is the code we used for understanding namespaces

 

Now,instead of printing the result within the function,use return function to bring the result back to main function.

  • return statement returns the value of the function back to main function

  • function terminates with return statement.

#function

def cal():

        return a+b,a-b,a*b,a/b  // returns as tuples(4,0,4,1)

#main

 num1=2

 num2=2

 sum,diff,pro,div=cal() // unpacks the values and assigns to                                                             sum,diff,pro,div.

Reversing two numbers using tuple.

num1=10

num2=20

num1,num2=num2,num1

print(num1,num2)

20,10

 

No comments:

Post a Comment

Anaconda Installation

In this post we will discuss about Anaconda and its installation. Anaconda comes with number of applications(jupyter notebook included), dat...