# Second programming language to get under the belt: python vs C

## Summary:

I'm spoiled with choice for a second programming language to learn. Python vs C.
Hello everybody,
I am a master student in Theoretical Chemistry and I am working in the DFT realm. Both TDDFT and DFT applied to extended systems (eg. using QUANTUM ESPRESSO). Of course I work with these softwares from a end-user point of view, not as a developer.

But anyway, even if some of you have no idea what are some of the acronym I used, still you could help with the dilemma I have:
I already am at an intermediate level in FORTRAN language and I am considering starting to look up a second language. The reason is that FORTRAN is a very old code and is used nowadays only in niche fields (mostly numerical-heavy applications), and I would like to learn a language that I could use also outside from my research field.

I am leaning towards Python, but I am not sure because I heard that C and C++ are widely used in the physics community.
Anyway, tell me what you think. Of course I am open to other programming languages besides the ones I mentioned (Julia for example).

Related Programming and Computer Science News on Phys.org
.Scott
Homework Helper
Judging only from the information in your post, I would think that Matlab or Python would be better for you than C.
You would need C or C++ to code an entirely new algorithm that needs to execute efficiently. In a situation like that, you would become a de facto developer and you would want the full power of C or C++. But it sounds as though that would be a very tiny minority of your programming needs - not enough to warrant the investment in leaning C.

pbuk
Gold Member
You wouldn't learn much more from C than you have from Fortran (which has not been capitalized for over 30 years!). C++ would give you OOP (Object Oriented Programming), but so would Python and it would also open up a huge ecosystem of easily used graphical, statistical and other packages and it would be my recommendation. But why not learn C++ too as this will teach you the strengths and weaknesses of strong typing?

atyy and Klystron
DEvens
Gold Member
The thing about this is, you can write bad programs in any language. The thing to do is to learn the principles of writing good code. Before you learn any more coding, read this book.

https://www.amazon.com/dp/0735619670/?tag=pfamazon01-20&tag=pfamazon01-20

Then you should pick out a task you want to accomplish with a new computer language. Then look around for the language that has the best development tools, best libraries, and most active developer community, relative to that task.

So, for example, if you were going to do some kind of analysis of molecules, and you were going to do the graphical part, then that points you in one direction. But if you were going to be doing some kind of numerical simulation of chemical reactions, and you needed particular specialist libraries, that points you in a very different direction. And yet again, if you were going to do some kind of database app, then that points you in yet another very different direction.

wle
Summary:: I'm spoiled with choice for a second programming language to learn. Python vs C.
Why not learn both? It's not like it would necessarily take all that much of your time. Python is among the easiest programming languages to pick up and start using and has lots of libraries available for getting things done. C is much lower level, so less practical for many purposes, but (as opposed to C++) it is a quite compact language that you can learn in a relatively short time and it will require you to learn some general low-level programming concepts, particularly pointers and memory management.

C is also the implementation language of most other programming languages (including Python) as well as probably the language that the core of the operating system you are using is written in. The result of this is that C practically defines the calling conventions and low-level interfaces used by software on your computer. Because of this you'll sometimes see C show up as a 'glue' language, letting you call code written in one programming language from a different programming language.

jedishrfu
Mentor
Given you work with Fortran, I would suggest Julia. Julia has syntax very similar to Matlab and indeed it works with vectors and matrices. Julia can interoperate with Fortran, C, Python and R. It’s been designed for Data Science projects.

If your work required migrating Fortran to a more modern language then Julia may fit your needs.

Keep in mind there is no perfect language only near perfect programmers who strive to use the tools they have to solve the problem at hand.

Wrichik Basu and scottdave
I probably should had also mentioned that I am currently working in a Linux machine and I frequently have to work with a supercomputer (obviously no GUI there). For this reason I am also trying to expand my knowledge of bash and basic shell scripting. I heard that C is very useful to understand better how a Linux machine works and for shell scripting.

On the other hand I know that C has a steep learning curve, meaning that I would need more time to get going with it.

So, of course learning both codes (Python and C) would be useful, but I am worried that trying to learn two programming languages and at the same time work with Fortran and play with bash could be a little overwhelming. Wouldn't it be confusing?

Also, instead of "more or less" knowing 3 programming languages could be better to know well just two. But I could be mistaking here.
Thanks again!

Staff Emeritus
2019 Award
DEvans is right. It's better to learn programming than programming languages.

jedishrfu
jedishrfu
Mentor
In general, people don't learn BASH they look for an example of what they want to do and then adjust it so basically they learn just enough to know what it can do.

BASH is usually used to prep an environment for a program to run in like setting up environment params, clearing directories, saving stuff and then running a program.

Bottom-line I wouldn't focus on BASH so much. There are cookbooks available loaded with BASH tricks and tips that can get you going.

One common trick in BASH is to use the AWK language to parse strings that are later used in your BASH script.

Ans an example, sometimes people need to kill running processes and they first have to run the ps command to find the process number and then use the kill command to kill the process. Folks have combined these into a script called nkill where you type in the name of the process and the script extracts it from a ps output via awk and constructs a kill command to run.

nkill:
#!/bin/bash
if [ "$1" == "-y" ]; then yn="y" shift echo "1 =$1"
else
yn="u"
fi

#echo "yn = $yn and arg1 =$1"
clear
echo "[....] NKILL Matching Processes: $1" echo "[....]" ps aux | grep$1 | awk '$0 !~ /grep|nkill/ {for(i=3;i<11; i++) {$i=""; }; print "[....] " $0; }' if [ "$yn" == "u" ]; then
echo ""
echo "ENTER y to terminate processes -- else ENTER n to break (yn=$yn)" read yn fi if [ "$yn" == "y" ]; then
echo "[....]"
echo "[....] Terminating ALL $1 processes..." echo "[....]" ps aux | grep -i$1 | awk '$0 !~ /grep|nkill/ {print "[....] "$0; system("kill -9 " $2 " >/dev/null"); print "[....] Process "$2 " terminated..."}'
fi
Unpacking some of the script tricks

One thing, I'd like to point out is the double quote(") vs single quote (') as used in BASH. If you notice there are some $0,$1 and $2 usages in the scripts. When$ variables are used in a double quote their value gets subbed in but when used in a single quote they remain as $xxx.$$nkill java So you can see the echo statements use double quotes and the$1 script argument ie the "java" is echoed back to you in the statement [....] Terminating ALL java processes..."

In contrast the \$ variables used in the AWK command are handled by AWK and mean different things to AWK.

In the last line, starting with the ps command:

1) ps reports on all processes running
2) grep strips out all process lines containing the string "java" or "JAVA" or any variation of casing.
3) awk extracts the process number from each line
4) awk then runs the kill command to kill that process making sure NOT to kill any nkill process or any grep process.

Last edited:
jedishrfu
Mentor
With respect to Python and C, yes you can get confused moving back and forth between them. My suggestion is to think about the project and decide on the language. If you are extracting data from files and doing analysis then Python would be better than C.

If you were extracting data directly from hardware and/or controlling hardware then C would be best.

Python and C can interoperate on a given platform using the ctypes package. This means that python can be your main language and when you need to do something more hardware specific then just write some C code for that task and use ctypes to interface the two languages.

https://docs.python.org/3/library/ctypes.html

Also look for some O'Rielly cookbooks for these languages as they are great references for doing specific tasks.