php python contrast

One, background

Artificial intelligence in recent years has been relatively fire, I always wanted to learn about it; because PHP has been engaged in development work, and not that much for Python contact is always at a critical time facing 基础不牢,地动山摇the embarrassment, such as in the face a little deeper these problems when it is easy to get stuck, so ready to learn from scratch, from entry Python;

I think there should be a lot of people are familiar with PHP or Python same language, the language of another is not too familiar with the idea of ​​learning another language, this article hopes to be able to have a little help.

Second, knowledge

Recently completed a small job, questions asked: achieved through Python code, allowing users to enter a user name and password, authentication is successful display a welcome message after unsuccessful attempts to exit the program.

In this article, can be summed up by this little work and the difference between PHP and Python syntax analysis, mainly deals with the following knowledge:

  1. The overall style of the code
  2. Variable naming convention
  3. Constant Naming Conventions
  4. Comment way
  5. type of data
  6. input Output
  7. if statements use
  8. while loop

Three, Python syntax

It requires strict adherence in Python spaces to indent, otherwise it will error; no need to use at the end of each line of code ;, the condition of the structure does not need to use (), you do not need to perform inside the body {};

Code Example 3.1

# -*- coding: utf-8 -*-
n = 0

while n < 3:
    #累计次数,用于循环条件
    n = n + 1
    #定义账号和密码
    uname = 'tangqingsong'
    pwd = '123123'
    #接收参数
    username = input('请输入用户名:')
    password = input('请输入密码:')

    #判断用户输入的账号和密码是否正确,正确将提示成功,并且退出循环体
    if uname == username and pwd == password:
        print ('恭喜你,登陆成功~')
        break
    #三次机会用完的时候,提示错误次数,并告知即将退出
    elif n == 3:
        print('已错误', n, '次,即将退出...')
    #如果在三次以内,提示还剩下几次机会
    else:
        print('抱歉,账号或密码不正确,你还有', 3 - n, '次机会')
复制代码

3.2 The basic syntax

The following aspects from the basic syntax, data types, the IF control, the while loop to talk some Python code specifications

3.2.1 basic grammar

Variables: In the Python variables with alphanumeric underscore, can not start with a number, not a python keywords, such as while, if, elif, else, break, continueand so on, also recommended hump naming and naming two specifications underscore naming format

Constants: constants and variables is not much difference in the way defined in Python, Python knowledge in all uppercase convention defined it

NOTE: In Python, annotations can #be annotated with a line of code, or by '''comments section of code, such as '' 'footnotes' ''

3.2.2 Data Types

In Python, can be obtained by type (variable name) data type of the variable, frequently used data types: Boolean, integer, floating point, string, and so on; True / True / 1 false in a boolean type / false / 0, a non-zero number is True;

May be defined by single and double quotes in a string of two ways, such as

a = '字符串'
b = "字符串"
复制代码

You can also use

a = '''可以换行
    这里有换行
  这里也有换行
的字符串
'''
#或者三个双引号

b = """可以换行
    这里有换行
  这里也有换行
的字符串"""
复制代码

A large segment defined character string; + embodiment can use string string string splicing, may be used 字符串 * 数字, with the repeated string, such as 'abc' * 2, name character string is derived abcabc

3.2.3 Data Types

Data in integer and floating point arithmetic type nothing too particular, the use of the same +, -, *, /four symbols, use %can be a number of I; the Python There are several special operator, such as can //be divisible, the result will not have a decimal, as shown in the following code:

a = 10 // 3 

# 得到的结果是 3
复制代码

Use may also be **possible to obtain power, the following code shown;

b = 2 ** 2 

# 得到的结果是 8
复制代码

3.2.4 Input Output

O: print keyword can be used in Python print output variable, parameters can be passed in a terminal receiving through user input, e.g.

inp = input('用户输入的时候看到的提示 :')
复制代码

All content received by way of input is a string type, if required to do operations require variables received type conversion; example

a = int(变量名)
复制代码

It can be converted to an integer variable, can also

f = float(变量名)
复制代码

Converted to float;

3.3 IF control

In Python conditions when used without the use of an if statement ()includes executable in no need to use {}include, but executable must be strictly adhered to indent the code shown below

# if a >3 and b==2:
    缩进 满足条件1之后要做的事情
  elif a>3 and b==3:
    缩进 不满足条件1但满足条件2之后要做的事情
  else:
    缩进 上面的条件都不满足要做的事情
复制代码

3.4 while loop

While using a method similar to PHP in Python, the following is pseudocode

while a == b:
    循环执行的代码
复制代码

Key words can be used breakto exit the loop, you may be used continueto skip one step in the cycle, the following code shown in FIG.

i = 1
while i < 10:   
    i += 1
    # 非双数时跳过输出
    if i%2 > 0:     
        continue
    # 输出双数2、4、6、8
    print i         
    # 当条件为8时候退出循环
    if i == 8:
        break
复制代码

Four, PHP syntax

