computer scince and engineeringpython

python list and tuple in hindi by unitdiploma

0views

•List

•Tuple

List

  • A sequence is a data types that represents a group of elements.
  • The purpose of any sequence is to store and process a group of elements.
  • In python, strings, lists, tuples and dictionaries are very important sequence data types.
  • All sequence allow some common operations.
  • A list is similar to an array that consists of a group of elements or items.
  • Just like array, a list can store elements.
  • But, there is one major difference between an array and a list.
  • An array can store only one type of elements whereas  a list can store different types of elements.
  • Hence lists are more versatile and useful than an array.
  • Perhaps  lists are the most used data types in python programming.

     Student = [10, ‘venu gopal’, ‘M’,  50, 55, 62, 74, 66]

List Contd..

  • Please observe that the elements of the student list are stored in square  bracket [ ].
  • We can create an empty list without any elements by simply writing empty square braces as:

  e_lst= [   ]  #this an empty list

  print(student[1])  # Output : venu gopal 

One of the most useful data structures that you will find are lists.

List can contains both number and string and can hold many of it.

#List with numbers

     lst1 = [1,2,3,4]

#List with strings

     lst2 = [‘string’ , ’can’ , ’go’ , ’another string’]

#List with strings and numbers

     lst3 = [1,2, ‘string’ , ‘another string’]

#Print each out

     print(lst1) 

     print(lst2)

     print(lst3)

Outputs:

[1,2,3,4]

[‘string’ , ’can’ , ’go’ , ’another string’]

[1,2, ‘string’ , ‘another string’]

List Contd..

Since, we just introduced a list, One of the most useful part of list is that they are iterable. An iterable is a collection of data that you can move through using a for loop.

#for loop through a list of numbers

     lst = [1,2,3,4]

#iterate through each number in the list

   for number in Ist:

#put the number along iteration

   print(number)

Output:

  1

  2

  3

  4

Creating for loops and putting it into function

Printing Word in the List

#Declare the list of words

     lst_string=[‘Hello’,’World’)

#Iterate through each word in the list

   for word in Ist_string:

#Print the word during each iteration

   print (word)

Output :

  hello

  world

The range() function

We can use range() function to generate a sequence of integers which can be stored in a list. The format of the range() function is:

  range(start, stop, stepsize)

If we do not mention the ‘start’, it is assumed to be 0 and the ‘stepsize’ is taken as 1. The range of numbers stops one element prior to ‘stop’.

  range(0, 10, 1) 

To loop through set of code a specified number of times we can use the range() function in the list.

The range() function return a sequence of numbers, starting from 0 by default and increment by one is default.

# create an iterable by using range

  for x in range(6):

# print the value of the iterable during each loop

  print(x):

Output:

  0

  1

  2

  3

  4

  5

The range() function Contd..

The range function defaults to 0 as starting value however it is possible to specify starting value by adding parameter range (2,6), which means value from 2 to 6 (but not including 6).

  for x in range (2,6)

     print (x)

Output:

  2

  3

  4

  5 

The range() function default to increment by 1 in the sequence, however it is possible to specify the increment value by adding a third parameter

  range(2,30,3):

  for x in range (2,30,3)

     print (x)

Output:

  5

  8

  11

  14

  17

  20

  23

  26

  29

Tuples

A tuple is a collection where is ordered and unchangeable in python tuples are written in brackets

  tuples=(“apple”, “banana”, “cherry”)

  print(tuples)

  #It will be return (apple, banana, cherry)

Tuples are sequence, just like list the difference between tuples and list are the tuple can not be change unlike lists and tuples use parenthesis, where as list are use sequence brackets

#declare tuples

  tuple1 = (1,2,3,4)

  tuple2 =  tuple ([1,2,3,4])

#Print the tuples

  print(tuple1)

  print(tuple2)

Output:

  (1,2,3)

  (1,2,3)

Just like with list, you access the data using indexes and you can iterate through the data. The biggest difference between a tuples and list, the list are mutable and tuple are immutable.

This means that in python given a list and tuple

# declare list and tuple

  list1=[1,2,3,4]

  tuple1=(1,2,3,4)

  print(list1)

  print(tuple1)

Output:

  [1,2,3,4]

  (1,2,3)

Using tuple in your code

1.   Length of tuples

  tup=(1,2,3)

  print(len(tup))  #It will be return 3

2.  Concatenate of tuples

  tup1=(1,2,3)

  tup2=(3,4,5)

  print(tup1+tup2)  #It will be return (1,2,3,3,4,5) 

3.  Repeation of tuples

  tup=(‘hello’) * 4

  print(tup)  #It will be return (hello, hello, hello, hello)

4.  Membership

  print(3 in (1,2,3))  #It will be return true

5.  Iteration

  for x is (1,2,3):

  print(x)  #It will be return 1,2,3

Built in function

  • len (tuple)   give the length value
  • max (tuple)   give the max value
  • min (tuple)   give the min value
  • tuple (seq)   turn sequence in tuple

Leave a Response