Python vs PHP or Python through the eyes of a PHP programmer

Sorry for my English! English is not my native language. One of the reasons to create this blog is to improve my English writing. So I will be highly obliged if you will help me with this. If you find a grammar error on this page, please select it with your mouse and press Ctrl+Enter.

Recently, I started learning Python a bit. It is a very popular programming language today, ranking among the top three most popular languages both in terms of the number of questions on various websites like Stack Overflow and the number of repositories on GitHub.

Or here's a comparison using Google trends
https://trends.google.com/trends/explore?date=today%205-y&geo=UA&q=%2Fm%...

Or here is information from the TIOBE website, they use a lot of parameters for analysis:
https://www.tiobe.com/tiobe-index/
According to their data, Python currently holds the top position in the rankings, surpassing even C++, C, C#, and Java. Meanwhile, PHP is in the 8th position (it was 9th a year ago, so it has shown some growth over the year). Over the past 15 years, Python has risen from the 7th position to the 1st, while PHP has slowly descended from the 5th to the 8th or 9th position. You can find all the information in the link above.

 
What is Python used for?:

  1. For learning: If in the past, schoolchildren and students were taught Pascal and BASIC, which are not used anywhere today, now everyone has switched to Python, and it's the right choice. Python is a simple, versatile language that is actively evolving and widely used. This might be why the language has seen such rapid growth. Yesterday's students, who learned the basics of Python during their education, now use it in their work. 
  2. For various workflow automation: Python is used for tasks such as web scraping, batch processing of Excel documents, making API calls, and thousands of other small tasks. Python has thousands of ready-made libraries that can be used for everything from parsing data to facial recognition, speech synthesis, and machine learning.
  3. For robotics: Python is used in robotics for programming devices like LEGO Mindstorms or Arduino boards, connecting sensors, motors, cameras, and more. You can write Python programs to control these devices, creating remote-controlled vehicles, quadcopters, home alarms, and a variety of other interesting projects.
  4. For data science, machine learning, neural networks, etc.: Python is the top language in this field, with a vast number of libraries available for these tasks. Some of these libraries use C++ for resource-intensive tasks. While there are alternatives in Node.js, PHP has very limited support in this area.
  5. Web Development and API Servers: Python offers frameworks like Django for web development. FastAPI is another popular choice for creating API services. While Django has many modules, it may seem somewhat limited in functionality when compared to frameworks like Laravel.
  6.  Console Applications
  7. Games: Python is used for creating 2D games or specific components for larger games, such as data processing.
  8. Desktop Applications
  9. Mobile Applications: Python is not as common in this field, as there are specialized tools for mobile app development. However, it's increasingly being used in mobile development as well.
  10. Python's versatility is expanding. Microsoft has integrated it into Excel, and it's being added to web browsers. It seems that Python will soon have a presence in nearly every field.

The Advantages of Python

  1. Ease of Learning: Python is easy to learn and is an excellent choice as a first programming language. It often reminds me of the days when I was learning Pascal in college.
  2. Vast Number of Libraries: There is an extensive collection of libraries for various use cases.
  3. Large Community: Python has a massive community of users and developers.
  4. Active Development: Python is actively and continuously evolving
  5. In the field of Machine Learning, it has virtually no alternative.

  6. Many Job Opportunities: There are many job opportunities available for Python programmers.

The Disadvantages of Python

  1. Speed: Python is relatively slow, like many scripting languages. Even the latest versions of PHP, such as 7.8, outperform Python in terms of speed. However, for the tasks for which Python is used, its speed is generally sufficient.
  2. Lack of Good Object-Oriented Programming (OOP): Python lacks proper private classes and methods, especially protected ones. It also lacks interfaces, and abstract classes are implemented as something of a workaround through third-party modules. It's worth noting that PHP, particularly in its earlier versions, was more functional in this regard.
  3. Mixed Quality of Libraries: While Python has some excellent libraries written by skilled programmers, it also has a fair share of poorly written code and inexperienced programmers since Python is used by a wide range of developers.

A little comparison of Python and PHP syntax

File extensions *.py
There's no need to open any PHP tags like <?php in PHP; you just open the file and write code.
Each file is a module, and you can load or import not the entire file but only a specific function or class from it, for example:
import random
or
from random import randint
There are no $ signs for variables, no ; at the end of each line, no {} for classes, functions, conditions, and loops. Parentheses () are also not written for conditions and loops; instead, Python uses indentation.
Not everyone likes this, but it makes the code very clear and it's impossible to write incorrectly formatted code.

