Skip to main content

Py: 7. Python Tuple

 Tuple is a data structure in Python. It is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses.

The main difference between lists and tuples are : Lists are enclosed within brackets ( [ ] ), and their elements and size can be changed, while tuples are enclosed within parentheses ( ( ) ) and cannot be updated. Tuples can be thought of as read-only lists.

Example:- 

tuple1 = (10, 20, 30, 40, 50, 60)    
for i in tuple1:    
    print(i)   
Output:-
10 20 30 40 50 60

Exercises:-
  1. Write a py program to find the length of tuple
  2. Write a py program to find the maximum element in tuple
  3. Write a py program to find the minimum element in tuple
  4. Write a py program to delete tuple
Why Tuple:-
  1. Tuples use less memory whereas lists use more memory.
  2. We can use tuples in a dictionary as a key but it's not possible with lists.
  3. The tuple is immutable.

Comments

Popular posts from this blog

React: Remember me functionality with React

Want to know how can you easily integrate Remember me on your website just follow the steps below and I am sure you will find this pretty easy. There is no rocket science to crack it you just need basic code knowledge only. Step 1: Add the react-cookie in your project    npm install react-cookie Step2: Add the following code in index.js import { CookiesProvider } from "react-cookie"; ReactDOM.render(      <CookiesProvider>           <App />      </CookiesProvider>,      document.getElementById('root') ); Step3: Add the following code in your component    import React, { useState } from 'react'; import { useCookies } from 'react-cookie'; const App = () => { const [name, setName] = useState(''); const [pwd, setPwd] = useState(''); const [cookies, setCookie] = useCookies(['user']); const handle = () => {      setCooki...

React: Print Specific Section of the page with - ReactToPrint

  Want to know how can you easily print specific section of your website just follow the steps below and I am sure you will find this pretty easy. There is no rocket science to crack it you just need basic code knowledge only. Step 1: Add the ReactToPrint in your project    npm install --save react-to-print Step2: Add the following code in your component   import React, { useRef } from 'react'; import ReactToPrint from 'react-to-print'; const Example = () => {      const componentRef = useRef();      return (           <div>                <ReactToPrint                     trigger={() => <button>Print this out!</button>}                     content={() => componentRef}    ...

JS: Cheatsheet

   Datatypes Difference Between Var, Let and Const Functions and its types Asynchronous operations in JS