After more than 10 years programming in traditional object-oriented languages like C#, embarking on the journey of programming in Python brought a mix of familiarity and surprise. Python, with its lean and flexible syntax, offers a unique approach to development that challenges some of the practices I was used to in the world of statically typed languages such as C#. In this article, I share what I learned while exploring the basics of Python, highlighting the differences, the similarities, and my first impressions for other programmers who, like me, are used to traditional OOP languages.
From the very beginning, Python stands out for the simplicity of its syntax. While in C# we are used to verbose structures that require braces, semicolons, and other markers, in Python things flow differently. For example, instead of braces to delimit code blocks, Python uses indentation, which improves readability and, at the same time, enforces a stricter organization. In Python, badly indented code does not run, because it is not just a matter of “messiness”.
This difference took some initial adjustment on my part, but it quickly became clear that using indentation makes sense. The code is clear and easy to understand. For those coming from C#, this change requires care, but it soon becomes natural.
One of the biggest contrasts between C# and Python is typing. In C#, you declare each variable with a specific type, which ensures safety but adds verbosity to the code. In Python, dynamic typing removes the need to specify the type, because the interpreter itself determines it at runtime.
Example in C#:
int number = 10;
string greeting = "Hello, world!"; In Python, it would simply be:
number = 10
greeting = "Hello, world!" This level of flexibility boosts productivity, but it requires some care to avoid mistakes. Programmers used to strict typing may miss the “safety net” that prevents operations between incompatible types. However, I learned that Python offers tools, such as the type function, for type checks when needed. In addition, Python 3.5 introduced type annotations (type hints), which help document and clarify code without giving up the flexibility of dynamic typing.
Declaring functions in Python is straightforward and intuitive. The def keyword is used to define a function, with no need to specify a return type. In C#, we are used to explicitly declaring the return type and the function parameters, which sometimes makes the code longer and less fluid.
Example of a function in C#:
public int Sum(int a, int b) {
return a + b;
} In Python, you write the same function more directly:
def sum(a, b):
return a + b
Another notable difference is that Python supports default arguments and variadic arguments more naturally, simplifying many functions that would require overloading in C#.
Python offers familiar control structures, such as if, for and while, but in a simplified form, without the clutter of parentheses and braces. If you come from C#, you need to get used to the absence of these delimiters, but the adjustment is quick. The built-in lists, dictionaries, and other data structures also impressed me with their versatility and ease of use.
Example of a for loop in Python:
for i in range(5):
print(i)
This is a typical example of how Python stays simple without losing power. The range function is flexible and lets you quickly create counting loops or iterate over lists and other iterables.
Finally, one of the most familiar parts for anyone coming from C# is Python’s object orientation. Python lets you create classes and work with methods and attributes, but with a lighter approach. Instead of public, private, and other access modifiers, Python uses conventions (such as the underscore _) to signal private variables. The structure of a class in Python is simpler, with no need to specify types for each variable.
Example of a class in Python:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print(f"Hello, my name is {self.name}")
In short, Python uses a more flexible style of object orientation than C#, without sacrificing expressiveness or power. This approach lets you build complex applications without the rigidity of more strictly typed languages.
Starting to program in Python after years of working with C# has been an enriching and challenging experience. Python is a powerful, general-purpose language that is easy to learn and is used in areas such as web development, artificial intelligence, and data science. Its simple, dynamic syntax is refreshing, while also demanding attention to new details such as indentation and dynamic typing.
For other developers getting started with Python, my recommendation is to embrace the differences and take advantage of what this language offers. The basics of Python may seem simple, but its applications are endless. With each new exercise, I see how this language can adapt and grow with your needs, enabling agile, versatile, and modern development.
Web development has evolved significantly in recent years, and Python frameworks are at the forefront…
Artificial intelligence is revolutionizing every sector of society, from enterprise applications to everyday solutions. At…
In this lesson of the Python mini course, I want to cover two collection types…
Among Python's most versatile data structures, dictionaries stand out. They help us represent structured data…
In this ninth lesson of the mini course, I want to talk about one of…
Understanding variable scope in Python is essential to avoid errors and write more predictable programs.…
Este blog utiliza cookies. Se você continuar assumiremos que você está satisfeito com ele.
Leia Mais...