Beginner in python programming

Thread Starter

ZimmerJ

Joined Dec 9, 2020
48
Hello, i'm new to this type of programming and i guess i do not really understand how it works, what it is used for and how you run it.
I haven't found a similar thread but if there is, i'd be happy to be directed there.

Some backround: I have previously done some basic embedded-C programming, and i understand the process of compiling and how the written code translates into what a circuit do. Once it is copied on-to ROM/RAM it is now a "stand-alone" system with its own program to run it.

Now starting this course i am completely lost. Does python-type-programming only run in existing operating systems? From what i read, it seems like it's meant for controlling other programs/softwares by utilizing the code the programmer writes, i think it's referred to as "scripting". I use an IDE called MU from which i can run and debug/check the code, i also use the python shell. To me it doesn't really matter because what can i do from there? The code only runs in the IDE? I think this is called REPL-programming, i am pretty sure there is more to it than just writing code in an IDE.

While i'm at it, i also read about interpreters and how they are needed to actually run the code, (something like a compiler but still different). This must be why a shebang-line is used when you run it else-where and not from an IDE? To address a path to the interpreter?

I know it's alot, any answers appreciated. Thanks.
 
Last edited:

hrs

Joined Jun 13, 2014
321
Python runs on Linux, Windows, MacOS and probably some others. You can use it for scripting in that you can use it to execute external programs but it's a full fledged programming language in its own right.

You can run programs from an IDE but also from a shell like bash on Linux or the command prompt on Windows. Or you can create a link to your program from the GUI like you would with any other program.

The shebang-line lets the OS know it should use the Python interpreter to execute the stuff after the shebang-line. On Linux I use
#!/usr/bin/env python because my environment knows where the interpreter is. You could also hard-link the Python interpreter executable but I wouldn't recommend it. I don't know this for sure but I think #!/usr/bin/env python also works on Windows. Since (the link to) the Linux binary is at /usr/bin/python and the Windows binary is at C:\Python39\python.exe or whatever it's a good idea to use the environment variable.
 

Thread Starter

ZimmerJ

Joined Dec 9, 2020
48
Python runs on Linux, Windows, MacOS and probably some others. You can use it for scripting in that you can use it to execute external programs but it's a full fledged programming language in its own right.

You can run programs from an IDE but also from a shell like bash on Linux or the command prompt on Windows. Or you can create a link to your program from the GUI like you would with any other program.

The shebang-line lets the OS know it should use the Python interpreter to execute the stuff after the shebang-line. On Linux I use
#!/usr/bin/env python because my environment knows where the interpreter is. You could also hard-link the Python interpreter executable but I wouldn't recommend it. I don't know this for sure but I think #!/usr/bin/env python also works on Windows. Since (the link to) the Linux binary is at /usr/bin/python and the Windows binary is at C:\Python39\python.exe or whatever it's a good idea to use the environment variable.
Do you mean that if i write a program, i can load it into the command prompt in windows and run it from there?

Can you simply run the code directly out of the python IDLE? Say if i want to execute an external program, on my OS (some program/software on my PC), would i be able to do it and eliminate the need of using a shebang-line?

If this is the case, why would someone want to run a code from for example the command prompt (given a shebang-line is used as path to interpreter)? Are there any ups and downs with running a code in Python IDLE compared to a shell or IDE?
 
Last edited:

hrs

Joined Jun 13, 2014
321
You can run programs from the command prompt, Python programs included. I don't understand what you mean by "load it into the command prompt".

Why would you want to eliminate the shebang-line? There are multiple ways to invoke the interpreter with your script. The shebang is the best way.
https://docs.python.org/3/using/windows.html#launcher

I think you should just start programming and you'll figure out most of these things.
 

Wolframore

Joined Jan 21, 2019
2,273
Sounds like you need to just play with idle and continue from there, as you have ideas about what you want to accomplish you can draw your conclusions about how to go about it.
 

ApacheKid

Joined Jan 12, 2015
385
Hello, i'm new to this type of programming and i guess i do not really understand how i works, what it is used for and how you run it.
I haven't found a similar thread but if there is, i'd be happy to be directed there.

Some backround: I have previously done some basic embedded-C programming, and i understand the process of compiling and how the written code translates into what a circuit do. Once it is copied on-to ROM/RAM it is now a "stand-alone" system with its own program to run it.

