Python vs php basic syntax, pythonvs

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.