These days in learning Python, I usually learn in order to facilitate memory and better compare and understand the language between the pros and cons in some cases, so took some time to sort out some of the differences between Python and PHP common syntax.
First, case
Php:
All user-defined functions, classes, and keywords (such as if, else, Echo, and so on) are not case sensitive;
All variables are case-sensitive.
Python:
1. Case-sensitive.
Second, the variable
Php:
1. Start with "$" identifier as $a = 1 definition
Python:
1. Direct definition such as a = 1 mode
Three, arrays/collections
Php:
Define $arr = Array (' Michael ', ' Bob ', ' Tracy ');//Call echo $arr [0]//michael// array append array_push ($arr, "Adam");//Array (' Michael ', ' Bob ', ' Tracy ', ' Adam ');
Python:
# list mode (variable) classmates = [' Michael ', ' Bob ', ' Tracy ']# call mode print (Classmates[0]) # ' Michael ' # append element classmates.append (' Adam ') # [' Michael ', ' Bob ', ' Tracy ', ' Adam ']# specify the insertion position Classmates.insert (1, ' Jack ') #[' Michael ', ' Jack ', ' Bob ', ' Tracy ']# Delete the specified element Classmates.pop (1) #[' Michael ', ' Bob ', ' Tracy '
To say this, Python has the following array types:
List: Linked list, ordered items, searched by index, using square brackets "[]";
Test_list = [1, 2, 3, 4, ' Oh ']
Tuple: tuples, tuples combine various objects together, cannot be modified, searched by index, using parentheses "()";
Test_tuple = (1, 2, ' Hello ', (4, 5))
Dict: Dictionary, a dictionary is a set of keys (key) and a combination of values (value) that are searched by key (key), without order, using curly braces "{}";
Set: Set, unordered, element appears only once, auto-de-weight, use "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. Conditions of Judgment
Php:
if ($age = ' man ') { echo "male";} else if ($age < and $age >) { echo "female";} else{ echo "Hum";}
Python:
Sex = "
if sex = = ' man ':
Print (' Male ')
elif sex = = ' Women ':
Print (' female ')
Else
Print (' This ~ ~ ')
V. Circulation
Php:
$arr = Array (' a ' + = ' apple ', ' B ' + = ' Samsung ', ' c ' = ' Huawei ', ' d ' = ' Google '); foreach ($arr as $key + $value) { echo "Array key:" $key. " <br> "; echo "key corresponds to Value:". $value. " <br> ";}
Python:
arr = {' A ': ' Apple ', ' B ': ' Samsung ', ' C ': ' Huawei ', ' d ': ' Google '}# first type for (Key,value) in Arr.items (): print ("This is key:" + key) print ("This is the value of key:" + value) # The second type for key in arr: print ("This is key:" + key) print ("This is the value of key:" + Arr[key])
Vi. functions
Php:
Function Calc ($number 1, $number 2 = ten) { return $number 1 + $number 2;} Print (Calc (7));
Python:
Def calc (number1, number2 = ten): sum = number1 + number2 return sum print (Calc (7))
What is wrong with the place or good advice, welcome message