From a PHP developer’s perspective, Python’s tuples can seem quite peculiar. They are like PHP’s array, but they are immutable, and sometimes, they even appear to be “invisible”! Immutability is covered in another section, so here, let’s focus on this “invisibility.”

Consider this example code:

def my_func():
	x = 1
	y = 2
	return x, y

This might lead PHP developers to wonder:

“Wait, can Python functions actually return two separate values at once? If so, how on earth would we handle them?”

Read more →

Now that your Python project is set up and your IDE is ready, it’s time to dive into the fundamentals of Python syntax. If you’re coming from PHP, you’ll notice some key differences in how Python handles code structure, variables, and control flow. This section will guide you through the core syntax elements that form the foundation of Python programming.


1. Indentation and Code Structure

Python uses indentation (spaces or tabs) to define blocks of code, unlike PHP, which uses braces {} for this purpose. Proper indentation is mandatory in Python and determines the scope of loops, conditionals, and functions.

Read more →

Concepts that confuses PHP devs

Immutability (especially for strings and tuples):

PHP Python
Strings and arrays are mutable. You can change a character in a string by its index ($str[0] = 'H';) or directly modify array elements. Strings and tuples are immutable. Once created, their content cannot be changed. Operations that seem to modify them actually create new objects.
PHP devs might try to assign to a string index (my_string[0] = 'h') in Python and get a TypeError. We need to learn patterns like string concatenation/slicing to build new strings or converting tuples to lists for modification and then back if needed. This impacts performance considerations and how data is handled.

“Everything is an Object” and Magic Methods (Dunder Methods):

PHP Python
PHP has a robust OOP system, but its “everything is an object” philosophy isn’t as pervasive as Python’s. PHP’s magic methods (__construct, __get, etc.) serve similar purposes but Python’s are more extensive and integral to the language’s protocols. In Python, functions, classes, numbers, strings—everything is an object with attributes and methods. Magic methods like __init__, __str__, __len__, __getitem__ allow objects to emulate built-in types and integrate with Python’s syntax.
Grasping how deeply objects are integrated and how to leverage magic methods to create idiomatic Python classes (e.g., making your object iterable by implementing __iter__ and __next__) is a learning curve.

Error Handling Philosophy (EAFP vs. LBYL):

PHP Python
PHP developers often use functions like isset(), empty(), array_key_exists(), file_exists() before performing an operation (Look Before You Leap - LBYL). Python often encourages “Easier to Ask for Forgiveness than Permission” (EAFP), meaning you try an operation and catch exceptions if it fails (try...except).
Shifting from proactive checking to reactive error handling via exceptions can feel less safe initially, but it’s often more readable and efficient in Python for many cases.

Mutable Default Arguments in Functions:

PHP Python
Default arguments in PHP functions are re-evaluated each time the function is called (if the argument isn’t provided). Default arguments are evaluated once when the function is defined. If a default argument is a mutable object (like a list or dictionary), and it’s modified in the function, that modification persists across subsequent calls.
This is a classic Python “gotcha.” PHP developers would not expect
def foo(a, my_list=[]):
	my_list.append(a)
	print(my_list)

to behave the way it does across multiple calls. The standard Python idiom is to use None as a default and create the mutable object inside the function.

Read more →

The Immutability

Python’s string and tuple are immutable. For example

// replace a character in a string
$a = "12345";
$a[2] = "T";

You will see a TypeError when you the following in Python

# replace a character in a string (=> TypeError)
a = "12345"
a[2] = "T"

That is because strings and tuples are immutable in Python. Once created, their content cannot be changed. If so, how do we modify a string? Create a new one with updated value! Operations that seem to modify them actually create new objects in Python.

Read more →