Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
What are some of the most basic things that a programmer who is used to coding in MATLAB needs to be cautious about, when beginning to code in Python? I am not asking for an entire list of differences between MATLAB and Python, just a few very basic things that can save a beginner a few hours of debugging.
An example would be how someone used to MATLAB's 1:N may make a mistake in python by using range(1,N)
Differences in concepts would be more helpful than absolute differences in specific commands.
The NumPy site has an excellent list, which I won't reproduce in full here. NumPy provides basically all the basic functionality of MATLAB for Python users, so it is highly recommended if you're coming to Python from MATLAB.
Link
Some highlights:
INDEXING: MATLABĀ® uses one based indexing, so the initial element of a sequence has index 1. Python uses zero based indexing, so the initial element of a sequence has index 0. Confusion and flamewars arise because each has advantages and disadvantages. One based indexing is consistent with common human language usage, where the "first" element of a sequence has index 1. Zero based indexing simplifies indexing. See also a text by prof.dr. Edsger W. Dijkstra.
LOGICOPS: & or | in Numpy is bitwise AND/OR, while in Matlab & and | are logical AND/OR. The difference should be clear to anyone with significant programming experience. The two can appear to work the same, but there are important differences. If you would have used Matlab's & or | operators, you should use the Numpy ufuncs logical_and/logical_or.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 months ago.
Improve this question
Im just learning about pythons bitwise operator << and >>. as far as I see, it takes the binary version of an integer and shifts it n places left or right. that would mean that saying x<<y is equivalent to x*(2**y)
so my question is why is there an operator for this? as far as I know python doesnt like to give you more than 1 way of doing things to avoid confusion. is there a reason this operator is particularly useful or typical scenerios where its used? I know this is a pretty open ended question but when searching for this I only come across what this operator does, not why we would use it. thankyou in advance
The key is in your remark "it takes the binary version of an integer and shifts it n places left or right".
Ask yourself this: how does your computer represent integers at all? What are integers? Any integer is a sequence of bits, (typically a multiple of 8 bits, i.e. a byte) and your computer is built around memory positions, registers, addresses, etc. that hold these integer values.
So, it makes sense for a CPU to have an operation to shift such a value left or right by one bit, for an extremely fast multiplication or division by 2, more so since powers of two are very commonly needed because everything in your computer is binary.
Other operations can be composed from simple addition, subtraction, shift by n, etc. - Python exposes this operation to give you access to this very basic and quick operation, although Python integers aren't always (or even all that often) the same efficient integers you operate on directly in many other languages.
But bit-shifting has many applications, and as a standard operation of your computer, it only makes sense that Python would give you access to a tool that programmers are very used to, and have applications for in many common algorithms.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Is there a way to do the same in C++?
Python
int.__add__(1, 1)
I can make something in C++, but it is still ugly and not very optimised and short.
I just want this : 1+1, but without + .
I absolutely want to use the int "class".
It's for matrix multiplication, addition, and subtraction...
I just want to have better-looking and shorter code.
I can make something in C++, but it is still ugly and not very optimised and short.
Last time I checked, int.__add__(1, 1) was way longer than 1+1. Just about anything is longer than 1+1. As for optimisations, you are not in a position to talk about what is more optimised, having not measured anything.
It's for matrix multiplication, addition, and subtraction
The same integer + operator is useful in lots of contexts. Matrix operations are among them. There is no need to single them out.
I just want this : 1+1, but without +
What you want is of secondary importance. Programming is a team sport. You do what everyone else does. Not only because it is likely to be tried and tested and the best thing after the sliced bread, but also because if everyone is doing something in a particular way and you are doing it differently, others will find it hard to understand what you mean.
This is not to say you cannot break conventions and introduce innovations. People do it all the time. But they are not asking anyone how to! If you need to ask, you are not in a position to lead the crowd.
I just want to have better-looking and shorter code.
Then absolutely positively use +. A no-brainer.
You want to use a function (or functor) for addition instead of the + operator. C++ has std::plus for that. I don't use C++ but I think one or both of these should work:
int a = std::plus(1, 1);
int a = std::plus<int>(1, 1);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Let's say I have a lot of .asm files on a python program (It can also be binary strings, hex strings or whatever you would like). How can I use those files to generate new files that function roughly the same (It's for an assembly game).
The thing is I have a lot of assembly players that were really good at the game and I wondered if I can somehow use natural selection to breed better assembly bots.
This sounds a lot like superoptimization (wikipedia).
e.g. STOKE starts with a sequence of asm instructions and stochastically modifies it looking for shorter / faster sequences that do the same thing.
(Or STOKE can start from scratch looking for an asm sequence that gives the desired result for a set of test-cases.)
It's open source, so have a look at the algorithms they use to modify asm and test-run the code. Of course it's possible if you have data structures that represent operands and opcodes.
See also Applying Genetic Programming to Bytecode and
Assembly, an academic paper from 2014.
I haven't read it, but hopefully it addresses ways to recombine code from different mutations and maybe get something useful more often than you get garbage that steps on the registers from the other code. (That's the major trick with random changes to code, especially in assembly where there are lots of non-useful instruction sequences.)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm studying a-level computer science and my text shows array pseudocode declarations, some starting at 1 and others 0. Can anyone tell me why this is the case. Please note that I am studying Python.
DECLARE List1 : ARRAY[1:3] OF STRING // 3 elements in this list
DECLARE List2 : ARRAY[0:5] OF INTEGER // 6 elements in this list
DECLARE List3 : ARRAY[1:100] OF INTEGER // 100 elements in this list
DECLARE List4 : ARRAY[0:25] OF STRING // 26 elements in this list
There are languages that use either or both, and some algorithms are easier to express with one or the other. For instance, a textbook heap uses 1-based indexing. C and Python use 0 based indexing, Pascal and Ada let you choose, Lua and Matlab use 1 based. In practical terms, you mostly need to be aware which is used in the language you write.
https://en.wikipedia.org/wiki/Comparison_of_programming_languages_(array) lists the conventions used in a few languages. One bit of trivia that's not in there is the C way to find the number of elements in an array: sizeof(array)/sizeof(array[0]). It's rarely used because C's calling conventions strip size information by demoting arrays to pointers, anyway.
In languages like for example Pascal you actually decide what is the lower and upper bound for indexing. This is the notation the text is using.
In Python and most other programming languages instead the first element is always at index 0 and when declaring an array you only say what is the number N of elements.
In a few badly designed programming languages the first element is instead at index 1.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I am intermediate in R and a beginner in Python. However my core abilities lie less in data analysis and more in programming and developing large software systems in teams, and I don't have time to become an expert in both.
Given the advances in the Python world in numpy, scipy, pandas, and its prevalence in data science and in general programming, I think I need to concentrate on Python (even though I enjoy R a lot), and accept that for some tasks I might be 75% as efficient, say, as I would be in R. I'd find this efficiency loss acceptable in order to be a master of one language rather than intermediate at both.
However I don't know enough about either language to really be sure of my facts. I would be very interested in hearing from anyone who is experienced in both R and Python and can say what would be the significant disadvantages, if any, of dropping R in favour of Python?
Edit 5: this question on stats.stackexchange is similar and has some great answers.
(Edits 3-4: reverted content/title to original question, which was closed. The original question attracted a lot of expert comment, my attempt to narrow the question to reopen it failed, and I'd prefer to have these comments below the original text they were commenting on.)