Showing posts with the label Tuple

Analytics Today: Python Tuple Vs Lists

Tuple Vs. List in Python is the most searchable word. Here, I have shared the differences between these two. Analytics Today: Python Tuple Vs Lists 1. Python List It is simple to create a list in Python.  Comma-separated elements inside a square bracket [] make a list. The list can contain any number of elements of different data types (integer, float, character, etc.)  A list can contain another list. And it is called a nested list List Example #Illustration of creating a list  new_list=[1, 2, 3, 4]  print(new_list)  # Homogeneous data elements  new_list1=[1, "John", 55.5]  print(new_list1)  # Heterogeneous data elements  new_list2=[111, [1, "Clara", 75.5]]  # Nested list  print(new_list2) Output [1, 2, 3, 4] [1, ‘John’, 55.5] [111, [1, ‘Clara’, 75.5]] 2. Python Tuple It is simple to create a tuple in Python.  The elements of a list are enclosed in square brackets []. But these are put in parenthesis (), separated with commas.  A tuple can have any number of elemen

Python: How to Swap Variables with Tuple

S wap two strings no need to define third variable in Python explained. To achieve this, you need a tuple , explained with an example of how to do it. Python ideas How to Swap Two Strings in Python You can swap two strings in Python using a tuple. You can have multiple values in a tuple. In this example, I have given a and b. When you use the swap function, it swaps the values. def swap(a, b):  return b,a  Example to Swap Two Strings. i = "Hello world" j = "This is ApplyBigAnalytics"  i, j = swap(i, j)  print(i)  print(j)  Example to Swap Two Numbers. i = 1  j = 2  i, j = swap(i,j)  print(i)   print(j) The Result is: This is ApplyBigAnalytics  Hello world  2  1 Keep Reading The Real Purpose of Underscore in Python