By- Anshul Kumar Class: XI “B”

Slides:



Advertisements
Similar presentations
ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
Advertisements

Course A201: Introduction to Programming 10/28/2010.
CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
Tuples. Tuples 1 A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The only difference is that tuples can't be.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Guide to Programming with Python
Lists Introduction to Computing Science and Programming I.
Lists/Tuples. Example Write a program to keep track of all students’ scores on exam 1. Need a list of everyone’s score Use 14 doubles What about next.
Guide to Programming with Python
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Lecture 21 - Tuples COMPSCI 101 Principles of Programming.
Chapter 7 Lists and Tuples. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Data Structures.
Built-in Data Structures in Python An Introduction.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
10. Python - Lists The list is a most versatile datatype available in Python, which can be written as a list of comma-separated values (items) between.
Lecture 19 - More on Lists, Slicing Lists, List Functions COMPSCI 101 Principles of Programming.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Lists CS303E: Elements of Computers and Programming.
1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible via index offsets into its set of elements. Examples:
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Strings … operators Up to now, strings were limited to input and output and rarely used as a variable. A string is a sequence of characters or a sequence.
1 CS Review, iClicker -Questions Week 15. Announcements 2.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Dictionaries Alexandra Stefan CSE1310 – University of Texas at Arlington.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
Unit 4 – Chapter 4 Strings and Tuples. len() Function You can pass any sequence you want to the len() function and it will return the length of the sequence.
String and Lists Dr. José M. Reyes Álamo.
Using Molecular Biology to Teach Computer Science
CMSC201 Computer Science I for Majors Lecture 14 – Tuples
Tuples and Lists.
Containers and Lists CIS 40 – Introduction to Programming in Python
Chapter 4 Strings & Tuples
Section 6: Sequences Chapter 4 and 5.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Creation, Traversal, Insertion and Removal
CHAPTER THREE Sequences.
Guide to Programming with Python
8 – Lists and tuples John R. Woodward.
4. sequence data type Rocky K. C. Chang 16 September 2018
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Chapter 5: Lists and Dictionaries
Presented by Mirza Elahi 10/29/2018
Topics Sequences Introduction to Lists List Slicing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CS1110 Today: collections.
15-110: Principles of Computing
Topics Sequences Lists Copying Lists Processing Lists
CISC101 Reminders Assignment 2 due today.
CHAPTER 4: Lists, Tuples and Dictionaries
15-110: Principles of Computing
Topics Sequences Introduction to Lists List Slicing
And now for something completely different . . .
CMSC201 Computer Science I for Majors Lecture 16 – Tuples
Intro to Computer Science CS1510 Dr. Sarah Diesburg
By Himanshi dixit 11th ’B’
CSE 231 Lab 7.
More Basics of Python Common types of data we will work with
Tuple.
Introduction to Computer Science
Presentation transcript:

By- Anshul Kumar Class: XI “B” TUPLE By- Anshul Kumar Class: XI “B”

Introduction In python, tuple is also a kind of container which can store list of any kind of values. Tuple is an immutable data type which means we cannot change any value of tuple. Tuple is a sequence like string and list but the difference is that list is mutable whereas string and tuple is immutable. In this ppt we will see manipulation tuple i.e. creation of tuple, its uses and functions.

Creation of Tuple In python, “( )” parenthesis are used for tuple creation. () empty tuple (1,2,3) integers tuple (1,2.5,3.7,7) numbers tuple (‘a’, ’b’, ‘c’) character tuple (‘a’, 1, ‘b’,3.5, ‘zero’) mixed value tuple (‘one’, ‘two’, ‘three’, ‘four’) string tuple *tuple is an immutable sequence whose values cannot be changed

Creation of Tuple Look at following examples of tuple creation carefully- Single tuple: Long tuple: Nested tuple: >>>t=(1) >>>t 1 >>>t=(0,1,2,3,4,5,6,7,8,9,10,11,12,13) >>>t (0,1,2,3,4,5,6,7,8,9,10,11,12,13) >>>t=(1,2,3,(4,5)) >>>t (1,2,3,(4,5))

