Looking for some help getting started with Python

I’m a programmer of some 45 years on various platforms and languages. I’ve been writing code for and building Arduino/Teensy based projects as a hobby for close to twelve years. My comfort zone is primarily with C++, beginning around 1988.
My problems with Python as a beginner stem from a lack of straightforward examples of code that runs without a string of compiler errors concerning missing or undefined libraries (modules?). After searching for an example or two I use the built-in editor to run a short snippet and receive in response a string of errors centered around two major areas: no such library/module or some esoteric compiler error complaining about an inability to resolve some octal character 116 at position x, or some such.
So the question is this: Am I making an error is using IDLE to try to enter python code? Are there websites which display entire code examples with nothing in the way of “includes” left out, examples which bring me all the way from including all relevant “libraries/code modules” to output I can look at to examine whether what I entered was what I intended?
To be specific, at present, I am attempting to read a single-column csv output from my Arduino C++ into python and numerically integrate a single column array to be output to an array of data I can plot. It’s not all I’m looking for, but it would tell me whether I want to invest more time using python as an analytical tool in conjunction with my SBC projects.

If you want to write python code that uses libraries that are not part of python itself then you must install them. Just as you would have with any C++ project.

Depending on your OS you may install from your OS packaging system or you can install using pip, typically into a venv.

If you can provide the extact error you are seeing we can comment on what is the issue.
Please post any code and error out as prefomatted text like this:

def do_it():

On the other hand, you can do a lot without ever installing extra modules. Python comes with a rich standard library. There’s even a csv module to help you read that data.

I think you will need external modules to plot it beautifully, but you could start without. Serious data scientists use matplotlib and seaborn, but the learning curve is quite steep.

This philosophy of “batteries included”, that led to a comprehensive standard library, goes back long before finding and installing modules was made easy by PyPI.

IDLE is perfectly good for small educational projects. I use that most of the time for learning. I use PyCharm for bigger things. I remember that the error messages take a bit of getting used to.

There are a lot of beginners tutorials on line, some pointed at by python.org. You may find The Python Cookbook (O’Reilly) good to have to hand.

Many thanks. Probably my greatest focus for now will to be using Python to read in single column arrays of data from my Arduino output, and use something like cumtrapz to perform an integration and output an array to be plotted. At present I use an m file to plot data in GNU Octave, but would likely prefer to remain in the Python world to do it all.
If Python looks to be worth the investment, I’d likely take the time to learn how to use it. I became a C++ user around 1990 and still love to use it. In those days the compiler our crowd of independent developers were in love with was Borland’s C++ compiler. Unfortunately, they got shoved ouy by MS :wink:
Thanks again.

Good; then by now you should have excellent debugging skills and an intimate familiarity with the process of reading and deciphering error messages. Python tries to make these clear and understandable (in particular, there has been a major push to re-improve them since 3.10) and I think the results are quite evident. Since Python is dynamically typed, it also does not bother with a complex generic typing system (as it does not require ad-hoc/parametric polymorphism since everything is done at runtime). As a result, Python’s error messages should be easy to understand for someone accustomed to wrestling with C++.

Aside from that, in my experience people who have used “various languages” are normally able to pick up the fundamentals of a new one in a few days, aside from perhaps stumbling on some unfamiliar paradigms. As a quick summary:

  • Python uses dynamic typing, as already noted - but also fairly strongly typed, in standard parlance. (People who use languages like Haskell often don’t consider dynamic typing to qualify as “typing”, so they don’t care about the strength of such typing.) Variables do not have types (types can be annotated in modern versions, but these annotations are only used by third-party tools and are neither required nor relevant for program correctness), but values do, and relatively few conversions happen implicitly. A string cannot be directly concatenated with an integer, for example, although of course there are numerous options for creating a formatted string that includes information from an integer source.
  • Python does not distinguish assignment from initialization, and does not have a concept of declaration (the set of local names used is determined by just seeing what’s assigned to; global names are looked up dynamically in a special sort of dictionary). It does have a concept of “uninitialized” variables (which are called “unbound” instead), but attempts to access these are safely detected at runtime, resulting in an exception.
  • Python uses reference semantics for variables (which we often prefer to call “names”). In practical terms and by analogy with C or C++, this entails that the actual contents of variables are indirected through a pointer that is automatically followed for every usage, while the language syntax itself does not allow for direct manipulation of pointers.
    This is just like how types other than non-array primitives work in Java, and how class types (as opposed to struct types) work in C#.
    This is not like C++'s reference types:
    • C++ references are not objects with inherent separate storage; every Python variable entails allocating storage for a pointer, even if it points to an already existing object-state.
    • Assignment to Python names rebinds them and does not mutate the state of any object and does not copy any object.
  • Relatedly, object attributes, elements (for sequences like the built-in list) and values (for mappings like the built-in dict) are similarly indirected. Assigning to them, like a[b] = c or a.b = c, by default, rebinds some internal reference; keep in mind that this has the effect of mutating a, unlike a = b.
  • Python passes arguments by assignment, which is exactly the historical meaning of “call by value” - but this term is outdated, because it is misleading in the presence of variables with reference semantics.
    • Assignment to a parameter does not affect the caller’s variable. The reference semantics do not and cannot implement true “pass by reference”; in particular you cannot write a “swap” function in Python. (But you don’t need to, because you can just write a, b = b, a - it creates an anonymous tuple from the RHS, then unpacks it into the LHS. There are many very powerful extensions of this technique.)
    • On the other hand, mutating that object does affect what the caller “sees”, since it is the same object bound to separate names in the caller and callee.

