Follow TV Tropes

Following

Media Notes / Python

Go To

https://static.tvtropes.org/pmwiki/pub/images/pythonthrow.png

Python is an interpreted, dynamically typed programming language. That means its programs are text files. To execute a Python program, you run a program called an interpreter that reads a text file full of Python code, and does what it says, and as long as an interpreter is available for one's platform of choice (UNIX-based or UNIX-like operating systems typically come with one and the developers maintain ports to Windows and macOS) the operating system and architecture are largely irrelevant since compilation isn't required. The language's name is a Line-of-Sight Name derived from Monty Python's Flying Circus; no relations to the snake.

Traditionally, interpreted languages like Python are not used to develop whole applications. Partly because the process of interpreting code is slower than the process of running code that's already been compiled into machine language. This is much less true than it once was, because after running a program once, Python stores it in "bytecode" that's been almost, but not quite, compiled into machine code. But for the most part, it's because interpreted programming languages have lacked access to the libraries that compiled programming languages had. Libraries like, say, OpenGL, which allows C++ programmers to render fancy graphics without first telling the computer what a sprite is, and how to move it around.

It is in this last area that Python breaks the mold. Python has extensions that are written in other languages, but unlike those previous languages, Python extensions can be generated automatically from existing C code, and with only a little extra work, you can use Python itself to tidy up the interface so that people who don't like C can use the library anyway.

Python therefore supports a bafflingly large array of functions, which Python programmers can tie together to make whole new programs without touching C note .

Python is distinguished by its near-complete lack of braces. Where other programming languages require you to partition your blocks of code using punctuation, Python looks at how it's indented. Other languages are customarily indented to indicate how they're organized, but in Python, that's an actual rule of the language that you have to follow, or your program won't work. This frequently makes Python programs more readable than their equivalents in JavaScript and the like, and readability is one of the goals of the language, as mentioned on the Programming Language article.

The language takes pains to prevent the programmer from having to worry about memory management: it has an automatic garbage collector, meaning you don't have to worry about deleting variables after you're done with them; all variables are technically "pointers," but some types of variable can't be modified after they're created, which prevents issues that arise in C++ and the like where you don't know whether you're dealing with a variable or its Evil Twin; it handles type conversion automatically, meaning the difference between 2, 2.0 and "2" is only as important as you want it to be; and the variable declarations that are used in most other languages to define what type of data a variable will hold are entirely absent from Python, where you "declare" a variable by assigning a value to it. Because of the ease of programming in Python and its clean syntax, it's replaced BASIC as the first language of many new coders, as well as Pascal and LISP in introductory university computer science courses. Cementing its status as a Spiritual Successor to BASIC, Python is ubiquitous on modern graphing calculators. It's also popular for scientific computing with the NumPy, SymPy, and SciPy libraries, giving Mathematica and MATLAB a run for their money.

Note that while the current versions of Python (the 3.x branch) are recommended for most purposes, code and examples can still be found which are written for the older 2.7 branch, which was supported as late as 2020. This distinction will seldom affect online examples, but when investigating physical books, it is worth checking that it uses the current syntax and features. Fortunately, it isn't hard to find books that cover both; for example, the popular computer book publisher O'Reilly Media currently covers both branches in its books, Learning Python (which is intended for newcomers to the language) and Programming Python (which is aimed at more advanced Python programmers).



    open/close all folders 
    Code Examples 

Hello Python

print("Hello World!")
# Outputs Hello World! to the console

Short and sweet syntax like this is why Python is beloved.

Variable Declaration and Concatenation

a = 10
b = 7
c = "Bob"

print(a + b)
# Outputs 17

print(b + c)
# Python will throw an error. You can't add a number and a string together
# The correct way would be print(str(b) + c), which outputs 7Bob

print("Hello, " + c)
# Outputs Hello, Bob

Unlike other languages, Python will not try to concatenate a number type and a string type together. Like other languages, the + operator is still overloaded with adding numbers and concatenating strings

For Loops

for i in range(0, 10):
print(i)
The program will output numbers 0 through 9, but not 10 — and the 0 in the parentheses can be dropped and it'll execute the same way. This code example also highlights Python's famous bracket-less syntax. Python uses a colon (:) instead of a left bracket ({) and uses the code's formating to figure out the rest.

While loops

i = 0
while i < 10:
print(i)
i += 1
In addition to for loops, Python also has while loops, which execute what's in them so long as the condition remains true. This snippet likewise prints out the numbers 0 through 9.

Popular Python Libraries and Resources

    3D graphics engines 

    Other 

Works that are written in Python

    Video Games 
    Other 
  • EventScript 2.0, a plugin for Valve's Source Engine
  • Some servers for Ace Of Spades use a Python plugin.
  • Blender is a free and open source 3D creation application written in Python.
  • Reddit is written in Python, using the Pylons framework.
  • Instagram is written in Django, a Python framework for websites and web apps.
  • Pinterest and LinkedIn use Flask, another Python framework for websites and web apps.

"Python for Software Design: How to Think like a Computer Scientist" is a rather good, freely available instructional book, and W3Schools.com has extensive and detailed information on the language.


Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
Tim Peters' Zen of Python, a set of guiding principles for the language

Top