Creation of Tuple >>>t=tuple(“Hello”) >>>t (‘H’ , ‘E’, ‘L’, ‘L’, ‘O’) Tuple() function is used to create a tuple from other sequences. See examples Tuple creation string Tuple creation from input Tuple creation from list >>>t1=tuple(input(‘enter element’)) enter element 12345 >>>t1 (‘1’,’2’,’3’,’4’,’5’) >>>L=[‘a’, ‘e’, ‘i’, ‘o’, ‘u’] >>>T=tuple(L) >>>T (‘a’, ‘e’, ‘i’, ‘o’, ‘u’)

Accessing a Tuple Indexing and Slicing: T[i] : Returns the item present at index i. T[i:j] : Returns a new tuple having all the items of T from index i to j. T[i:j:n] : Returns a new tuple having difference of n elements of T from index I to j. >>>T=(1,2,3,4,5,6,7,8,9,10) >>>T[1:10:3] (2,5,8)

Accessing a Tuple Concatenation and Replication operators Membership operator: Working of membership operator “in” and “not in” is same as in a list. Concatenation and Replication operators +operator adds second tuple at the end of first tuple. * operator repeats elements of tuple.

Accessing a Tuple Accessing Individual elements- >>>L=[‘a’, ‘e’, ‘I’, ‘o’, ‘u’] >>>L[0] ‘a’ >>>L[3] ‘o’ Traversal of a Tuple- (‘P’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’) P Y T H O N T=tuple(“Python”) Print(T, end=“ “) Print( ) For a in T : print(a) OUTPUT

Tuple Operations Tuple joining (+) Tuple Replication- (*) Both the tuples should be there to add with+. SOME ERRORS IN TUPLE JOINING- In Tuple + number In Tuple + complex number In Tuple + string In Tuple + list Tuple + (5) will also generate error because when adding a tuple with a single value, tuple will also be considered as a value and not a tuple.. Tuple Replication- (*) >>>tp1=(1,3,5) >>>tp2=(6,7,8) >>>tp1+tp2 (1,3,5,6,7,8) >>>tp3=tp1+tp2 >>>tp3 >>>tp1=(1,3,5) >>>tp1 * 3 (1,3,5,1,3,5,1,3,5) >>>tp1 (1,3,5) >>>tp2=tp1*3 >>>tp2

Tuple Slicing >>>tp1=(10,12,14,20,22,24,30,32,34) >>>seq=tp1[3:-3] >>>seq (20,22,24) >>>tp1[3:30] (20,22,24,30,32,34) >>>tp1[-15:7] (10,12,14,20,22,24,30) Tuple will show till last element of list irrespective of upper limit.

Tuple Slicing >>>tp1=(10,12,14,20,22,24,30,32,34) (10,14,22,30,34) >>>tp1[0:10:3] (10,20,30) Tp1[ : : 3] Every alternate element will be shown. Every third element will be shown.

Tuple Tuple Comparision >>>a=(2,3) >>>b=(2,3) True >>>c=(‘2’, ‘3’) >>>a==c False >>>a>b

Tuple Tuple Comparision >>>d>a False >>>d==a True >>>a<e

Tuple Unpacking >>>t=(2,3,’A’, ‘b’) >>>w, x, y, z=t >>>print(w) 2 >>>print(x) 3 >>>print(y) A >>>print(z) B

Tuple Deletion As we know that tuple is of immutable type, it is not possible to delete an individual element of a tuple. With del() function, it is possible to delete a complete tuple. Look at following example- >>>t= (2,3, ‘A’, ‘B’) >>>del t[2] Error shown because deletion of a single element is not possible >>>del t >>>t Complete tuple has been deleted. Now error shown on printing of tuple

Tuple Function len( )Function Max( ) Function Min( ) Function >>>emp=(‘Ram’, 25000, 24, ‘LKO’) >>>len(emp) 4 >>>tp1=(10,12,14,16,18,20,22) >>>max(tp1) 22 >>>tp12=(“karan”, “Zubin”, “Zara”, “Ana”) >>>max(tp12) ‘Zubin’ >>>min(tp1) 10 len( )Function Max( ) Function Min( ) Function

Tuple Function >>>min(tp12) ‘Ana’ >>>tp12.index(“Zubin”) 1 >>>tp13=(10,12,14,16,10,18,20,10,22) >>>tp13.count(10) 3 >>>t=tuple(“Hello”) >>>t (‘H’, ‘e’, ‘l’, ‘l’, ‘o’) Index( ) Function Tuple( ) Function

THANK YOU