Home  >  Article  >  Backend Development  >  Detailed introduction to the basic syntax of Python VS PHP

Detailed introduction to the basic syntax of Python VS PHP

高洛峰
高洛峰Original
2017-03-20 10:10:371377browse

I have been learning Python these days. In order to facilitate my memory and better compare and understand the advantages and disadvantages of the two languages ​​​​in certain situations, I spent some time sorting out Python and PHP. Some differences in common syntax.

1. Case

PHP:

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

  2. All variables are case-sensitive.

Python:

1. Case sensitive.

2. Variables

PHP:

1. Start with the “$” identifier such as $a = 1 and define

Python:

 1. Direct definition such as a = 1 method

3. Array/Collection

PHP:

// 定义
$arr = array('Michael', 'Bob', 'Tracy');

// 调用方式
echo $arr[0]
// Michael

//  数组追加
array_push($arr, "Adam");
// array('Michael', 'Bob', 'Tracy','Adam');

Python:

# list方式(可变)
classmates = ['Michael', 'Bob', 'Tracy']

# 调用方式
print(classmates[0])
# 'Michael'

# 末尾追加元素
classmates.append('Adam')
# ['Michael', 'Bob', 'Tracy', 'Adam']

# 指定插入位置
classmates.insert(1, 'Jack')
#['Michael', 'Jack', 'Bob', 'Tracy']

# 删除指定元素
classmates.pop(1)
#['Michael', 'Bob', 'Tracy']

What to say here Let’s take a look, Python’s array types are as follows:

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

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

  • ##tuple: Tuple, the tuple will be diverse Objects are collected together and cannot be modified. To search through the index, use brackets "()";

    • ##test_tuple = (1, 2, 'Hello', ( 4, 5))
    • dict: Dictionary. A dictionary is a combination of keys and values. Searches are performed by keys. There is no order. Use large numbers. Brackets "{}";
    • ##test_dict = {'Wang' : 1, 'Hu' : 2, 'Liu' : 4}
      • set: Set, unordered, elements appear only once, automatically deduplicate, 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'])
    4. Conditional judgment

    PHP:

    if($age = 'man'){
        echo "男";
    }else if($age < 20 and $age > 14){
        echo "女";
    }else{
        echo "嗯哼";
    }

    Python:

    <p>sex = &#39;&#39;<br/>if sex == &#39;man&#39;:<br/>    print(&#39;男&#39;)<br/>elif sex == &#39;women&#39;:<br/>    print(&#39;女&#39;)<br/>else:<br/>    print(&#39;这~~&#39;)<br/></p>

    5. Loop

    PHP:

    $arr = array(&#39;a&#39; => &#39;苹果&#39;, &#39;b&#39; =>&#39;三星&#39;, &#39;c&#39; => &#39;华为&#39;, &#39;d&#39; => &#39;谷歌&#39;);
    foreach ($arr as $key => $value){
        echo "数组key:".$key."<br>";
        echo "key对应的value:".$value."<br>";
    }

    Python:

    arr = {&#39;a&#39;: &#39;苹果&#39;, &#39;b&#39;: &#39;三星&#39;, &#39;c&#39;: &#39;华为&#39;, &#39;d&#39;: &#39;谷歌&#39;}
    
    # 第一种
    for (key,value) in arr.items():
        print("这是key:" + key)
        print("这是key的value:" + value)
    
    # 第二种
    for key in arr:
        print("这是key:" + key)
        print("这是key的value:" + arr[key])

    6. Function

    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

    The above is the detailed content of Detailed introduction to the basic syntax of Python VS PHP. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn