PythonVSPHP Basic syntax

Source: Internet
Author: User
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:

  1. All user-defined functions, classes, and keywords (such as if, else, and echo) are case insensitive;

  2. 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:

  1. List: linked list, ordered items, search by index, using square brackets "[]";

    • Test_list = [1, 2, 3, 4, 'oh']

  2. 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 "{}";

    • Test_dict = {'Wang': 1, 'hu': 2, 'liu': 4}

  • 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!

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.