def my_func(x):
  #comment
  return x*x

this is a function, instead of function - def

a function can return more than one value, for example

return x,x*2,x*3

and then we do

x,y,z = my_func(x)

there is no operator ++ instead they just write

 x +=1

No ternary operator

$x = true ? 1 : 0;

instead they do it like this:

x = 1 if True else 0

it seems logically even more correct - 1 if true otherwise 0, but not usual)

True and False with a capital letter

instead of NULL - None

In classes I already wrote there are no normal private, protected, interface, traits, etc. And the fact that there are rather some kind of crutches. That is, in essence, there is no normal encapsulation. Rather, there is a set of rules that programmers must follow, but if desired, this is all bypassed and private variables can be easily accessed. Fans of the language unanimously repeat that Python is the best language, and since it is not there, then it is not needed there. Until recently, they said the same thing about dynamic typing, and then (like with version 3.6) typing was introduced into the language (though it’s not necessary to use it). By the way, the same thing literally word for word can be heard from opencart fans)) opencart is the best, and since it’s not there, it means it’s not needed here)) I categorically disagree with this, since the presence of some functionality does not make to use it, but it gives a choice, and its absence practically deprives any choice at all.

But at the same time, classes in Python have one very interesting feature - there are a lot of magic methods (which begin and end with __), which allow you to perform almost all (or even all) possible operations with this class, various +, -, addition, division, exponentiation, comparison, conversion to number, string, array, etc.
for example you create a class method

__add__()

with your own logic and you can do this

object3 = object1+object2

Classes also support multiple inheritance, which PHP does not support, but PHP supports multiple implementation of interfaces and traits, which Python does not.
In Python, the constructor and destructor method are __init__ and __del__ respectively
Vice versa:
In Python, self points to a class object (analogous to $this in PHP), and in PHP to the class itself (for accessing class variables and class methods)
Python has the concept of a static class method and a class method. The first is just a function that does not have access to class variables, and the second does, just the second option works like a static method in PHP.

Instead of one array or [] as in PHP
There are as many as 4 types of array types:
list or list, a regular one-dimensional array

[1,2,"foo","bar"]

tuple (tuple) - very similar to a list, only it cannot be modified. Through it, several values are returned from the function. Through it you can also easily swap the values of variables, for example

x,y=y,x

and that’s it) now the variable x contains the value from y and vice versa.

(1,2,"foo","bar")

set - has all elements unique

{1,2,"foo","bar"}

dictionary - a dictionary, an analogue of an associative array in PHP

{1: 2, "foo": "bar"}

there are slices, a very convenient thing, for example, getting array elements from 1 to 3

new_list = old_list[1:3]

or will reverse all the elements, who was first will become last, etc.

seq[::-1]

the same thing with strings, since a string works like a list, you can make slices, use loops over characters, etc.

string = 'строка'
string[1:3] # 'тр'

or this kind of magic is used very often:

squares = [i * i for i in range(10)]
print(squares)
#[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

That is, in one line of code you can create a list, set, word

And quite a lot like magic

In short, the syntax is all, if I made a mistake somewhere, please correct someone who is more familiar with this language or add if there is anything, since I am just starting to learn it, it is quite possible that I missed something.

Do I need to learn this language? It seems to me - definitely - yes.

  1. it’s simple, you can learn it quickly enough, especially since there are just a bunch of free courses on Python, videos on YouTube, etc.
  2. It's fun to write code on it, you just open the editor and write))
  3. it is very popular and there are many vacancies
  4. there is a huge community and tons of libraries for all occasions, this opens up great opportunities
  5. you can work in the field of machine learning and neural networks, but now this is generally a very popular area and Python seems to have no competition here, it has conquered almost everything here))
  6. very useful for overall development
  7. very useful for a resume, even if you are a PHP programmer, having Python as a second language on your resume will be a huge plus for you as a specialist
  8. It seems to me that it is very well suited for OpenCart developers, there are no complex structures like in Laravel, there is simple code and syntax, well, almost like in OpenCart itself :)

Photo taken from https://hackr.io/

Tags: 

Add new comment

CAPTCHA
Spam protection
Target Image