Python
In short
Python is a general-purpose, dynamically typed programming language known for readable syntax and wide use in data science, automation, and web backends.
What is Python?
Python is a high-level, general-purpose programming language created by Guido van Rossum and first released in 1991. It is known for readable code that uses indentation instead of curly braces to mark blocks, and for a large standard library that covers everything from file handling to web requests. Python 3 is the current version; Python 2 reached its end of life in 2020.
Python is dynamically typed, meaning variables don't have declared types and type checks happen while the program runs. It is also strongly typed, so '3' + 4 raises an error instead of silently converting one of the values. Optional type hints, such as def greet(name: str) -> str, can be checked by separate tools but are ignored by the interpreter at runtime. Memory is managed automatically: the standard implementation, CPython, uses reference counting plus a garbage collector that cleans up reference cycles.
Python is used for data analysis, machine learning, scientific computing, automation scripts, web backends, and teaching programming. Its code is often described as reading almost like pseudocode, which makes it a common first language. A huge ecosystem of third-party packages, installed with the pip tool, extends it into almost every field.
Python is often called an interpreted language, which is only partly accurate. CPython first compiles source code into bytecode and then runs that bytecode in a virtual machine, similar in spirit to Java, but Java's JVM compiles hot code to machine code while CPython mostly interprets it, so pure Python code usually runs slower. For heavy number crunching, popular Python libraries do the actual work in compiled C, C++, or Rust code underneath.
Key takeaways
- Python uses indentation to define code blocks, which keeps code visually consistent.
- It is dynamically but strongly typed, with optional type hints.
- Memory is managed automatically through reference counting and garbage collection.
- It is widely used for data science, machine learning, automation, and web backends.
Example
# Count how often each word appears
from collections import Counter
text = "the quick brown fox jumps over the lazy dog the end"
counts = Counter(text.split())
for word, count in counts.most_common(3):
print(f"{word}: {count}")Readers ask
Is Python good for beginners?
Python is one of the most common first languages because its syntax is short and readable and its interactive shell gives instant feedback. The concepts you learn, such as variables, loops, and functions, carry over to other languages.
Is Python compiled or interpreted?
Both, in a sense. The standard implementation, CPython, compiles source code to bytecode and then runs that bytecode in a virtual machine, so from a developer's point of view it behaves like an interpreted language.
What is the difference between Python 2 and Python 3?
Python 3 is the current version and is not fully backward compatible with Python 2; for example, print became a function and strings became Unicode by default. Python 2 reached end of life in January 2020, so new code should use Python 3.
Often compared
- Python vs JavaScriptPython is a general-purpose language known for readable code and its data and AI libraries, while JavaScript is the web's language, run by browsers and Node.js.
- Python vs JavaPython is a concise, dynamically typed language popular for data, AI and scripting, while Java is a statically typed JVM language for enterprise and Android.
See also
- Data TypeProgramming Fundamentals, p. 14A data type is a classification that tells a program what kind of value a piece of data holds, such as a number or text, and which operations work on it.
- Garbage CollectionProgramming Fundamentals, p. 23Garbage collection is automatic memory management in which the language runtime finds data a program can no longer use and frees that memory for reuse.
- CompilerProgramming Fundamentals, p. 11A compiler is a program that translates source code written in a programming language into a lower-level form, such as machine code, that a computer can run.
- Machine LearningAI & Machine Learning, p. 27Machine learning is a branch of artificial intelligence in which computers learn patterns from data to make predictions instead of following hand-written rules.
- JavaScriptWeb Development, p. 24JavaScript is the programming language that runs in web browsers to make pages interactive, and it also runs on servers through runtimes like Node.js.
- DjangoBackend & APIs, p. 11Django is an open-source Python web framework that comes with an ORM, an admin panel, authentication and security protections, so teams can build quickly.
- FastAPIBackend & APIs, p. 17FastAPI is a modern Python API framework that uses standard type hints to validate requests, convert data and generate OpenAPI documentation automatically.
- FlaskBackend & APIs, p. 18Flask is a lightweight Python web framework offering routing, request handling and templates, leaving the database and project structure to the developer.
Spotted a mistake or something missing on this page?Suggest an edit