Skip to document

Accenture 2 - Practice notes

Practice notes
Course

python programming (cs8151)

86 Documents
Students shared 86 documents in this course
University

Anna University

Academic year: 2014/2015
Uploaded by:
0followers
4Uploads
0upvotes

Comments

Please sign in or register to post comments.

Preview text

Python Data Structure

Tuples

Hands-on Exercise

Index

  • 1 Objectives
  • 2 Overview
  • 3 Prerequisites
  • 4 Preliminary Concept
    • 4 What is Tuple?
  • 5 Guided Exercise
    • 5 Problem Statement
    • 5 Tuples
      • 5.2 Create Tuple
      • 5.2 Indexing
      • 5.2 Slicing
      • 5.2 Concatenation and Repetition
      • 5.2 Sorting
      • 5.2 Nested Tuple
      • 5.2 Iterating through Tuple
  • 6 Hands-on Exercise
    • 6 Problem Statement
      • 6.1 Solution Instructions

2 Overview

This document provides scenario-based guided and practice exercises on tuples to help you understand the concept. ▪ Integrated Development Environment (IDE) to perform exercise: Jupyter Notebook ▪ Estimated time needed: 20 minutes Note: The links provided in the document navigate to the respective web sources and open in the same window. The users can use the back arrow in the browser, which navigates back to the beginning of the document.

3 Prerequisites

  1. Visit edX website and check the reference videos on List and Tuples
  2. Jupyter Notebook installation

5 Guided Exercise

5 Problem Statement Write the code snippets to show different ways of creating tuples for the following scenario. Scenario: Consider that you want to evaluate player details for making a fantasy cricket team called “My Super”. Here are the details:

  • Player - Name of the player
  • Team - Player belongs to which country
  • Matches - Total numbers of matches played
  • Innings - Total number of innings played
  • NO - Total number of NOT OUT
  • Runs - Total number of runs
  • HS - Highest score
  • Average - Batting average
  • 100s - Total number of hundreds scored
  • 50s - Total number of fifties scored Few experts have recommended the player details based on performance in different leagues. You have collected all the recommendations with records for each player. The following dataset shows the details: Player Team Matches Innings NO Runs HS Average 100s 50s Alex Team 1 251 242 39 12040 183 59 43 60 Henry Team 2 77 75 11 3580 125 55 12 16 Evan Team 1 224 217 32 9115 264 49 29 43 John Team 3 232 216 39 8574 181 48 21 51 William Team 4 126 122 3 4822 153 41 16 26 James Team 5 143 126 30 5507 185 47 12 35 Liam Team 6 123 121 6 5267 179 45 18 21 Oliver Team 4 151 144 14 6173 148 47 13 39

5 Tuples 5.2 Create Tuple To create a tuple, place all the elements within round brackets (), separated by commas. a. Create a tuple with mixed data types

Create tuple

my_tuple = ("Alex",12040,59) my_tuple Output ('Alex', 12040, 59) Look at the figure below to learn more: b. Check the type of variable

Check the type

print(type(my_tuple)) Output <class 'tuple'> c. Create a tuple with single element

Relationship between index and items in the tuples a. Access the values in a tuple using index

Access elements using index

my_tuple = ("Alex",12040,59) print(my_tuple[0]) print(my_tuple[1]) print(my_tuple[2]) Output Alex 12040 59. b. Check the type of each element in the tuple

Print the type of value on each index

print(type(my_tuple[0])) print(type(my_tuple[1])) print(type(my_tuple[2])) Output Alex 12040 59.

5.2.2 Negative Indexing Access the last element, -1 refers to last element, -2 refers to second last element and so on. # Use negative index to access last element and second last element my_tuple[-1] my_tuple[-2] Output 59. 12040

Use negative index to get the value of the second last element

batting_avg[-2] Output 49. 5.2 Slicing You can access number of elements in a tuple using slicing [ start: end ]. a. Slice indexes from 0th to 2nd my_tuple = ('Alex', 12040, 59, 'Evan', 9115, 49) # Slice indexes 0th to 2nd

Output ('Alex', 12040, 59, 'Evan', 9115, 49) b. Use * sign to repeat the element of the tuple for a given number of times

Repeat

my_tuple * 2 Output (Evan', 12040, 59, 'Evan', 12040, 59) 5.2 Sorting a. Sort values in a tuple and save to a new tuple

A sample tuple

batting_average = (47,45,47,59,49,55)

Sort the tuple

batting_average_sorted = sorted(batting_average) batting_average_sorted Output [45, 47, 47, 49, 55, 59] 5.2 Nested Tuple A tuple can have another tuple as an element. This is called 'nested' tuple.

nested tuple

n_tuple = (12040, 9115, ("Alex", "Evan"))

a. Use index to obtain each element in the tuple, including other tuples # Access element for each index print("Element[0] : ", n_tuple[0]) print("Element[1] : ", n_tuple[1]) print("Element[2] : ", n_tuple[2]) Output Element [0] : 12040 Element [1] : 9115 Element [2] : ('Alex', 'Evan') b. Access the nested tuples # nested indexes print("Element [2][0] : ", n_tuple[2][0]) print("Element [2][0] : ", n_tuple[2][1]) Output Element [2][0] : Alex Element [2][0] : Evan

6 Hands-on Exercise

6 Problem Statement Write the code snippets to show different ways of creating tuples for the following scenario. Scenario: Consider that you are using pandas to evaluate player details for making a fantasy cricket team called “My Super”. Here are the details:

  • Player - Name of the player
  • Team - Player belongs to which country
  • Matches - Total number of matches played
  • Innings - Total number of innings played
  • NO - Total number of NOT OUT
  • Runs - Total number of runs
  • HS - Highest score
  • Average - Batting average
  • 100s - Total number of hundreds scored
  • 50s - Total number of fifties scored Few experts have recommended the player details based on performance in different leagues. You have collected all the recommendations with records for each player. The following dataset shows the details: Player Team Matches Innings NO Runs HS Average 100s 50s Alex Team 1 251 242 39 12040 183 59 43 60 Henry Team 2 77 75 11 3580 125 55 12 16 Evan Team 1 224 217 32 9115 264 49 29 43 John Team 3 232 216 39 8574 181 48 21 51 William Team 4 126 122 3 4822 153 41 16 26 James Team 5 143 126 30 5507 185 47 12 35 Liam Team 6 123 121 6 5267 179 45 18 21 Oliver Team 4 151 144 14 6173 148 47 13 39

6.1 Solution Instructions a. Write a code to create the tuple of players ('Alex', 'Henry', 'Evan', 'John', 'William', 'James', 'Liam', 'Oliver') and assign it to the variable players_tuple # Write your code below Output b. Write a code to find the length of the tuple # Write your code below Output c. Write a code to access the element at index 3 # Write your code below Output d. Write a code to access the element at indexes 5,6,7(use slicing) # Write your code below

Copyright © 2021 Accenture All rights reserved. Accenture and its logo are trademarks of Accenture.

Was this document helpful?

Accenture 2 - Practice notes

Course: python programming (cs8151)

86 Documents
Students shared 86 documents in this course

University: Anna University

Was this document helpful?
Python Data Structure
Tuples
Hands-on Exercise