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

Detailed introduction to the basic syntax of Python VS PHP

高洛峰
Release: 2017-03-20 10:10:37
Original
1444 people have browsed it

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');
Copy after login

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']
Copy after login

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']

  2. ##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)
      Copy after login
    Output:

    [1, 2, 3, 4, 'Oh']  
    (1, 2, 'Hello', (4, 5))  
    {'Liu': 4, 'Wang': 1, 'Hu': 2}  
    set(['Liu', 4, 'Wang', 'Hu'])
    Copy after login

    4. Conditional judgment

    PHP:

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

    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>
    Copy after login

    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>";
    }
    Copy after login

    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])
    Copy after login

    6. Function

    PHP:

    function calc($number1, $number2 = 10)
    {
        return $number1 + $number2;
    }
    print(calc(7));
    Copy after login

    Python:

    def calc(number1, number2 = 10):
        sum = number1 + number2
        return sum
        
    print(calc(7))
    Copy after login
    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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template