This article describes the basic syntax of PythonVSPHP. in the past few days, I have been studying Python. in order to make it easier to remember and better compare and understand the advantages and disadvantages of the two languages in some situations, so it took some time to sort out the differences between the common syntax of Python and PHP.
I. case sensitivity
PHP:
-
All user-defined functions, classes, and keywords (such as if, else, and echo) are case insensitive;
-
All variables are case sensitive.
Python:
1. case sensitive.
II. variables
PHP:
1. start with the "$" identifier as defined in the format of $ a = 1
Python:
1. define the method as a = 1.
3. array/set
PHP:
// Define $ arr = array ('Michael ', 'Bob', 'Tracy '); // call method echo $ arr [0] // Michael // array append array_push ($ arr, "Adam"); // array ('Michael ', 'Bob ', 'Tracy ', 'Adam ');
Python:
# List method (variable) classmates = ['Michael ', 'Bob', 'Tracy'] # Call method print (classmates [0]) # 'Michael '# append the element classmates. append ('Adam ') # ['Michael', 'Bob ', 'Tracy', 'Adam '] # specify the insert location classmates. insert (1, 'Jack') # ['Michael ', 'Jack', 'Bob', 'Tracy'] # Delete the specified element classmates. pop (1) # ['Michael ', 'Bob', 'Tracy']
The Python array types are as follows:
-
List: linked list, ordered items, search by index, using square brackets "[]";
-
Test_list = [1, 2, 3, 4, 'oh']
-
Tuple: tuples. tuples combine various objects and cannot be modified. they are searched through indexes and use parentheses "()";
-
Test_tuple = (1, 2, 'hello', (4, 5 ))
-
Dict: a dictionary is a combination of keys and values. it is searched by keys without order. braces (braces) are used "{}";
-
Set: set, unordered, the element only appears once, automatic deduplication, using "set ([])"
-
Test_set = set (['Wang ', 'hu', 'Liu', 4, 'Wang '])
Print:
print(test_list) print(test_tuple) print(test_dict) print(test_set)
Output:
[1, 2, 3, 4, 'Oh'] (1, 2, 'Hello', (4, 5)) {'Liu': 4, 'Wang': 1, 'Hu': 2} set(['Liu', 4, 'Wang', 'Hu'])
IV. condition judgment
PHP:
If ($ age = 'Man ') {echo "male";} else if ($ age <20 and $ age> 14) {echo "female ";} else {echo "hmm ";}
Python:
Sex =''
If sex = 'Man ':
Print ('male ')
Elif sex = 'women ':
Print ('female ')
Else:
Print ('Here ~~ ')
V. Loop
PHP:
$ Arr = array ('a' => 'apple', 'B' => 'Samsung ', 'C' => 'Huawei ', 'D' => 'Google '); foreach ($ arr as $ key => $ value) {echo "array key :". $ key."
"; Echo" key value: ". $ value ."
";}
Python:
Arr = {'a': 'apple', 'B': 'Samsung ', 'C': 'Huawei', 'D ': 'Google '} # First for (key, value) in arr. items (): print ("This is key:" + key) print ("This is key value:" + value) # The second for key in arr: print ("This is key: "+ key) print (" This is the key value: "+ arr [key])
VI. Functions
PHP:
function calc($number1, $number2 = 10){ return $number1 + $number2;}print(calc(7));
Python:
def calc(number1, number2 = 10): sum = number1 + number2 return sum print(calc(7))
Please leave a message if you have any mistakes or suggestions.
The above is a detailed introduction to the basic syntax of Python vs php. For more information, see other related articles in the first PHP community!