This section covers the core concepts of Python, including language fundamentals, built-in data types, and variables. Understanding these basics is crucial for anyone looking to build a solid foundation in Python programming.
12 audio · 2:57
Nortren·
What are the main built-in data types in Python?
0:14
The main built-in types are: numeric types int, float, complex, and bool; sequence types list, tuple, range, and str; mapping type dict; set types set and frozenset; binary types bytes, bytearray, and memoryview; and the special types None and NotImplemented.
What is the difference between a list and a tuple?
0:14
Lists are mutable, tuples are immutable. Lists use square brackets, tuples use parentheses or no brackets at all. Tuples are slightly more memory efficient and can be used as dictionary keys or set elements because they are hashable. Lists support more operations like append and extend.
Use a tuple when the collection has a fixed structure and should not change. Tuples express intent that the data is constant. They are appropriate for representing records, returning multiple values from a function, and as dictionary keys. Lists are for collections that grow or change.
A dictionary is a mutable mapping from hashable keys to arbitrary values. It is implemented as a hash table internally. Lookups, insertions, and deletions are average constant time. Since Python 3.7, dictionaries preserve insertion order as part of the language specification.
An object is hashable if it has a hash value that does not change during its lifetime, and if it can be compared to other objects. Hashable objects must implement both dunder hash and dunder eq. Immutable built-in types are hashable, while mutable collections like lists and dicts are not.
What is the difference between a set and a frozenset?
0:11
A set is a mutable, unordered collection of unique hashable elements. A frozenset is the immutable version of a set. Frozensets are hashable themselves, so they can be elements of other sets or used as dictionary keys, while regular sets cannot.
Sets are implemented as hash tables, similar to dictionaries but storing only keys. This gives average constant time lookup, insertion, and deletion. Sets are ideal for membership testing and removing duplicates from a sequence.
None is a singleton object representing the absence of a value or a null value. It is the default return value of functions that do not explicitly return anything. There is exactly one None object, so it should always be compared with the is operator, never with double equals.
What is the difference between range and xrange in modern Python?
0:16
In Python 3, xrange does not exist. The built-in range returns a lazy range object that generates values on demand, similar to Python 2 xrange. The Python 2 list-returning range was removed entirely. The current range is memory efficient even for very large numbers.
In Python 3, strings are sequences of Unicode code points and are stored as immutable objects. Bytes are a separate type for raw binary data. The conversion between them requires explicit encoding and decoding. Python 3 enforces this distinction strictly to avoid ambiguity.
F-strings, formally called formatted string literals, are strings prefixed with f or F that allow embedded expressions inside curly braces. They were introduced in Python 3.6 and are the recommended way to format strings. They are faster than the percent operator and the format method.
What are template strings introduced in Python 3.14?
0:21
Template strings, called t-strings, were introduced in Python 3.14 via PEP 750. Unlike f-strings, which immediately produce a regular string, t-strings produce a Template object containing both the static parts and the interpolations. This lets you process, escape, or transform values before rendering, which is valuable for SQL, HTML, and shell command construction with proper escaping.
---