In PHP does not require strict compliance with spaces indentation, but the corresponding Python and it is often desirable to use at the end of each line ;ending condition also requires the use of structures (), which also need to perform the body {};

4.1 code examples

<?php

$n = 0;

while ($n < 3) {
    #累计次数,用于循环条件
    $n = $n + 1;
    #定义账号和密码
    $uname = 'tangqingsong';
    $pwd = '123123';
    #接收参数
    fwrite(STDOUT, '请输入用户名:');
    $username = trim(fgets(STDIN));
    fwrite(STDOUT, '请输入密码:');
    $password = trim(fgets(STDIN));

    #判断用户输入的账号和密码是否正确,正确将提示成功,并且退出循环体
    if ($uname == $username and $pwd == $password) {
        print_r('恭喜你,登陆成功~');
        break;
        #三次机会用完的时候,提示错误次数,并告知即将退出
    } elseif ($n == 3) {
        print_r("已错误{$n}次,即将退出...");
    } else {
        #如果在三次以内,提示还剩下几次机会
        $j = 3 - $n;
        print_r("抱歉,账号或密码不正确,你还有{$j}次机会");
    }
}
复制代码

4.2 The basic syntax

The following aspects from the basic syntax, data types, the IF control, the while loop to talk some PHP code specifications

4.2.1 basic grammar

Variables: In PHP variables alphanumeric underscore, must $begin with a symbol, and the first character can not start with a number, because the variables are in PHP start with $, so basically there is no keyword argument, but writing code try not to cover variable system can also recommend the hump naming and naming two specifications underscore naming format

Constant: In PHP constant similar convention defines all uppercase only, in the manner defined by a dedicated custom format, such as the definition DAXIAof the value tangqingsongwhen the code shown below

const   DAXIA = 'tangqingsong';
复制代码

Note: In PHP, annotations can #be annotated with a line of code may be used //define the line of code, or by /****/comments section of code, the following code shown in FIG.

#这是行注释

//这是行注释,一般习惯是使用此种方式

/**
 *  块注释,一般在自定义函数和类方法的时候使用
 */
复制代码

4.2.2 Data Types

In PHP, by var_dump (variable name) and the value of simultaneously printing variable type, frequently used data types: boolean, integer, floating point, string and the like; the same in the Boolean True / True / 1 False / False / 0, non-zero digit is True;

May be defined by a string of single and double quotation marks in two ways, it can not be put in single quotes variable, but the quotes are possible, the following code shown in FIG.

$n = 123;

a = '字符串';
b = "字符串{$n}";
复制代码

When we definition block text, it can also be used delimiter embodiment, the following code shown in FIG.

$a = <<<EF

这里是大文本内容,可以写任意文本,EF是自定义的,大家也可以把EF写成DAXIA,但是必须前后对应,后面的必须定格,后面一个“EF”不能用空格之类的字符;

EF;
复制代码

The string can be a symbol .for splicing, as shown in the following code:

$name = 'daxia' . 'tangqingsong';
复制代码

4.2.3 Data Types

In PHP integer and floating point data types, Math nothing too special, as is the use +, -, *, /four symbols, use %can out of the remainder;

4.2.4 Input Output

O: can be used in PHP print_r keyword variable printouts little trouble in receiving the standard input, by the need to fwriteparameters received in the terminal user is transmitted, and then by fgetsthe function value of the variable is taken out, but also by trimthe space behind the filter, e.g.

    fwrite(STDOUT, '请输入用户名:');   
    $username = trim(fgets(STDIN));
复制代码

All content received by way of the above code is a string type, but PHP is a weakly typed language, a data type is not powerful variable, so that in most cases without the need for the type of conversion, if necessary use a type conversion method and Python general type, as shown in the following code

a = intval(变量名)
复制代码

It can be converted to an integer variable, can also

f = floatval(变量名)
复制代码

Converted to float;

4.3 IF control

if condition: PHP in use if statement in the code shown below

<?php
if (a >3 and b==2){
    满足条件1之后要做的事情
} elseif (a>3 and b==3){
    不满足条件1但满足条件2之后要做的事情
} else {
    上面的条件都不满足要做的事情
}
复制代码

4.4 while loop

while loop: while in PHP conditions must be used ()include, must be used in the executable {}includes, without demanding executable indentation, but for aesthetic purposes, usually indented, the following is a pseudocode

<?php
while ($a == $b){
    //循环执行的代码
}
复制代码

Key words can be used breakto exit the loop, you may be used continueto skip one step in the cycle, the following code shown in FIG.

<?php

$i = 1;
while ($i < 10) {
    $i += 1;
    //非双数时跳过输出
    if ($i % 2 > 0) {
        continue;
    }
    
    //输出双数2、4、6、8
    print_r($i);
    
    //当条件为8时候退出循环
    if ($i == 8) {
        break;
    }
}
复制代码

 

Guess you like

Origin blog.csdn.net/jiangjunshow/article/details/95244009