Skip to main content

Python | Data Structure Or Data Type (Tuple)

Data Type Of Python

 In this session we will see last data type of python which is Tuple.

5) Tuple:- Tuple is a ordered collection of immutable datatype, tuples are quite similar to lists but difference is list are mutable where as tuple are immutable.
  • Empty tuple:- 
    t = () or tuple()

  • Tuple with single element:-
    t = (10,)

  • Tuple with multiple element:-
    t1 = ( 10, 20, 30, 'Hello' )

  • Nested tuple:- 
    t1 = ( 10, 20, 30, 'Hello' )
    p = t2, 30, 40, 50
    print(p) => ((10, 20, 30, 'Hello'), 30, 40, 50)

Methods of Tuple:- 
  1. count(value):- It return the count of value if the value is present in the tuple else it return 0.
    For example:-
    t = (10, 20, 30, 'Hello')
    t.count(10) => 1 
    t.count(50) => 0

  2. index(value,start,end):- It return the index number if the value is present in the tuple, otherwise it raised value error.
    For example:-
    t = (10, 20, 30, 'Hello')
    t.index(10) => 0
    It return the index number of value 10.


Comments

Popular posts from this blog

Python | Introduction

Python is developed by Guido van Rossum . Guido van Rossum started implementing python in 1989. Python is very simple programming language so if you are new to programming, you can learn python without facing any issue. Features of Python Free and Open Source -                                                                                      Python is a open source language. We can easily install python.It is freely available at python's official website . High level language -                                                                                     ...

Basic Knowledge of Python part 1

Basic Mathematics equations using Python- As we know python is a simple and easy to learn programming language. In this section we learn how to done mathematics equations using python. To perform this we use our basic operators +,-,/,* .  To perform python we have to open python interpreter. In this interpreter you can type simple equation like 5+5, 4/2 etc. For example: The integer number (e.g. 1,2,3....) have type int and the number with fractional part (e.g. 2.5,2.1,4.5...) have type float. Note:-  The division type always returns float. To get only an integer result of division you can use the // operator and to get remainder from division result you can use % operator. In python you can easily calculate power of the number by using ** operator. Like this you can easily calculate mathematics equation. Now we see how to find area of rectangle. Area = length*width is the formula to find area of rectangle. Here '=' sign is used to store value to the variable. You can als...

Python | Data Structure Or Data Type (Dictionary)

Data Type Of Python In this session we will see the new data type of python which is Dictionary. 4) Dictionary:- Dictionary is a unordered collection of mutable data type. It contains key value pair enclosed in curly braces {}. Dictionary are hashable by keys. Dictionary keys should be unique and key should be immutable. That means we can use string, tuple, frozenset as a dictionary key but not list, dictionary, or other mutable types. Empty dictionary:-  D = {} Dictionary with multiple elements:- D = { 'a':10, 'b':20, 'c':30 } Here the part before colon is the key and the part after colon is value like this dictionary has key value pair. Iteration:-  D = { 'a':10, 'b':20, 'c':30 } For i in D:        print(i) => 'a'      'b'      'c' In the dictionary only keys are hashable not values therefor when you iterate dictionary you get only keys. Hashable:-  Dictionary are hashable by keys. For example:- D = { 'a...