Navigating the Shift: What I Learned Moving from C# to Python

As of this moment, I have full immersed myself into Python for two months. Although I've dabbled in it sporadically throughout my career, this was my first deep dive. Transitioning from C#, a language I've mastered over the past decade, presented its unique set of challenges and surprises. Some aspects of C# that I found cumbersome are simply absent in Python, offering a refreshing change. In this article, I aim to share my discoveries and compare Python with C#, a language I have been using for over 10 years.

While I intend to share differences, it’s worth mentioning the similarities, which is what helped me decide on choosing Python for the next adventure. The object-oriented nature of Python felt familiar, with classes, inheritance, and polymorphism playing a central role in both languages. Coming from C#, where memory management is more abstracted compared to languages like C++, I found Python's automatic memory management to be a relief. This meant I could focus more on developing the logic of my applications without being overly concerned about the underlying memory allocation and garbage collection. This common ground in fundamental programming paradigms provided a solid foundation for building my Python skills, leveraging my C# experience to understand Python concepts with more ease.

One of the more pronounced differences between Python and C# lies in their typing systems. Python's dynamic typing allows developers to write code without explicitly declaring variable types. Interestingly, Python introduced type hinting in version 3.5, offering a middle ground by allowing developers to annotate variables with types. This feature enhances code readability and supports better tooling for Python, bringing some of the benefits of static typing to a dynamically typed language. Another noteworthy difference is in how the two languages handle function expressions and data querying. C# developers often leverage lambda expressions and LINQ for concise and readable data manipulation. Python, while supporting lambda functions, typically uses list comprehensions and built-in functions like map() and filter() for similar purposes.

Here’s an example. Let’s say I have a list of strings. I want to return a new list that only contains strings from the list that contains one word.
In C# with LINQ:
inputList.Where(s => s.Split(' ').Length == 1).ToList();
In Python:
[s for s in input_list if len(s.split()) == 1]

Python's approach to object-oriented access modifiers diverges significantly from C#. In Python, the convention is that everything is public, adhering to the philosophy of "we are all consenting adults here." While Python does not enforce access restrictions as strictly as C#, using naming conventions (like prefixing names with an underscore for 'protected' members) and properties, it trusts developers to respect these conventions rather than enforcing them. This openness promotes a culture of transparency and flexibility, allowing developers full access to modify and interact with object properties as they see fit. Although some argue for the benefits of strict access control, I find Python's approach refreshingly straightforward, simplifying the development process without sacrificing functionality or design integrity.

Python Package Index (PyPi)

Both Python and C# make adding new features to your projects easy by using packages or libraries. Python has something called the Python Package Index (PyPI) where you can find all sorts of libraries for different tasks, like web development with Django or data analysis with Pandas. To add these to your project, you use a tool called pip. It's pretty straightforward to use, and it lets you quickly get the tools you need to work on your project. C# does something similar with a system called NuGet. It's built into the .NET framework, and like pip, it lets you add libraries to your project. If you're using Visual Studio for C# development, NuGet is integrated right into the IDE, making it easy to find and install what you need. I personally find Python's approach a bit easier to work with. Using pip from the command line is simple, and Python's easy-to-read syntax makes trying out new libraries and adding them to your project really straightforward. C# and .NET are great too, especially for big projects, but I like the freedom and simplicity Python offers for trying new things and building projects quickly.

Diving into Python after years of working with C# has been an enlightening journey. While both languages have their unique strengths and weaknesses, I've come to appreciate the simplicity and flexibility Python offers. The differences in syntax, typing systems, and accessibility have pushed me to adapt and learn, enriching my programming skills. As I continue to explore Python, I plan to seek out new projects that challenge me and expand my knowledge. This exploration isn't just about mastering a new language—it's about understanding different ways to solve problems and making the most of what each language offers. Whether it's through contributing to open-source projects, developing new applications, or simply experimenting with Python's vast array of packages, my journey into Python is only just beginning.