Anna GE 3151 - UNIT IV: LISTS, TUPLES, DICTIONARIES

Unformatted text preview:

UNIT IV LISTS TUPLES DICTIONARIES Lists Lists are ordered collections of items which can be of different data types 1 List Operations Creating lists my list 1 2 3 Accessing elements my list 0 zero based indexing Slicing my list 1 3 Concatenation list1 list2 Repetition 1 2 3 2 List Methods append item Adds an item to the end of the list insert index item Inserts an item at a specific index remove item Removes the first occurrence of the item pop index Removes and returns the item at the specified index sort Sorts the list in ascending order reverse Reverses the order of the list Iterating over a list using a for loop Lists are mutable meaning their elements can be modified after creation Aliasing Multiple variables referring to the same list Cloning Creating a new copy of a list 3 List Loop 4 Mutability 5 Aliasing and Cloning 6 List Parameters Lists can be passed as arguments to functions and modified within the function Tuples Tuples are ordered collections of items similar to lists but they are immutable cannot be modified after creation 1 Tuple Assignment Creating tuples my tuple 1 2 3 Tuple unpacking x y z my tuple 2 Tuple as a Return Value Functions can return tuples to provide multiple values Dictionaries Dictionaries are unordered collections of key value pairs where keys must be unique and immutable e g strings numbers tuples 1 Dictionary Operations Creating dictionaries my dict apple 2 banana 3 Accessing values my dict apple Adding modifying values my dict cherry 5 Removing values del my dict banana 2 Dictionary Methods keys Returns a view object containing the dictionary s keys values Returns a view object containing the dictionary s values items Returns a view object containing the dictionary s key value pairs get key default Returns the value associated with the key or the default value if the key is not found Advanced List Processing List Comprehension allows you to create a new list by applying an expression to each item in an existing list Example squares x 2 for x in range 1 6 Illustrative Programs 1 Simple Sorting 2 Histogram 3 Student Marks Statement 4 Retail Bill Preparation


View Full Document

Anna GE 3151 - UNIT IV: LISTS, TUPLES, DICTIONARIES

Download UNIT IV: LISTS, TUPLES, DICTIONARIES
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view UNIT IV: LISTS, TUPLES, DICTIONARIES and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view UNIT IV: LISTS, TUPLES, DICTIONARIES 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?