1. Background
Artificial intelligence has been quite popular in recent years, and I have always wanted to learn about it. ; Because I have been engaged in PHP development work, I don’t have much contact with Python. I always face the embarrassment of a weak foundation and shaking the earth at critical moments. For example, when I encounter a slightly deeper problem, I am easily stuck, so Prepare to learn from the beginning with Python;
The author thinks that there should be many people who are also familiar with PHP or Python language, but are not too familiar with the other language. They have the idea to learn another language. I hope that through this article The article can be of some help to everyone.
2. Knowledge points
I recently completed a small assignment. The topic requirements: Implemented through Python code, allowing users to enter username and password, and display a welcome message after successful authentication. , exit the program after three incorrect entries.
In this article, we will summarize and analyze the grammatical differences between PHP and Python through this small assignment, which mainly involves the following knowledge points:
1. Overall code style
2. Variable naming convention
3. Constant naming convention
4. Comment method
5. Data type
6. Input and output
7. Use of if statement
8. While loop
3. Python syntax
Spaces need to be strictly observed in Python Indent, otherwise an error will be reported; there is no need to use; after each line of code, there is no need to use () in the conditions of the structure, and there is no need to use {} in the execution body;
3.1 Code Example
# -*- 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 Basic syntax
Let’s talk about some specifications in Python code from the aspects of basic syntax, data types, IF control, and while loops
3.2.1 Basic syntax
Variables: Variables in Python are composed of numbers, letters and underscores. They cannot start with numbers and cannot be keywords in python, such as while and if. , elif, else, break, continue, etc. It is also recommended to use two standard naming formats: camel case naming and underline naming
Constant: There is not much difference in the way of defining constants and variables in Python. Knowledge of Python The convention is to use all uppercase definitions
Comments: In Python, comments can be used to comment out a line of code through #, or they can be used to comment out a certain piece of code through ''', such as '''comment content'''
3.2.2 Data type
In Python, you can get the data type of a variable through type (variable name). The commonly used data types are: Boolean , integer type, floating point type, string, etc.; in Boolean type, true/True/1 false/False/0, non-0 numbers are all True;
can be used in strings through single quotes and Double quotes are defined in two ways. For example,
a = '字符串' b = "字符串"
can also be used to define a large string. Strings can be spliced using string strings, or using characters. String * number, repeat the string by, for example, 'abc' * 2, the string obtained by name is abcabc
3.2.3 Data typeThere is nothing special about addition, subtraction, multiplication and division in integer and floating-point data types. The same four symbols, -, *, / are used, and % can be used to get the remainder; there are several special operators in Python, for example, you can use //Perform integer division, and the result will not have decimals, as shown in the following code:
a = '''可以换行 这里有换行 这里也有换行 的字符串 ''' #或者三个双引号 b = """可以换行 这里有换行 这里也有换行 的字符串"""
You can also use ** to get the power, as shown in the following code;
a = 10 // 3 # 得到的结果是 33.2.4 Input and output
Input and output: In Python, you can use the print keyword to print out variables, and you can receive parameters passed by the user in the terminal through input, such as
b = 2 ** 2 # 得到的结果是 8
All content received through the input method is of string type. If it needs to be used for calculations, the received variables need to be type converted; for example,
inp = input('用户输入的时候看到的提示 :')
can convert the variable into an integer type, or by
a = int(变量名)
Convert to floating point type;
3.3 IF controlWhen using the if statement in Python, the condition does not need to include (), execute There is no need to use {} to include the body, but the execution body must strictly abide by the indentation as shown in the following code
f = float(变量名)3.4 while loop
Use the while method in Python Similar to PHP, as shown in the following pseudo code
# if a >3 and b==2: 缩进 满足条件1之后要做的事情 elif a>3 and b==3: 缩进 不满足条件1但满足条件2之后要做的事情 else: 缩进 上面的条件都不满足要做的事情
You can use the keyword break to exit the loop, or you can use continue to skip a certain step in the loop process, as shown in the following code
while a == b: 循环执行的代码4. PHP syntax
In PHP, there is no need to strictly adhere to space indentation, but corresponding to Python, it is usually necessary to use; at the end of each line of code, and the conditions of the structure are also You need to use (), and {} is also required in the execution body;
4.1 Code example
i = 1 while i < 10: i += 1 # 非双数时跳过输出 if i%2 > 0: continue # 输出双数2、4、6、8 print i # 当条件为8时候退出循环 if i == 8: break4.2 Basic syntax
Let’s talk about some specifications in PHP code from the aspects of basic syntax, data types, IF control, and while loops
4.2.1 Basic syntax
变量: 在PHP中变量以数字 字母 下划线组成,必须以$符号开头,且第一个字符不能以数字开头,在PHP中因为变量都是以$开头,所以基本上没有关键字的说法,但是写代码的时候尽量不要覆盖系统变量即可,同样推荐使用驼峰命名和下划线命名两种规范命名格式
常量: 在PHP中常量同样约定俗成的使用全大写定义而已,在定义的方式有专门的定义格式,比如定义DAXIA的值为tangqingsong时候,代码如下所示
const DAXIA = 'tangqingsong';
注释: 在PHP中,注释可以通过#来注释某行代码,也可以使用//定义行代码,也可以通过/****/注释某段代码,如下代码所示
#这是行注释 //这是行注释,一般习惯是使用此种方式 /** * 块注释,一般在自定义函数和类方法的时候使用 */
4.2.2 数据类型
在PHP中,可以通过var_dump(变量名)同时打印变量类型和值,经常使用到的数据类型有:布尔、整型、浮点型、字符串等;同样在布尔型中 真/True/1 假/False/0,非0的数字都是True;
在字符串中可以通过单引号和双引号两种方式定义,单引号中不能放变量,但是双引号是可以的,如下代码所示
$n = 123; a = '字符串'; b = "字符串{$n}";
当大家定义块文本内容的时候,也可以使用定界符方式,如下代码所示
$a = <<字符串可以使用符号 . 进行拼接,如下代码所示:
$name = 'daxia' . 'tangqingsong';4.2.3 数据类型
在PHP中整型和浮点数据类型中加减乘除没有什么太特别,同样是使用+、-、*、/这四个符号,使用%可以的出余数;
4.2.4 输入输出
输入输出:在PHP中可以使用print_r关键字对变量进行打印输出,在接收标准输入方面稍微麻烦,需要先通过fwrite接收用户在终端中传递的参数,然后再通过fgets函数将变量值取出来,还需要通过trim将后面的空格过滤,例如
fwrite(STDOUT, '请输入用户名:'); $username = trim(fgets(STDIN));通过上面代码方式接收的所有内容都是字符串类型,但是PHP是弱类型语言,并不强大变量的数据类型,所以大部分情况无需进行类型转换,如果需要用到类型转换方法和Python大体类型,如下代码所示
a = intval(变量名)可以将变量转换成整型,也可以通过
f = floatval(变量名)转换成浮点型;
4.3 IF控制
if条件:在PHP中if语句的使用方法如下代码所示
3 and b==2){ 满足条件1之后要做的事情 } elseif (a>3 and b==3){ 不满足条件1但满足条件2之后要做的事情 } else { 上面的条件都不满足要做的事情 }4.4 while循环
while循环:在PHP中while的条件必须使用()包括,执行体在也必须使用{}包括,执行体不要求严格的缩进,但是为了美观,一般都会缩进,如下为伪代码所示
可以使用关键词break退出循环,也可以使用continue跳过循环过程中的某一步,如下代码所示
0) { continue; } //输出双数2、4、6、8 print_r($i); //当条件为8时候退出循环 if ($i == 8) { break; } }