|
CAO Apr 2020Python code:
Python’s a high-level language that’s easy to learn, read, and write. It’s popular, has a robust collection of third-party modules, and a large community willing to help. It’s friendly to multiple programming styles: procedural and functional; object-oriented, and not. Generally, it runs slowly compared to other languages like C, Go, Java, Julia, Rust, and Haskell. Python is versatile, and is especially popular for scientific computing and web development. It’s not suitable for systems programming, and is weak when making distributable stand-alone programs. What makes Python different from other languages? -Significant whitespace. In other words, you don’t use braces to indicate blocks of code, you use indentation. -Duck typing: Python doesn’t care about the actual type of the objects you’re working with as long as the object implements the correct attributes and methods. Python is also strongly typed, meaning that you can’t can add a string, a number, and a file object together to get some guessed-at result. -Extensive standard library. Python has a split userbase between (incompatible) versions 2 and 3. Many old code bases use v2. Highlights: The versions handle character encoding differently, v3 uses lazy evaluation in the standard library when possible, and v3 includes many new features. General links
Learn
Installing Python and third-party packages There are two main paths for installing Python. If you’re new, download Anaconda; it has many common third-party packages built-in. You can install additional packages later, with its conda package manager, or using the official one, pip. You can also install Python directly from the Official downloads page.The default links on this page are for x86-versions; browse to the OS-specific pages to find the 64-bit downloads. If you’re on Linux, you probably have Python installed already – perhaps multiple versions. Using virtual environments is especially important on Linux; altering the system python may break your OS. See the Virtual environments section below. Third-party packages can be installed with the built-in package manager pip. Run pip install packagename. Or create a text file of package names, and install with pip install -r requirements.txt. Anaconda users can install most packages with conda install packagename. Consider using Poetry or pipenv to install packages, rather than using pip and virtual environments separately. Use commands like this: pipenv install requests, or pipenv install, if you already have a pipfile established. (pipenv creates and manages this file automatically). Selected third-party packages
Virtual environments Using virtual environments lets you isolate a project's dependencies from your system's Python installation. This is important on Linux, where altering the system Python can break your OS, and useful on any OS to keep packages separate and easy-to-manage. Official python virtual environment reference. Consider using pipenv instead of this directly; it combines workflows for package management and virtual environments. You can access the environment from a terminal using commands such as pipenv shell, or pipenv run, followed by a command. Anaconda has its own virtual-environment tools. Scientific computing The Scipy Stack is a collection of modules that expand python’s numerical-computing capability. They work well together, but learning when to use each package can be tricky. The Python Data Science Handbook, linked below, can help. All of these packages can be installed with conda; most with Pip. Numpy: Provides a multi-dimensional array data type that allows fast vectorized options. Great for linear algebra, and nearly all numerical computing in Python relies on it. Scipy – A collection of unrelated tools, divided into submodules. Includes modules for statistics, Fourier transforms, scientific constants, linear algebra, signal processing, image manipulation, root-finding, ODE-solvers, and specialized functions (Bessel etc) etc. If you're considering implementing a common scientific operation by hand, check Scipy first. Matplotlib– Plotting. Robust and flexible. Has two APIs, both of which are awkward. Sympy – symbolic computation. Manipulate equations abstractly, take derivatives and integrals analytically, manipulate variables. Pandas – Used in statistics and data analysis; wraps Numpy arrays with labels and useful methods. Ipython / Jupyter. Exists as three related components: Ipython terminal: A powerful improvement over the default. Ipython QtConsole: Similar to the terminal app, but has advantages provided by a GUI. Notebook: Work on pages of mixed code, LaTeX, and text on redistributable web pages. Set to be replaced by a new project called Jupyter Lab Related: Scikit-learn is a machine-learning package with a simple API. Solves classification, regression, and clustering problems with a range of techniques. Tensorflow and Theano provide neural net frameworks. Code editors
Web development Python is great for server-side web development, when paired with one of these packages:
Warning: Diving into these packages directly can be challenging if you’re new to web development, since it requires proficiency in several skills. I recommend learning Python, database basics, HTML, CSS, and Javascript independently first. Check out Tango with Django for a Django tutorial. huhu posted a Flask intro in this thread. Heroku is a service that makes hosting Python websites easy; it has free plans for development, and can quickly scale up for production use. Microcontrollers Micropython allows you to run Python on embedded systems. Additionally, Python has good library support on single-board-computers like the Rasperry Pi. Alternatives For scientific computing: Julia is fast, and has more natural syntax for math and equations. Similar syntax to Python; it’s easy to pick up one if you know the other. code:
Python code:
Pure functional programming: Haskell has a steep learning curve, and is a robust functional language. Is a high-level langauge like Python. Statistics and data analysis: R. Specialized, popular language with a large collection of stats packages. Web development: Ruby on Rails is another high-level framework with nice syntax. Complementary languages
Expanding Python into new realms
Neat specialized tutorials
(By Randall Munroe) This OP’s a community effort; PM me for updates and edits. Dominoes fucked around with this message at 16:22 on Apr 23, 2020 |
# ¿ Mar 6, 2017 15:36 |
|
|
# ¿ Apr 29, 2024 13:33 |
|
You're catching the TypeError before it occurs; ie once you've gotten thing.foo, you have no more error checking, and 10 doesn't have any keys. @property makes methods look like properties; I think they're for writing Java-style code. I wouldn't use them, since they mask that calculations are being run. Boris Galerkin posted:Basically I want a dict/container object that has a default return value when I don't give it an index or key or whatever. If I do then I want it to return the value of that key, which internally in my class is saved as a dictionary. Python code:
Python code:
Dominoes fucked around with this message at 21:24 on Mar 8, 2017 |
# ¿ Mar 8, 2017 13:12 |
|
And I'd like to clarify: That type annotation's not accurate for the function I posted; I got confused part way through about what Boris wanted. Union[dict, int] means it could return either a dict or int. As posted, my example was really just int, or Union[Any, int] if you expect to pass it arbitrary dicts.
|
# ¿ Mar 9, 2017 16:00 |
|
I like being able to get an idea of what the function does by looking at its signature.
|
# ¿ Mar 9, 2017 20:41 |
|
Objects are nice if you're defining something that can be thought of as a datatype, or if you're using a collection of similar items.
|
# ¿ Mar 12, 2017 21:09 |
|
namedtuple unless it's mutable, or would benefit from methods.
|
# ¿ Mar 13, 2017 00:05 |
|
I'm mostly repeating what Eela and Epsilon said: It doesn't matter what tools you use at this point. Atom and Visual Studio Code are simple, good text editors. Spyder's an IDE that's easy to use; you could try that as well: 'pip install spyder' in a terminal. You might like it because it has a console built in, and buttons to run your code. Don't mess with PyCharm yet; it can be overwhelming while you're learning. Installing Anaconda will make installing third-party packages easier.
|
# ¿ Mar 28, 2017 03:02 |
|
Linked to huhu's post in OP.
|
# ¿ Mar 30, 2017 18:21 |
|
I've been using wrappers like this if I'm working with dataframes and the speed is a limitation. Lets you use Pandas' descriptive row and column indicies instead of integers, while maintaining the speed of working with arrays. Pandas DFs can be orders-of-magnitude slower than arrays.Python code:
Dominoes fucked around with this message at 12:09 on Mar 31, 2017 |
# ¿ Mar 31, 2017 12:06 |
|
funny Star Wars parody posted:I got promoted to developer recently and my first project has been building a file directory browser in php and Ajax and HTML and it's a fuckton to take in in two weeks so ya idk what it is with web stuff that is so hard to learn (probably all the acronyms and different languages/frameworks interfacing) but ya appreciate it! Flask looks like the way to go 😊 thanks! Re Flask vs Django: Django for websites, Flask for other things you need a webserver for. Flask becomes a pain once you start adding plugins for things like admin, auth, migrations, databases etc. Dominoes fucked around with this message at 02:35 on Apr 1, 2017 |
# ¿ Apr 1, 2017 02:32 |
|
Explicit is better than implicit...
|
# ¿ Apr 11, 2017 12:07 |
|
QuarkJets posted:Wouldn't that necessitate also specifying that the step size is 1, then?
|
# ¿ Apr 11, 2017 16:37 |
|
Whatever, nerd.
|
# ¿ Apr 11, 2017 17:37 |
|
Broad topic. Many python modules use C code to improve speed. When installing them from source, (Including pip without a wheel) you'll need a compiler; Visual C++ is used in Win. Which package are you referring to? You can usually avoid this by using Conda, or a Chris Gohlke binary. To speed up Python, you can use Cython, Numba, vectorized code, PyPy etc, on the bottlenecks. What specifically do you want to know?
|
# ¿ Apr 12, 2017 17:06 |
|
Whoah - I always assumed they leaked, and set my vars up so it wouldn't be an issue. FYI, javascript uses a 'let' command that explicitly makes a variable nonleaky.
|
# ¿ Apr 13, 2017 14:22 |
|
PyCharm doesn't like to work with files unless they're in a folder that includes PyCharm meta files (.idea directory). Maybe that's it?
|
# ¿ Apr 15, 2017 23:07 |
|
Cingulate posted:Different question: does Continuum make money? What are the chances "conda install all_the_software_i_need" won't work in 2018 because Travis Oliphant has to choose between making it slightly easier for me to set up numpy or feeding his kids? onionradish posted:As a Python user on Windows, I'd put Christoph Gohlke in the same "I hope nothing ever happens to him" category. His "Unofficial Windows Binaries for Python" site has been a project lifesaver on more than one occasion. Dominoes fucked around with this message at 18:46 on Apr 18, 2017 |
# ¿ Apr 18, 2017 18:43 |
|
Azuth0667 posted:I'm not sure what this would be called but, is there anything for python gui making that is similar to what VB.net has where you can place all the widgets on a template?
|
# ¿ Apr 21, 2017 20:02 |
|
XLSXwriter can do pretty-much everything, but it's missing some useful abstractions. Ie the code will be kind of brute force, iterating over row indexes etc. The API and docs's nice. I don't know how it compares to OpenPyxl; they're both popular, full-featured libraries; after researching which to go with, I ended up neutral, and picked writer arbitrarily. Those are the only two full-featured, modern libraries.
Dominoes fucked around with this message at 11:58 on Jun 1, 2017 |
# ¿ May 31, 2017 23:00 |
|
__init__ is boilerplate used in classes. Its most typical use is to convert arguments that are supplied when creating instances of the class into class-scope 'self' variables. Ie:Python code:
Python code:
Python code:
Python code:
Dominoes fucked around with this message at 12:18 on Jun 1, 2017 |
# ¿ Jun 1, 2017 12:09 |
|
Good point I missed: If you run_all() at the end without __main__, your code will run whenever it's imported. __main__ will only run if you load your script standalone.
|
# ¿ Jun 2, 2017 13:06 |
|
PyCharm and Anaconda aren't mutually exclusive. I apologize if I'm saying things you know already, but would you please describe exactly what steps you did to install the packages, then use them? To install packages, do this: code:
code:
Import and use packages like this: Python code:
Dominoes fucked around with this message at 13:38 on Jun 4, 2017 |
# ¿ Jun 4, 2017 13:34 |
|
LochNess - I don't know what the Anaconda prompt is, but by default, the Anaconda installer should add Python and related tools (pip, jupyter qtconsole etc) to your system path, exposing them to a normal terminal. Anaconda does include Python. If these aren't set up, do this: press winkey, type 'environment', press enter, click 'Environment Variables', edit Path, and add your Python scrips path... Someone who's using Ananconda can tell you what it is; for stock python, it's something like 'C:\Users\UserName\AppData\Local\Programs\Python\Python36\Scripts'. Open powershell or command prompt. Type 'pip list' or 'conda list'. What do you see?
|
# ¿ Jun 4, 2017 15:32 |
|
LochNessMonster posted:None of the commands (search / list / install ) give any output at all. Just a blank line and then a new prompt. code:
Do this exactly: -Press the win key -type 'powers'; press enter -type 'pip list'; press enter -Post the output here. Dominoes fucked around with this message at 16:50 on Jun 4, 2017 |
# ¿ Jun 4, 2017 16:43 |
|
Like baka said, don't worry about the Anaconda shell. Conda makes installing binary packages easy, and uniform across WIndows, Mac and Linux. Your alternative is to use installers from this site for packages pip gives you errors with. It's up to you whether you want to use conda or pip; try them out and make your own choice, or better yet, pick one and don't worry about it yet. I chose to stick with pip only, and ditched Anaconda, because dealing with two separate package managers was confusing; you still have to use pip for packages that aren't on conda.
|
# ¿ Jun 4, 2017 22:13 |
|
I recommend stop trying to run the anaconda shell (I assume that's what typing 'anaconda' in powershell does) until you sort the basics out. It seems like a big stumbling block, and is unnecessary. Viking - if you're right about pip being tied to a prev install, he can test by running 'python' in powershell, and checking if 'Anaconda' appears in the Python console's header. I think the best option is a clean install. Use the 'Add or Remove Programs' tool in Windows to remove everything with the name Python or Anaconda in it. Run either the Anaconda, or Python installer again, and we'll go from there if you have any issues. Fixing the path is easy (I posted instructions earlier for the non-Anaconda version), but I think it's best you start fresh. Dominoes fucked around with this message at 13:24 on Jun 5, 2017 |
# ¿ Jun 5, 2017 13:14 |
|
LochNessMonster posted:That last post was after a complete uninstall and reinstall of Anaconda. I checked if there were any Python related programs in the Programs/Features list but there weren't. Type 'python' in powershell. What do you see? I'm out of ideas.
|
# ¿ Jun 5, 2017 23:40 |
|
Loch - I've no idea why the Anaconda-specific commands aren't working.shrike82 posted:Do most of you run Python in Windows? Dominoes fucked around with this message at 12:16 on Jun 6, 2017 |
# ¿ Jun 6, 2017 12:14 |
|
Dex posted:were you trying to replace the system python version, because you really don't ever want to do that. building from source with configure, make and make altinstall then using python3.6/pip3.6 works fine for me on centos7. literally did this today as part of an automated setup for a vm i need to do stuff on Dominoes fucked around with this message at 14:32 on Jun 6, 2017 |
# ¿ Jun 6, 2017 14:30 |
|
I think the break involved trying to uninstall 3.5 or 3.6. Multiple versions on the system is awkward at best. In the future, I'd use Anaconda, which sets up things up nicely automatically, or virtual environs. There's probably a clean way to get 3.6 working in Ubuntu, but googling and troubleshooting for hours didn't do it; I'm classifying this one as difficult. It's comparatively straightforward in Windows, due to the lack of system python, and a nice official installer.
Dominoes fucked around with this message at 05:07 on Jun 7, 2017 |
# ¿ Jun 7, 2017 05:03 |
|
Your avatar is ridiculous.
|
# ¿ Jun 11, 2017 15:00 |
|
Eela6 posted:I agree. Differential equations are prone to all sorts of numerical analysis problems. Don't roll your own solutions - use a known stable algorithm. Dominoes fucked around with this message at 19:41 on Jun 17, 2017 |
# ¿ Jun 17, 2017 19:13 |
|
Update on the status of pip on windows: It works pretty good! Here's my requirements.txt; you need to install numpy+mkl, scipy, and llvmlite via Chris Gohlke installers (numpy on its own works fine in pip, btw); the rest will pip install, assuming you have C++ build tools 2015; the link to DL that shows up in the error you get for not having it installed.Python code:
Dominoes fucked around with this message at 05:17 on Jul 1, 2017 |
# ¿ Jul 1, 2017 04:23 |
|
Additionally, the builtin math.isnan and np.isnan work on several nan-like-types.
|
# ¿ Jul 4, 2017 18:47 |
|
Hadlock posted:For those who have used VS Code in the last ~6 months (the vscode product has changed/improved a lot since early last year) What's the delta between vscode and pycharm these days once you plugin the top two or three vscode python plugins? I've been using vscode now for a little over a year writing Go, Powershell, Bash etc and trying to see if the jump to pycharm CE is worth it for Python.
|
# ¿ Jul 27, 2017 18:12 |
|
Like, Intel Inside and Gateway2000 stickers?
|
# ¿ Sep 2, 2017 06:49 |
|
Is there a package out there that simply requires the scipy stack? (numpy, scipy, pandas, jupyter, sympy, matplotlib) Like django-toolbelt. I didn't find anything. edit: There is now. Dominoes fucked around with this message at 21:22 on Sep 12, 2017 |
# ¿ Sep 11, 2017 01:37 |
|
Thirding Anaconda; a stock installation should do.
|
# ¿ Sep 14, 2017 21:58 |
|
Thermo and public each gave you a valid answer; this is a pain point in Anaconda: You have two separate package managers, and packages may need to be installed in a mix of the two.
|
# ¿ Sep 15, 2017 14:04 |
|
|
# ¿ Apr 29, 2024 13:33 |
|
Hey dudes, I set up some Powershell scripts to automate installing a calculator-like virtual env. Running the setup script does the following: -Creates a virtual environment in the directory you downloaded the scripts -Installs the scipy stack, PyQt, and Spyder. (Downloads Scipy and its numpy+MKL prereq from Chris Gohlke's site, then the rest from PyPi. See this for why this is currently necessary; something to do with a Fortran compiler license) -Setups up shortcuts that run Qtconsole or Spyder in that env. I made bash scripts to do the same, but can't test it since my Ubuntu version has broken venvs. Might try setting up a special ipython config to automatically import numpy as np etc, but haven't figured out how to do that on a per-env or per-instance basis. Stack Overflow post I made describing my troubles here. Related: Spyder feels appropriate for this type of use; I didn't give it a chance before, but it's great for writing and running one-off scripts for solving math and science problems. Dominoes fucked around with this message at 16:21 on Sep 22, 2017 |
# ¿ Sep 22, 2017 02:47 |