Python Data Structures PDF

Summary

This document explains the different data structures in Python, including lists, tuples, sets, and dictionaries. It also covers various methods and functions associated with each data structure. The document is suitable for learning or reviewing data structures in Python.

Full Transcript

way of organizing data so that it can be accessed more efficiently -- **Data Structure** That organize and group data according to type -- **Container** a collection which is ordered, changeable, and allows duplicate members -- **List** a collection which is ordered, unchangeable, and allows dupl...

way of organizing data so that it can be accessed more efficiently -- **Data Structure** That organize and group data according to type -- **Container** a collection which is ordered, changeable, and allows duplicate members -- **List** a collection which is ordered, unchangeable, and allows duplicate members -- **Tuple** a collection which is unordered, unchangeable, unindexed and does not allow duplicate members items are unchangeable, but items can be added or removed -- **Set** a collection which is ordered, changeable, and does not allow duplicate members -**Dictionary** just like the arrays declared in other languages, which is an ordered collection of data − very flexible because it can contain different data types -- **List** Adds an element at the end of the list -- **append()** Removes all the elements from the list -- **clear()** Returns a copy of the list -- **copy()** Returns the number of elements with the specified value -- **cout()** Add the elements of a list (or any iterable), to the end of the current list -- **extend** Returns the index of the first element with the specified value -- **index** Adds an element at the specified position -- **insert** Removes the element at the specified position -- **pop** Removes the item with the specified value -- **remove** Reverses the order of the list -- **reverse** Sorts the list -- **sort** a collection of Python objects like a list but are immutable in nature -- **Tuple** Returns the number of times a specified value occurs in a tuple -- **cout** Searches the tuple for a specified value and returns the position of where it was found -- **index** an unordered collection of data that is immutable and does not allow any duplicate element − used to include membership testing and eliminating duplicate entries − can contain different data types -- **Set** Adds an element to the set - **add()** Removes all the elements from the set - **clear()** Returns a copy of the set - **copy()** Returns a set containing the difference between two or more sets - **difference()** Removes the items in this set that are also included in another, specified set -**difference\_update()** Remove the specified item - **discard()** Returns a set, that is the intersection of two other sets - **intersection()** Removes the items in this set that are not present in other, specified set(s)-**intersection\_update()** present in other, specified - **set()** Remove the specified item -- **discard()** Returns whether two sets have a intersection or not - **isdisjoint()** Returns whether another set contains this set or not - **issubset()** Returns whether this set contains another set or not - **issuperset()** Removes an element from the set -- **pop()** Removes the specified element -- **remove()** Returns a set with the symmetric differences of two sets - **symmetric\_difference()** inserts the symmetric differences from this set and another **symmetric\_difference\_update()** Return a set containing the union of sets -- **union()** Update the set with the union of this set and others -- **update** An ordered collection of data values − holds the key:value pair key - an object which can never change like strings, numbers, tuples, etc. − cannot have two items with the same key -- **Dictionary** Removes all the elements from the dictionary -**clear()** Returns a copy of the dictionary -- **copy()** Returns a dictionary with the specified keys and value -- **fromkey()** Returns the value of the specified key -- **get()** Returns a list containing a tuple for each key value pair - **items()** Returns a list containing the dictionary\'s keys - **keys()** Removes the element with the specified key -- **pop()** Removes the last inserted key-value pair -- **popitem()** Returns the value of the specified key. If the key does not exist: insert the key, with the specified value - **setdefault()** Updates the dictionary with the specified key-value pairs **- update()** Returns a list of all the values in the dictionary -- **values()** used to declare a block − if the statements are on the same level of indentation, then they are on the same block -- Identation used to test a specific condition, if the condition is true, a block of code (if-block) will be executed − condition can be any valid logical expression -- **if Statement** if the condition in the if statement is false, it will execute the else statement -- **If-else Statement** checks multiple conditions and executes the block of code of the condition that evaluated to true − optional like else − can contain multiple elif after an if -- **Elif statement** allows the use of another if statements inside an if - **Nested** used for iterating on a sequence (either a list, a tuple, a dictionary, a set, or a string) − a set of statements may be executed once for each item, tuple, set, etc. − does not require an indexing variable to set beforehand -- **For** loop Can stop the loop before it has looped through all the items -- **Break Statement** can stop the current iteration of the loop, and continue with the next -- **Continue Statement** returns a sequence of numbers from 0 and increments by 1, by default, and ends at a specified number -- **range()** block of statements to be executed when the loop is finished cannot be executed if the loop is stopped by a break statement - **else in for Loop** a loop inside a loop − the inner loop will be executed once per iteration of the outer loop -- **nested loop** for loop cannot be empty -- **pass()** as long as the condition is true, it can execute a block of statements − needs to define an indexing variable increment/decrement this variable or the loop will not end -- **while loop** the loop can be stopped even if the while condition is still true - **break** stop the current iteration and continue with the next **- continue Statement** can run a block of code when the condition is no longer true - **else Statement** can run at least once − Python does not have a built-in do-while loop but possible to emulate - **do-while Loop** tests if a condition is true or false and returns the corresponding value, all in one line of code - **Conditional Operator** used to combine conditional statements -- **and or** used to reverse the result of the conditional statement -- **not** keyword used to define a function -- **def** information passed into a function − specified after the function name and inside the parentheses − separated by a comma - **Arguments or args** variable inside the parentheses in the function definition - **parameter** value sent to the function when called -- **argument** order of arguments does not matter -- **kwargs** function calls itself specifies the region where a variable can be accessed o local o global o nonlocal -- **recursion** created inside the add num function and can only be accessed within (local) -- **sum** keyword to create nonlocal variables o used in nested functions when local scope is not defined -- **nonlocal** Python has a built-in random module that can be used to generate random numbers -- a small anonymous function − can take any number of arguments, but can only have one expression - **Random Number** lambda arguments : expression - syntax

Use Quizgecko on...
Browser
Browser