Python sort() vs sorted(): What Is the Difference?
sort() sorts a list in place and returns None, while sorted() returns a new sorted list from any iterable. See examples with key, reverse, and stability.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
sort() sorts a list in place and returns None, while sorted() returns a new sorted list from any iterable. See examples with key, reverse, and stability.
append adds its argument as a single element, while extend adds each item of an iterable. Learn the differences with clear examples and pitfalls.
A list is mutable and a tuple is immutable. See how they differ in syntax, hashing, memory and use cases, with examples you can run.
exec() runs code from strings in every Python version, but execfile() existed only in Python 2. Here is how to run script files in Python 3 instead.
eval() evaluates one expression and returns its value, while exec() runs statements and returns None. See examples, namespace control and safer alternatives.
execfile() was a Python 2 built-in that ran a file in the current namespace. See why it is gone in Python 3 and how exec, runpy and importlib replace it.
Understand how Python's exec() handles namespaces, captures output and builds functions dynamically, plus safer alternatives and a short security checklist.
compile() turns Python source or an AST into a code object for exec() or eval(). See the three modes, practical uses, speed benefits and why untrusted input is unsafe.
execfile() is a Python 2 built-in that ran a source file in a namespace. Python 3 removed it; use runpy.run_path, importlib or exec(compile()) instead.
exec() runs Python statements from a string or code object. Learn its syntax, how namespaces work, how it differs from eval, and why untrusted input is dangerous.
eval() evaluates a Python expression from a string, but it executes arbitrary code. See how it works, why it is unsafe with user input and what to use instead.
locals() returns the current local namespace as a dictionary. See how it behaves in functions, modules and classes, what changed in Python 3.13, and safer alternatives.
Learn what Python's globals() returns, how to read and modify global variables with it, and when the global statement is the better choice.
The vars() function returns an object's attribute dictionary. Learn how it differs from dir() and locals(), and when it raises TypeError.
callable() returns True when an object can be called like a function. See which objects qualify, how __call__ works, and how to validate callbacks safely.
How Python's oct() converts integers to octal strings, how to parse and format octal values, and where octal is still used.
Learn how Python hex() converts integers to hexadecimal strings, how to pad or drop the 0x prefix, handle negatives and convert hex back to int.
bin() turns an integer into a 0b-prefixed binary string. See padding, negatives, int(text, 2) and bit-flag examples.
chr() turns an integer Unicode code point into a one-character string and is the inverse of ord(). See the valid range, errors, and practical uses with examples.
ord() returns the Unicode code point of a single character, and chr() reverses it. See syntax, practical examples, common errors and edge cases.