Now starting this course i am completely lost. Does python-type-programming only run in existing operating systems? From what i read, it seems like it's meant for controlling other programs/softwares by utilizing the code the programmer writes, i think it's referred to as "scripting". I use an IDE called MU from which i can run and debug/check the code, i also use the python shell. To me it doesn't really matter because what can i do from there? The code only runs in the IDE? I think this is called REPL-programming, i am pretty sure there is more to it than just writing code in an IDE.

While i'm at it, i also read about interpreters and how they are needed to actually run the code, (something like a compiler but still different). This must be why a shebang-line is used when you run it else-where and not from an IDE? To address a path to the interpreter?

I know it's alot, any answers appreciated. Thanks.
Python is an interpreted language as opposed to a compiled language like C.

So you never compile or link or build the code, certainly not in the explicit sense that we do with C or Java.

What kind of hardware, development board are you using Python with?
 

Thread Starter

ZimmerJ

Joined Dec 9, 2020
48
You can run programs from the command prompt, Python programs included. I don't understand what you mean by "load it into the command prompt".

Why would you want to eliminate the shebang-line? There are multiple ways to invoke the interpreter with your script. The shebang is the best way.
https://docs.python.org/3/using/windows.html#launcher

I think you should just start programming and you'll figure out most of these things.
Alright, thanks.

I mean that you probably wouldn't write individual code instructions after another in command prompt, but rather write the whole code in another shell/IDE and then call that code-file into the command prompt by it's name, atleast that's how i figure it works.
And then, i wonder if there are any benefits of doing that, oppose to running the code through shell, IDE or any another that uses python interpreter?

It's not that i want to or not, maybe i shouldn't have included that in the question, it got confusing. What i meant was if i could run it through the IDLE, and without the shebang, as i figure that the IDLE already has the interpreter going in the program itself? It's more of my curiosity rather then me having a good reason of doing it.
 
Last edited:

Thread Starter

ZimmerJ

Joined Dec 9, 2020
48
Python is an interpreted language as opposed to a compiled language like C.

So you never compile or link or build the code, certainly not in the explicit sense that we do with C or Java.

What kind of hardware, development board are you using Python with?
Yes i have been trying to read about the differences between interpreted and compiled languages, it's alot to get my head around for sure.

I am not using any kind of development board or hardware whatsoever, that's the thing haha. I have no idea what the code is for, except that i am writing it through python shell in my operating system. I am doing very basic stuff for now, just learning the language. But if i want to write code that's going to do anything, how would that kind of code look like? Can i shut my computer of with it? Can i open my web-browser with it? This sounds to me, like very complicated things to write code for, but i don't see what else i can do with it if i am not copying the code to a chip or whatever.
 

Thread Starter

ZimmerJ

Joined Dec 9, 2020
48
Sounds like you need to just play with idle and continue from there, as you have ideas about what you want to accomplish you can draw your conclusions about how to go about it.
I answered the other questions also, maybe that explains my situation, but yes i agree. I probably do need to write a bit more and see what happens. But i have no goals to accomplish, this is why it's tricky for me to ask the right questions. I am just trying to understand what you can do with the code in a operating system, when it is not going to be written for a specific chip and then copied onto the chips memory.

A chip has it's architecture which is why it's important to choose a compiler that knows it's architecture, the compiler also needs to understand the language of your choice. And once you have a schematic over the chip-pins and what those functions are, you can write code dependent on what you want it to do. This to me is more straight forward.
 

hrs

Joined Jun 13, 2014
321
I mean that you probably wouldn't write individual code instructions after another in command prompt, but rather write the whole code in another shell/IDE and then call that code-file into the command prompt by it's name, atleast that's how i figure it works.
You write the program in a text editor or IDE. Then you use a shell to execute it.
Or you can use the interactive interpreter. I often use that as a glorified calculator. For example:
Code:
[hrs@rz ~]$ python
Python 3.9.1 (default, Dec 13 2020, 11:55:53)  
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 1
>>> b = 2
>>> a + c
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'c' is not defined
>>> a + b
3
>>>
What i meant was if i could run it through the IDLE, and without the shebang, as i figure that the IDLE already has the interpreter going in the program itself? It's more of my curiosity rather then me having a good reason of doing it.
I have never used IDLE so I don't know, but you can run programs from Spyder (https://www.spyder-ide.org/) without the shebang.
 

ApacheKid

Joined Jan 12, 2015
385
Yes i have been trying to read about the differences between interpreted and compiled languages, it's alot to get my head around for sure.