I assume this is intended only as a very rough summary.

The only compiler error Python produces is SyntaxError; errors due to un-importable modules occur at runtime. (When using the interpreter prompt, this distinction is usually hard to notice, for obvious reasons.)

To comment clearly on any “esoteric” error I would have to see a specific example of what you encountered. The only reason I can think of for an error message mentioning octal is because you tried to write an integer literal with leading zeros. This used to mean that the digits should be interpreted as octal, but now it’s an error - you need a 0o prefix. (Either way, of course, 8 and 9 are not valid digits in octal.) But that description sounds more like it had something to do with character encoding.

Oh, right, that’s another major difference. Python’s string type is built-in (not from the standard library), and string literals create that type; it supports Unicode natively; subscripting it creates one-element strings (there is not a separate “character” type); and in even remotely modern versions it is a completely separate type from the built-in type for sequences of bytes (called bytes). Whenever you, for example, read a text file, you must (this is true in every language!) know the text encoding in order to properly make sense of the contents. Python’s concept of “opening a file in text mode” will produce strings rather than byte-sequences from the file contents, but you should specify the encoding explicitly (and need to know which encoding to use), as the default may not be suitable.

You should learn the fundamentals of the language first, but the tasks you describe are much more commonly done using certain (very popular) third-party libraries. In particular, Pandas (which depends on Numpy) has sophisticated built-in support for reading CSV files to create its own data structure (which leans on Numpy data structures, which are implemented using C under the hood), and Numpy provides numerical integration functionality.

For fundamentals, the tutorial included in the official documentation should be pretty solid. It includes many self-contained examples, and only imports from the standard library or from other files that are part of the same example. It also keeps the examples that require imports in the section that’s specifically about the module import system.

In my personal experience, IDLE is not very useful. An IDE is really not required to edit Python code; it’s perfectly possible to get by with a suitably sophisticated text editor and the command line. As an IDE, IDLE doesn’t provide a lot of functionality on top of that; and because it’s implemented using Tkinter (the windowing toolkit provided in the standard library), I’ve found that its event loop can have obnoxious interactions with the event loop of any GUI programs you try to make with Tkinter. (However, this impression was formed many years ago, and could have been really my fault; but still, I don’t find IDLE useful at all.)

The code examples that you’re finding probably aren’t “leaving anything out”. Python has a real module import system, not a preprocessor doing text search-and-replace to piece together “translation units”. Where the code example says import foo, that’s all the code needs to say in order to access that module. If it isn’t being found, that’s an issue with the actual module being provided. Numpy, Pandas and many other popular tools are third party, just like Boost is for C++, and must be installed separately.

1 Like

Lots of good info. Thank you very much for taking the time.
I’ll try an alternative to IDLE (perhaps Notepad++ would work in this case). Borland’s compiler was so good, I could skate over a lot when it came to running code in the Windows environment, so approaching 80, I’ll be happy to work with a text editor and let the compiler shout at me!
As for the odd error message re a character in some position out in a line of text, I have no clue. I probably couldn’t reproduce it as I was only fooling with an example. The Arduino C++ compiler can come up with error messages which can send one all over the script. It looks very esoteric right up to the point where you figure out what you did and where you did it.
Again, many thanks for the robust response, it’s appreciated.

I looked over what you wrote and am puzzled at the tone. Apparently I was looking in the wrong area. My mistake.

The answers to your questions will vary and depend on, not only how you like to learn and code, but also on how a respondent likes to learn and code. So, with that in mind…

Possibly. Although I admire the work that went into IDLE, I’m not a huge fan and did not take to it when I started coding Python. You don’t need to use a full on IDE; you can use a text editor and a text editor that is Python aware, will make for a better experience, in the same way as it does for coding C. With a IDE such as Wing (I use Wing Personal) you get a real boost in terms of code analysis and bug fixes, which you will not get from even the best text editor, when coding, and I found the experience with Wing, much more to my liking, when compared to IDLE.

Yes; there are 100s and some are better than others. As with IDEs and text editors, each respondent will have their own take on which is which. One of the better ones (IMHO) has to be Real Python. Although some of the content is behind a paywall, there is much that is not and many (if not all) of the tutorials have more detail than I can digest in one sitting.

I’m only going to link one more source as I don’t want to make this into some kind of a TL;DR post: Automate the Boring Stuff with Python. This is just one of the books that Al Sweigart has produced. You can buy it or you can read it, on-line, for free. It should go without saying that a “donation” would be the right thing to do, but each to their own on that one.

It sounds to me that you have had a less than encouraging introduction to Python, but don’t let that put you off. Python is a wonderful language and (for the most part) has a very helpful community of coders; it simply takes a little time to find your way with both the tools and the people who will be of the greatest help to you, from the get-go.

1 Like

Thank you for a to the point and highly useful response. Very much appreciated. I shall look at your suggestions and look forward to using Python as a data analysis and display tool. Again, many thanks!
A note: made some progress just in the past two days and have been able to see how to gain access to the public interface of the language.

1 Like

One additioinal short note: many people install pandas and numpy,
which come with good analysis and plotting features.