
Explore Python best practices focused on built-in data types—lists, tuples, sets, and dictionaries—with author James Powell, covering basics and practical applications.
Learn how to access and organize your working files for the Python best practices course, including downloading, extracting a zip, and saving them to your desktop.
Explore how to choose and use Python's dict type idiomatically, leveraging built-in data structures and the standard library to write clearer, more efficient code.
Explore idiomatic dictionary use in Python while solving two problems: implementing a visioneer cipher for encryption and decryption, and computing a concordance or top 10 words from Paradise Lost.
Explore how Python dictionaries relate to look-up tables, maps, and other names across languages, revealing similar features to guide idiomatic and effective use.
Discover how Python dictionaries map keys to values with a literal using curly braces, retrieve items via square brackets, and handle key errors when a key is missing.
Explore how dictionaries act as maps by linking unique keys to values. Build mappings from courses to professors and from students to courses using dict literals and sets.
Learn to create dictionaries in Python using curly braces, the dict constructor with keywords, or a collection of key-value pairs; ensure keys are hashable and immutable, while values remain flexible.
In Python best practices, dict.update merges another mapping into a dictionary by iterating keys and overwriting existing values while adding new key-value pairs.
Discover the idiomatic use of Python dictionaries by inspecting their public methods. Filter out double-underscore attributes to reveal the core methods for idiomatic dictionary usage.
Learn how Python's dict.clear mutates a dictionary by removing all entries without destroying the dictionary object, unlike reassigning the reference, and how id shows its identity.
Explore how dict.copy creates a shallow copy of a dictionary, copying keys but keeping values as references, and how the copy module provides a deep copy function for nested structures.
Learn to retrieve dictionary values with square brackets or the get method, decide when to provide a default to avoid key errors, and compare with try-except for idiomatic handling.
Retrieve and remove dictionary items with dict.pop and dict.popitem, optionally providing a default value; note that popitem returns an arbitrary entry since dictionary contents are not sorted.
Explore iterating dictionaries with keys, values, and items, and compare Python 2 and Python 3 behavior. Learn to avoid mutating dictionaries and use a set to delete keys.
Demonstrate how dict.setdefault initializes a missing key to an empty set, returns the existing or new value, and adds a course in a single idiomatic line.
Explore Python dict best practices by using the __missing__ method to customize lookups, create a case-insensitive dictionary subclass, and implement terse, idiomatic behavior for missing keys.
Master dict comprehensions to build dictionaries with keys and values, including squaring numbers and optional predicates. Apply codebook creation and reversal for a substitution cipher using ASCII lowercase and join.
Explore how a lookup table and dictionaries power function evaluation, including pre-computed sine values from -pi to pi, interpolation, and an interpolating dictionary that auto adds interpolated results.
Explore how a Python dictionary can act as a relation or function, offering a mapping of keys to values, and how __missing__ and memoization with decorators realize custom logic.
See how the getitem and __call__ protocols let Python objects respond to bracket and parentheses syntax, showing dictionary lookup, simple computations, and the similarities between item and call semantics.
Build a pandas dataframe with 0 to 10,000 in a column, save it to an hdf store, and inspect the stored frame, illustrating dictionary-like semantics via the getitem protocol.
Explore range dict and passthrough dict concepts in Python, implementing a content-addressable dictionary that maps by numeric ranges and passes through missing keys for censorship-style lookups.
Explore python's defaultdict from the collections module, which auto-creates a default value for missing keys using a factory, like an empty set, enabling seamless updates.
Explore how Python dictionaries are implemented as hash tables and why keys must be immutable, then compare hash-based lookups to linear list indexing and bisect-based binary search, with constant-time implications.
Explore the difference between dictionaries and objects in Python through attribute and item lookups, and weigh semantics against timings when modeling data.
Explore the semantics of objects versus dictionaries in Python to understand when dictionaries provide simple modeling, and how get item protocol, chain map, and MRO shape behavior.
Explore how Python handles attributes and dictionary keys, learn about attribute dictionaries that support dot and bracket lookups, and discover safer alternatives to avoid memory cycles.
Demonstrates building a Vigenere cipher encoder with a two-level dictionary codebook, cycling the key, and encoding and decoding letters while stripping non-letter characters.
Demonstrate building a concordance by counting word frequencies and extracting the top 10 in Paradise Lost text, using utf-8-sig encoding, lowercasing, regex cleanup, and counter-based approaches.
Explore the Python set type to store unique elements and perform unions, intersections, and differences while building a simple prime finder using trial division up to a maximum.
Describe the Python set type as a unique, unordered collection of hashable items, enabling intersection, difference, and union operations and highlighting its similarities to dictionaries.
Learn how to construct sets in Python using literal syntax and the set constructor, from iterables, handling duplicates, including empty sets, adding elements, and building a 52-card deck.
Understand that sets have no indexing or ordering, unlike lists and dictionaries. See how this absence blocks using random choice and shuffle on sets, requiring alternative implementations.
Build a five-card hand from a deck without mutating it. Use two loops and pop, then unite hand and board with | and union().
Explore set operations in Python: add elements to a set and preserve uniqueness, then compare remove versus discard: remove errors on missing items, while discard safely ignores absent elements.
Explain Python set comprehension, resembling builder notation to create strings like 'two of spades' by iterating ranks and suits. Show how add and update extend deck with jokers from iterables.
Build a poker deck as suit and rank tuples using Python sets and frozensets, then identify valid straights to evaluate hands like straight flush, four of a kind, and flush.
Write a best_hand function that evaluates a poker hand using set comprehension, group by, and itertools combinations to detect straights, flushes, and kinds, returning the best hand.
Create a best hand function to evaluate poker hands, mapping rank to value for numbers and face cards with aces high, and supply helpers to pick best and worst cards.
Test code runs every five-card combination from a 52-card deck through a best hand function to determine poker hand, including straight, three of a kind, two pairs, and one pair.
This lecture demonstrates displaying example poker hands with code that prints hand type and cards, using examples like one pair, straight flush, full house, flush, two pairs, and high card.
Explore seven additional set methods, clear, difference, intersection, and symmetric difference, and their updates and augmented assignments, with operator overloading and practical examples.
Explore prime number generation in Python using the set type to implement trial division, set operations, and a generator-based approach that yields primes.
Explore the list and tuple types in Python, compare their operations and semantic differences, and apply criteria to choose the appropriate type for the given problem.
Explore simple problem statements using lists and tuples to build a personal rolodex or phone book, with operations to insert, remove, and retrieve entries by name or detail.
Explore the Python list type as a mutable, homogeneous collection and sequence with order preserved, enabling iteration, aggregate operations, and dynamic add or remove changes.
Explore the tuple type as a fixed-size, immutable, heterogeneous collection that preserves element positions like records or coordinates, contrasting with mutable lists and showing how to extract elements.
Construct and manipulate Python lists and tuples using literal syntax, including square brackets for lists, and use the list function with iterables like strings and range objects, creating empty lists.
Learn to create tuples in Python using literal syntax or the tuple() function, including one-item tuples with a trailing comma, and representing a person's name and birthday.
Explore list comprehensions and generators, explain that tuple comprehensions don’t exist, and show how to build tuples with tuple() from a list comprehension or a generator expression, including even squares.
Use square brackets to index and slice Python lists and tuples, starting at zero to retrieve items. Slice syntax supports start, stop, and step with sensible defaults, including reverse order.
Set items in lists by assigning to an index or a slice. Explain that tuples are immutable, so set item syntax cannot modify them.
Tuples are immutable and do not support item assignment; explore corner cases with mutable items inside a tuple and augmented assignment affecting the inner list.
Learn how to get and set items, delete by index or slice in lists, and contrast tuples that cannot be changed with lists that can grow, shrink, and reassign elements.
Explore iteration for lists and tuples in Python, using for loops, slicing, and enumerate, and learn why modifying a collection during iteration can skip items and is best avoided.
Explore Python unpacking for tuples and lists, including multi assignment, swaps, and the requirement that left and right sides share the same number of elements.
Learn how Python lists and tuples use addition and multiplication to concatenate and repeat elements, explore augmented assignment, and distinguish mutable lists from immutable tuples.
Learn argument unpacking in Python by feeding a three-element list or tuple to a function that takes x, y, and z, and avoid errors from few or many arguments.
Explore how count and index work on lists and tuples, counting element occurrences and locating the first index, with apples, bananas, and a tuple of names including missing-element behavior.
Explore list-specific methods like append, clear, extend, insert, pop, remove, and reverse, while understanding how lists differ from immutable tuples and how to use sort, sorted, and reversed.
Explore semantics of lists and tuples in Python, distinguishing fixed-size records from dynamic collections, using unpacking, heterogeneous vs homogeneous data, and how sets affect order and uniqueness.
Explore Python's collections.namedtuple to create named-field records and access fields by name. See a practical example counting years after leaving office and visualizing with histograms.
Explore Python data structures for trees and traversals, using lists and deques to implement preorder and level-order traversals with stacks and queues, and validate palindromes with a deque.
Explore collections deque as a stack and implement a balanced parentheses and brackets checker in Python using a stack, dictionary mappings, and a final empty stack check.
Learn to use the Python heapq module to transform a list into a heap, push elements, and pop smallest or largest values efficiently for priority-based scheduling.
Review built-in Python datatypes—lists, tuples, sets, and dictionaries—focusing on mutability, ordering, and their roles in sequences, records, lookups, and memoization.
This Python Best Practices training course, covers the built-in data types in Python. This course is designed for beginning to intermediate Python users.
You will start by learning about data structures, including the dict type, problem statements, terms, and how to create a dict. From there, the author will teach you how to interact with the set type, such as type constructing, comprehension, and indexing. Finally, this video tutorial covers list and tuple types, including the list type, how to create a list and tuple, and semantics.
Once you have completed this computer based training course, you will have gained a solid understanding of the built-in data types in Python. Working files are included, allowing you to follow along with the author throughout the lessons.