Python and PHP: Main Syntax Differences

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome to Python and machine learning daily if you jump to python from PHP background like myself I want to invite you to this video where I will explore the differences the fundamental differences in syntax between PHP and Python and this will be based on my course that I've prepared earlier on my python ml.com which is called python 101 for PHP developers I will not read that course in full in this video I will focus on the differences in syntax how Python and PHP treat different things like variable types values functions loops and other fundamental stuff for programmers so by the end of this video you should be prepared for two things first read python code and understand it and then second write simple Python scripts avoiding stupid errors and typos and IDE warnings or at least you would understand them and also I will link the full course in the description below which will contain more information about python functionality that you need to know as a PHP developer but now let's dive into syntax differences between PHP and python first variables you have to have dollar sign in PHP and you don't need to have that in Python then end of statement semicolon in PHP just end of the line in Python then we have if statement in PHP it's with curly braces in Python it's colon and then indentation so there may be another print down below with indentation of tabs or spaces and that would be considered in the same block also with if statement difference is else if and L if in Python and finally with if statement you don't need to have brackets here for the condition it's just a is bigger than five unless you have more complex conditions then you may have brackets but it's not necessary in PHP this is mandatory another important difference between PHP and python is how they deal with different types of variables so for example in PHP it's a very loose operation so you may have string as a variable and then do plus 5 and the result will be calculated automatically transforming that 10 as a string to a number in Python that wouldn't be allowed in Python you still don't need to Define string or integer or something python would automatically transform that or cast that to a string and this one would be a number but if we launch that code we will get an error can only concatenate string to string and even pie charm here is underlining with an expected type so if you want to perform such operation you need to specifically cast number to in like this and then if you launch the result is successful without any errors another two differences I wanted to show you is how to concatenate the string so in PHP we use dot to concatenate two strings in Python we use the plus sign in Python there are other methods to concatenate the strings as well like format or so-called F strings but the general syntax is this difference and also comments for comments in PHP we use double slash in Python we use the hash symbol and for multi-line comments in PHP we use slash asterisk in the beginning and then asterisk slash at the end in Python people usually just use hash on every line there is a certain syntax that would be interpreted as multi-line comments but I don't even want to show that because I rarely see that in practice another kind of small but important syntax difference is about logical operators and we get back to if statements here in PHP there are these symbols for and or and not operations in Python they are used as English words so and or and not and you can see the example python code here below the next quite important difference is that python treats strings as objects so if you want to perform some operations on the string it's usually string. method in PHP it's the other way around because there are methods that accept string as a parameter in Python it looks like this so you have a string you don't even need to define the variable and then you may call the methods like this one with this result of this script another two differences two in one I wanted to show you is how python treats the values of Bine and null values so similarly to PHP you can assign true or false two variables but if we launch that program we will have an error name true is not defined did you mean true and you can see uppercase here so the values of Bine in Python are case sensitive so true is okay false is also okay but lower case would not be recognized and then the null values in Python there's no null value or there is but it is called differently if launch that null will not be defined or recognized the value of empty variable is called nonone in Python now if we launch we shouldn't get get any errors now let's talk about loops differences between Python and PHP and I will read the examples from my tutorial on python ml dailyc so this is for Loop in Python which is equivalent of for each in PHP in case of the list which is actually by the way in Python is called list not array but if you call it an array everyone will understand you anyway so for variable in list or if you want to have iteration like pH PHP for Loop this would be the PHP syntax and this is the syntax in Python range function may be simple with just one variable or more complicated with start stop and step like this next while loop is pretty similar to if statement that we've seen already in the beginning of this video so no brackets no braces and also important difference that I didn't mention is python does not have Plus+ operator for increment or minus -4 decrement the syntax is plus equal 1 or whatever is your increment which may be two or more next let's talk about functions in PHP you Define function something like this in Python it's a different keyword called def Define and then similarly to loops and if statements no curly braces here colon instead and then indentation here to Define that it's inside the function also important in PHP P it's called named arguments in Python it's also very popular because if you have for example four parameters four arguments and you call that function like this another developer wouldn't understand what is that true what is that 20 so what often is done you can call the variables like this with their names specified especially with machine learning libraries there are for example methods with 10 or 12 parameters or arguments and you need to Define only three of them so this is a very widely used syntax option another important difference about function is something that python does fundamentally differently from PHP the argument parameters are passed by reference which means they change the value and the value is changed outside of the function so you have my list variable then you call the same my list twice and it performs the same operation tce twice to the same list in PHP it wouldn't do that in PHP this variable would be considered as local variable to the function and wouldn't change any of the value of outside variable if you wanted to achieve the same behavior in PHP you would need to pass that as reference with M syntax in Python this behavior is the default and the final difference with function you would notice that you would get warnings about styling if you don't pass a few spaces a few line spaces between the functions so python standard code styling is two empty lines between the functions so yeah these are the main differences the most probably important fundamental differences if you jump from PHP to python this should be enough for you to understand python code if you read it or to avoid silly typing mistakes when writing simple python of course there are more differences in terms of capabilities of python so there are functions that don't exist in PHP or for example types of data like Tuple but this is not fundamentally about differences this is about python behavior and I will link the full course the text course that partially I'm reading here in this video I will link that in the description below you can see there are more lessons about for example CSV libraries and those complex data types like Tuple that I mentioned so I advise you to read all that course it's free and it should give you a good start with writing python code and for more python tips of course subscribe to this channel because I will keep shooting videos about python so you will definitely learn more syntax tips and tricks in the future videos and see you guys in those other videos
Info
Channel: Python ML Daily
Views: 2,022
Rating: undefined out of 5
Keywords:
Id: Tbhw4ZD0I88
Channel Id: undefined
Length: 9min 47sec (587 seconds)
Published: Tue Jan 09 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.