Install Plotly in Anaconda

If you don’t care which version of Plotly you install, just use pip. pip install plotly is an easy way to install the latest stable package for Plotly from PyPi. pip is a useful package and dependency management tool, which makes these things easy, but it should be noted that Anaconda’s conda tool will do the same thing. pip will install … Read more

“for loop” with two variables?

If you want the effect of a nested for loop, use: If you just want to loop simultaneously, use: Note that if x and y are not the same length, zip will truncate to the shortest list. As @abarnert pointed out, if you don’t want to truncate to the shortest list, you could use itertools.zip_longest. UPDATE Based on the request for “a … Read more

How to use pygame.KEYDOWN to execute something every time through a loop while the key is held down?

Use pygame.KEYDOWN and pygame.KEYUP to detect if a key is physically pressed down or released. You can activate keyboard repeat by using pygame.key.set_repeat to generate multiple pygame.KEYDOWN events when a key is held down, but that’s rarely a good idea. Instead, you can use pygame.key.get_pressed() to check if a key is currently held down:

Different meanings of brackets in Python

[]: Lists and indexing/lookup/slicing Lists: [], [1, 2, 3], [i**2 for i in range(5)] Indexing: ‘abc'[0] → ‘a’ Lookup: {0: 10}[0] → 10 Slicing: ‘abc'[:2] → ‘ab’ (): Tuples, order of operations, generator expressions, function calls and other syntax. Tuples: (), (1, 2, 3) Although tuples can be created without parentheses: t = 1, 2 → (1, 2) Order of operations: (n-1)**2 Generator expression: (i**2 for i in range(5)) Function or method calls: print(), int(), range(5), ‘1 2’.split(‘ … Read more

How to read pickle file?

Pickle serializes a single object at a time, and reads back a single object – the pickled data is recorded in sequence on the file. If you simply do pickle.load you should be reading the first object serialized into the file (not the last one as you’ve written). After unserializing the first object, the file-pointer … Read more