I am not using any kind of development board or hardware whatsoever, that's the thing haha. I have no idea what the code is for, except that i am writing it through python shell in my operating system. I am doing very basic stuff for now, just learning the language. But if i want to write code that's going to do anything, how would that kind of code look like? Can i shut my computer of with it? Can i open my web-browser with it? This sounds to me, like very complicated things to write code for, but i don't see what else i can do with it if i am not copying the code to a chip or whatever.
The differences between compiler and interpreter are no longer as clear cut as there were thirty years ago that's for sure, there is a big grey area now that bridges these to concepts.

What is the operating system you use? what tools do you use to edit and run the code you write?

In principle most programming languages will provide some level of interaction with the host OS, but this is not uniform, some languages and tools make it easier than others.

Having code to shutdown an OS should not be overly hard to do in principle if you need to do that. Opening a web browser would likely be much less of a problem too, but the way these things are done are usually outside of the language itself, there is usually a OS specific component that is crafted for each platform be it Windows, Linux, MacOS or even MCUs.
 
Last edited:

Thread Starter

ZimmerJ

Joined Dec 9, 2020
48
Yes, seems like a lot in terms of computer science, or electronics in general is a grey area. Reading about architecture i get the same feeling.

I use windows 10 64 bit, and i use an editor called MU ( https://codewith.mu/en/about ). There seems to be no interaction with anything but the editor itself. It is probably because of my little experience with programming of this type, since i am only doing basic stuff learning about functions, objects etc. Embedded programming works basically the same, except that you really don't have to write such complicated codes in order to make it do anything. It just seems different.

I don't even know what this type of programming is called? Here is an example i found with code: https://matplotlib.org/3.1.1/gallery/lines_bars_and_markers/simple_plot.html

This is probably the most interaction outside a shell/editor/IDE that i've seen, where a picture comes up. And looking at the code, there is almost nothing that i am familiar with. But still, here it imports a library that uses a picture in order to make the graph, so really there is only interaction with the editor still.

Okey so basically you need other softwares in order to make code interact with other than the editor?
 

ApacheKid

Joined Jan 12, 2015
385
A tool named "Visual Studio Code" (not to be confused with a different tool named simply "Visual Studio") is available for free from Microsoft and this tool runs on Linux, MacOS as well as Windows, its also referred to as "VS Code" sometimes.

Python is one of many languages that rich support in VS Code.

You'll get full syntax coloring and debugging and so on.

However MU looks fine particularly for working with some boards.

I'm hugely interested in ways of making MCU devices much easier to write code for than they are ordinarily and Python is a much less fussy language than C or C++ - I just don't know much about Python.
 

djsfantasi

Joined Apr 11, 2010
7,693
A programming language has been referenced in the thread as “loose”. A major component influencing people’s description of “loose”, is how the language treats “typing” of “objects” within that language. An object is a function, variable or data structure. Typing is described in This Wikipedia article on typing.

Secondly, how a program is run can be classified into several levels. In an environment based on an operating system, the shell or command line is an interpreter. It uses parsing rules to further define how a command is executed. Some commands are internal to the OS. Some commands are external programs contained within the OS. Some commands are interpretive. Like a .BAT or .CMD file in Windows. Otherwise known as a script.

The other function of the OS command line is to execute non-internal programs. One type is an application such as Word under windows. Another type of program is a run-time or compiler for a programming language. A compiler produces a new external program that can be run from the command line. It creates a machine language stand-alone program that may use the OS libraries.

An interpreter runs a text file program. It translates the commands contained therein into machine language code. One could consider this a language-specific command line program executed within the OS command line shell.

So we have layers upon layers. We start with interpreters that eventually run machine language code.

There is another layer of abstraction. There are languages that are translated into non-text tokens that are used to direct an interpreter to execute machine language. This doesn’t invalidate the model I’ve presented. It’s just a special case.

I’ve coded a system that runs on an Arduino. It “pre-compiles” a proprietary language into a set of numerical values called tokens. Another Arduino sketch takes these tokens and controls a machine. There is no OS, but an environment. Thus, I skip the first layer of abstraction for the pre-compiler. The other sketch executed an interpreter which executed my pre-compiled code.

If you understand this explanation, then hopefully you can better understand the answers to your questions.
 

Thread Starter

ZimmerJ

Joined Dec 9, 2020
48
A programming language has been referenced in the thread as “loose”. A major component influencing people’s description of “loose”, is how the language treats “typing” of “objects” within that language. An object is a function, variable or data structure. Typing is described in This Wikipedia article on typing.

Secondly, how a program is run can be classified into several levels. In an environment based on an operating system, the shell or command line is an interpreter. It uses parsing rules to further define how a command is executed. Some commands are internal to the OS. Some commands are external programs contained within the OS. Some commands are interpretive. Like a .BAT or .CMD file in Windows. Otherwise known as a script.

The other function of the OS command line is to execute non-internal programs. One type is an application such as Word under windows. Another type of program is a run-time or compiler for a programming language. A compiler produces a new external program that can be run from the command line. It creates a machine language stand-alone program that may use the OS libraries.

An interpreter runs a text file program. It translates the commands contained therein into machine language code. One could consider this a language-specific command line program executed within the OS command line shell.

So we have layers upon layers. We start with interpreters that eventually run machine language code.

There is another layer of abstraction. There are languages that are translated into non-text tokens that are used to direct an interpreter to execute machine language. This doesn’t invalidate the model I’ve presented. It’s just a special case.

I’ve coded a system that runs on an Arduino. It “pre-compiles” a proprietary language into a set of numerical values called tokens. Another Arduino sketch takes these tokens and controls a machine. There is no OS, but an environment. Thus, I skip the first layer of abstraction for the pre-compiler. The other sketch executed an interpreter which executed my pre-compiled code.

If you understand this explanation, then hopefully you can better understand the answers to your questions.
Interesting, yes it is more clear to me now. Thanks.

Regarding what you coded, what are the benefits of skipping the first layer of abstraction using tokens? My guess would be faster execution and it might take up less space in terms of memory?
 

MrSalts

Joined Apr 2, 2020
282
The question is, what do you want to do with Python?

do you want to control I/o pins on a serial output?

do you want to scrape a website or list of websites for "about" information? Or scan for and save every email address listed in all the accessible links on a website?

search a series of text files, pdf, xml or html files for certain phrases, update the phrase and re-save.
convert one image file type to another.
Read data from a sensor connected to a computer via usb/ftdi serial adapter, process/save data and control a motor or display or both that corresponds to the sensor value.

Make a slide show script to show all of the photos in your image library.

python can do anything, it is not fast but it is easy. 5th graders use python to learn code and Google uses python for lots of small tasks and to glue various major pieces of code together. It is simple to learn, simple to understand other people's small scripts and easy to change (no compiling needed.
 

Thread Starter

ZimmerJ

Joined Dec 9, 2020
48
The question is, what do you want to do with Python?

do you want to control I/o pins on a serial output?

do you want to scrape a website or list of websites for "about" information? Or scan for and save every email address listed in all the accessible links on a website?

search a series of text files, pdf, xml or html files for certain phrases, update the phrase and re-save.
convert one image file type to another.
Read data from a sensor connected to a computer via usb/ftdi serial adapter, process/save data and control a motor or display or both that corresponds to the sensor value.

Make a slide show script to show all of the photos in your image library.

python can do anything, it is not fast but it is easy. 5th graders use python to learn code and Google uses python for lots of small tasks and to glue various major pieces of code together. It is simple to learn, simple to understand other people's small scripts and easy to change (no compiling needed.
I see, yes i think it's just a matter of putting some time into it and eventually start experimenting with importing files and write different kind of code. By different i mean in contrast to only naming a couple of variables and learning different kind of functions and how the language work in general. Appreciate the answers, Thanks!
 

merdobeyz

Joined Apr 21, 2021
4
Python runs on Linux, Windows, MacOS and probably some others. You can use it for scripting in that you can use it to execute external programs but it's a full fledged programming language in its own right.

You can run programs from an IDE but also from a shell like bash on Linux or the command prompt on Windows. Or you can create a link to your program from the GUI like you would with any other program.

The shebang-line lets the OS know it should use the Python interpreter to execute the stuff after the shebang-line. On Linux I use
#!/usr/bin/env python because my environment knows where the interpreter is. You could also hard-link the Python interpreter executable but I wouldn't recommend it. I don't know this for sure but I think #!/usr/bin/env python also works on Windows. Since (the link to) the Linux binary is at /usr/bin/python and the Windows binary is at C:\Python39\python.exe or whatever it's a good idea to use the environment variable .
its true
 
Top