Studies

How to Program in Python – The Basics You Need to Know

A Fresh Start for a Programmer Experienced in Object-Oriented Languages

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.

First Impressions: The Simplicity of Programming in Python

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.

Dynamic Typing and Flexibility

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.

Working with Functions: The Simplicity of “def”

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#.

Control Structures: Back to Basics

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.

Object-Oriented Programming in Python: Simple but Powerful

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.

Conclusion – Learning to Program in Python Is a Must

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.

Learn more from the official documentation

Vinicius Sodré

Formado em Ciência da Computação pela Unicarioca, desenvolvedor de software com 15 anos de experiência em grandes empresas nacionais e multinacionais. Vinicius está à frente deste blog, feito de desenvolvedor para desenvolvedores de iniciantes a experientes.

Compartilhar
Publicado por:
Vinicius Sodré

Posts Recentes

Functions in Python: How to Create and Use Them Efficiently – Lesson 7

Functions are one of the most important concepts in any programming language, and Python is…

2 years atrás

Loops in Python: Run Code Repeatedly – Lesson 6

Loops in Python are one of the most fundamental concepts in any programming language. They…

2 years atrás

Conditional Statements in Python: Control the Flow – Lesson 5

When developing software, one of the most common tasks is controlling the program's flow of…

2 years atrás

Learn How to Manipulate Strings in Python – Lesson 4

Manipulating strings in Python is one of the most common tasks, and Python offers incredibly…

2 years atrás

Data Types in Python: Understanding Primitive Types – Lesson 3

Understanding data types in Python is one of the first steps to mastering the language.…

2 years atrás

Garbage Collector in .NET: Avoid Errors and Improve Performance

The Garbage Collector (GC) is one of the essential pillars of memory management in .NET.…

2 years atrás

Este blog utiliza cookies. Se você continuar assumiremos que você está satisfeito com ele.

Leia Mais...