Detailed introduction to Python VS PHP basic syntax

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

    1. All user-defined functions, classes, and keywords (such as if, else, Echo, and so on) are not case sensitive;

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

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

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

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

      • Test_dict = {' Wang ': 1, ' Hu ': 2, ' Liu ': 4}

  • 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

Related Article

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.