“You’re comparing apples to oranges,” Susan, a developer at my company, said when I asked her which programming language she preferred, C # or Python. “It’s like asking me if I prefer wrenches or hammers. One language is a compiled, statically typed language, the other is a ducktyped scripting language. Each is excellent in its correct context.”
She was right, of course. It’s no use asking which programming language is best. You can only decide which is best for your immediate needs.
In short, C# is best for speed, performance, and game development. Python is best for novice coders, machine learning, and versatility.
Let’s get into a deeper discussion of these two languages, C # and Python.
History of C# and Python 🔗
I like to start these kinds of comparison articles with a bit of history. I find it helpful to understand the circumstances around what caused each programming language to be invented. Hopefully, you do too.
A brief history of C# 🔗
In 2000, Microsoft’s Anders Hejlsberg designed C#. It was nearly called Cool, which stands for C-like object-oriented language. I don’t know about you, but I prefer C#.
C# is a statically typed, general-purpose language, that supports true object oriented programming (OOP), functional, and component-oriented programming, along with others. It’s very similar to Java, because it was invented to compete with it.
Its main claim to fame is its tight integration with the .NET framework, which allows for cross-language interoperability, automatic memory management, and access to a large class library. If you prefer a longer history, I encourage you to check out Wikipedia’s page on C#.
A brief history of Python 🔗
You know what I love about Python’s history? Python stayed so true to its mission. Guido van Rossum, way back in 1991, wanted to develop a language that was easy to read, write, and understand. He wanted to open up the field of computer programming to everyone.
Today, the Python programming language is the most popular programming language because… it’s easy to read, write, and understand. It’s a high-level, interpreted programming, dynamically typed language that supports OOP, procedural, and functional programming. It has a vast community of developers, coders, and hobbyists who continually release amazing packages.
It may have started as a hobby to keep Van Rossum busy during the week around Christmas when his office was closed. But in the three intervening decades, it has grown in leaps and bounds to become a beloved, useful language. If you want more history, the Wikipedia page is great as is Programming Python’s forward.
It’s often described as a language with “batteries included” because of how jam-packed with useful resources the language is. Its main strengths are its syntax and rich set of libraries. Python is very popular for machine learning, data science, and artificial intelligence.
Hang on, what about the other C-suite languages? 🔗
Let’s take a brief look at C programming, as well as C++ since they’re often confused with C#. Here’s a quick table with some pros and cons of the C programming language family.
Language | Type | Pros | Cons |
---|---|---|---|
C# | High-level language | Simplified syntax, automatic memory management, .NET Framework libraries, versatile for desktop and web applications | Built for Microsoft platforms, runs on a virutal machine, automatic memory management |
C | Low-level language | Direct access to system resources, efficient memory management, fast and efficient | No built-in support for OOP, compiles to machine code, manual memory management |
C++ | Mid-level language | Efficient memory management, support for object oriented programming language, compiled language, fast and efficient for performance-critical tasks | Built-in support for OOP, more high-level features than C, compiles to machine code, manual memory management |
Code examples 🔗
Now that you know a little about how each language, C# and Python, came to be, let’s have a look at some code examples.
Here’s one for C#: 🔗
using System;
class Person {
public string Name { get; set; }
public int Age { get; set; }
public void SayHello() {
Console.WriteLine("Hello, my name is " + Name + " and I am " + Age + " years old.");
}
}
class Program {
static void Main(string[] args) {
Person person = new Person { Name = "John", Age = 25 };
person.SayHello();
}
}
This code defines a simple Person
class with two properties (Name
and Age
) and a method (SayHello
) that prints a greeting to the console. The Main
method creates a new instance of the Person
class and calls the SayHello
method.
One of the weaknesses of C#, especially as compared to Python: it’s very wordy. Compared to another dynamically-typed programming language like Python, you need much more boilerplate code. In this example, the syntax to define and instantiate the Person
class is more complex than what you would see in a similar Python program.
Now let’s look at a similar Python code example: 🔗
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def say_hello(self):
print(f"Hello, my name is {self.name} and I am {self.age} years old.")
def main():
person = Person(name="John", age=25)
person.say_hello()
main()
Look how readable that is! In my opinion, the Python code is much easier to understand, there are fewer arcane keywords like static
and void
, and the syntax is much more concise.
But, if you were to run that code, you’d quickly discover one of Python’s weaknesses – it can be a little slow due to its dynamic type-checking and interpreted nature. C# code often runs up to 10 times faster than Python code, depending on the task.
If you want more Python code examples to demonstrate how simple Python is to read, write, and understand, Boot.dev’s Learn Python course is chock full of fun examples like this one.
Which is best for beginners? 🔗
There’s no question – when comparing the Python language to C#, Python is a much, much better programming language for beginners. Here’s why:
Syntax 🔗
Imagine if I told you that, before learning to code, you actually have to learn Spanish first. (Or another language if you already speak Spanish.)
That’d be a real wrench in the gears, right? It’d slow you down.
Python is like coding in English. It’s designed to be a simple, straightforward programming language, easy for nonprogrammers to read at a glance. It reads much more like a spoken human language than C#.
C#, by contrast, is like if you had to learn another language before you could start coding. It takes time, it’s not easy or fun to read, and it’s frustrating to learn from others.
No compiling 🔗
What is compiling? Non-programmers have no clue, and with Python, you don’t need to. Python is an interpreted language, which means you don’t need to translate your source code and translate it into something a machine can read.
With C #, you need to have a C# compiler installed on your computer so that when you run your code, the machine knows what to do with it.
Dynamically typed 🔗
In a statically typed language like C#, you have to declare every variable with a specific data type before it can be used. For example, say you want to store an integer value in a variable in C#. Here’s how you’d need to declare it:
int myNumber = 42;
Python is dynamically typed, which means it will automatically figure out what kind of data type it is. You can just assign a value to a variable. In contrast, in Python, you do not need to declare the data type of a variable explicitly.
Let’s look at that same task using Python programming. To store the integer value 42 in a variable in Python, you can simply do the following:
my_number = 42
Python knows that my_number is an integer variable without you having to say so.
Fast feedback loop 🔗
All the features above combine to give Python a very fast, rewarding feedback loop.
Imagine you’re trying to build a table versus building a deck as a novice carpenter. You could probably figure out how to construct a table without too much difficulty. It wouldn’t be perfect, and you might have to research or ask questions along the way, but it’d be doable. And then you’d have a table you built yourself. You’d be keen to go on constructing things of increasing difficulty.
Compare that to building a deck as a novice builder. Where do you even start? You’d have to do so much reading and learning before you’d even know the right questions to put into Google to figure out how to build it.
Coding in Python is like building a table. There’s such a shallow learning curve to get started compared to a more complex language like C#. You build something, deploy it, feel proud and accomplished, and immediately want to dive back in for more.
Which is more popular? 🔗
From a pure user perspective, Python is definitely more popular. The 2023 Tiobe Index ranks Python as the most popular. C#, by contrast, comes in at number five. The StackOverflow 2022 survey had similar results.
Python was also slightly more “loved” than C#, at 67.34% versus 63.39% loved respectively. However, 17% of respondents want to learn Python next, while only around 6% have their sights set on C#.
This isn’t surprising and isn’t really a good indicator of which is best. Of course, Python is more popular; as I explained, it’s much better for beginners. But among certain career paths, such as game developers, C# is infinitely more popular because it’s the lingua franca. Unity, a cross-platform game engine and development platform, uses C#.
Which has a better performance? 🔗
Short answer: C# is faster, and it’s compiled to run on a VM like Java. But Python is more versatile than C# and can do more things. It runs with an interpreter.
If you need speed or .NET, C# is your language. But if you need it to do a weird or edge-case thing, I’d recommend Python.
To give a more elaborate explanation, I’d say C# has a better performance when you already know what you’re doing and you’re familiar with your toolbox. Think of it like a very technical mountain bike. For experienced mountain bikers, there’s no contest – it’s the fastest.
Beginners on that mountain bike would go slowly, because they wouldn’t know how to use its features appropriately. A general-purpose hybrid bike would be a better fit to help novice cyclists perform better. That’s why for prototyping and the general speed of getting stuff done as a beginner, I recommend Python.
It’s worth mentioning that both are a type of structured programming language – they are both designed to be easier to understand, write, and maintain, by breaking down complex tasks into simpler, more manageable tasks that can be easily understood and modified.
Python vs C# careers 🔗
Let’s compare the two languages from a job perspective. I’ll look at:
- job openings
- salaries
Job openings 🔗
This isn’t a perfect measure, of course, but let’s check out the number of job openings that mention C# versus Python on LinkedIn, Glassdoor, and Indeed.
JOB OPENINGS | Indeed | Glassdoor | |
---|---|---|---|
Python | 85706 | 45988 | 166352 |
C# | 40708 | 22319 | 89957 |
It appears that there are roughly 2x as many Python developer job openings as there are C# job openings. Now let’s look at salaries.
Salaries 🔗
SALARIES | Indeed | Glassdoor |
---|---|---|
Python | 114k | 102k |
C# | 114k | 107k |
Salaries are roughly comparable, maybe with a slight increase for C#.
Final thoughts 🔗
Hopefully this article has helped you decide where you are on your learning journey, and which language makes the most sense for you.
To summarize: Python is better for people just getting into coding, data science, machine learning, data analysis, or any web development not done on the .NET framework. C# is better for performance, speed, game development, and web development done on .NET
And ultimately, remember that these don’t have to be either-or questions. Think of these as tools – you may start with learning how to use a hammer, and work your way up to using a power tool, but it’s helpful to have both at your disposal.