Python vs php basic syntax, pythonvs
I have been studying Python over the past few days. It takes some time to learn the advantages and disadvantages of the two languages in some situations to facilitate memory and better comparison and understanding, I sorted out some differences between the common syntax of Python and PHP.
I. Case sensitivity
PHP:
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:
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. "<br>"; echo "key value :". $ value. "<br> ";}
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))
If you have any mistakes or good suggestions, please leave a message.