Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Comparison of syntax differences between PHP and Python codes

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "comparing syntax differences between PHP and Python code". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "through PHP and Python code comparison syntax differences" bar!

I. background

Artificial intelligence has been quite popular in recent years, the author has always wanted to learn; because I have been engaged in PHP development work, there is not much contact with Python, and I always face the embarrassment of weak foundation and earth shaking at critical times, for example, it is easy to get stuck when I encounter some deeper problems, so I am ready to learn from the beginning of Python.

The author thinks that there should be many people who are also familiar with PHP or Python language, not too familiar with another language, and have ideas to learn another language. I hope this article can be of some help to you.

Second, knowledge points

Recently completed a small assignment, the title requirements: through the Python code to enable users to enter a user name and password, after successful authentication to display welcome information, enter the wrong three times to exit the program.

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:

Overall code style

Variable naming convention

Constant naming convention

Annotation mode

Data type

Input and output

If statement uses the

While cycle

III. Python grammar

In Python, you need to strictly observe space indentation, otherwise an error will be reported; there is no need to use it after each line of code; the condition of the structure does not need to be used (), but there is no need for {} in the execution body.

3.1 Code example

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

< 3: #累计次数,用于循环条件 nn = 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 基本语法 下面从基本语法、数据类型、IF控制、while循环几个方面来聊聊Python代码中的一些规范 3.2.1 基础语法 变量: 在Python中变量以数字 字母 下划线组成,不能以数字开头,不能是python中的关键字,比如 while、if、elif、else、break、continue等,同样推荐使用驼峰命名和下划线命名两种规范命名格式 常量: 在Python中常量和变量在定义的方式上没有太大的区别,知识Python中约定俗成的使用全大写定义而已 注释: 在Python中,注释可以通过#来注释某行代码,也可以通过'''注释某段代码,比如'''注释内容''' 3.2.2 数据类型 在Python中,可以通过type(变量名)来获取变量的数据类型,经常使用到的数据类型有:布尔、整型、浮点型、字符串等;在布尔型中 真/True/1 假/False/0,非0的数字都是True; 在字符串中可以通过单引号和双引号两种方式定义,比如 a = '字符串' b = "字符串" 也可以使用 a = '''可以换行 这里有换行 这里也有换行 的字符串 ''' #或者三个双引号 b = """可以换行 这里有换行 这里也有换行 的字符串""" 来定义一大段字符串;字符串可以使用字符串 + 字符串的方式进行拼接,也可以使用 字符串 * 数字,将字符串重复凭借,比如 'abc' * 2,name得出的字符串便是abcabc 3.2.3 数据类型 在整型和浮点数据类型中加减乘除没有什么太特别,同样是使用+、-、*、/这四个符号,使用%可以的出余数;Python中有几个特殊的运算符,比如可以使用//进行整除,得出的结果不会有小数,如下代码所示: a = 10 // 3 # 得到的结果是 3 也可以使用**可以得到乘方,如下代码所示; b = 2 ** 2 # 得到的结果是 8 3.2.4 输入输出 输入输出:在Python中可以使用print关键字对变量进行打印输出,可以通过 input接收用户在终端中传递的参数,例如 inp = input('用户输入的时候看到的提示 :') 通过input方式接收的所有内容都是字符串类型,如果需要用来做运算需要对接收的变量进行类型转换;比如 a = int(变量名) 可以将变量转换成整型,也可以通过 f = float(变量名) 转换成浮点型; 3.3 IF控制 在Python中if语句使用的时候条件不需要使用()包括,执行体在也不需要使用{}包括,但执行体必须严格遵守缩进如下代码所示 # if a >

3 and bounded indentation 2: what to do after indentation meets condition 1 elif a > 3 and bounded indentation 3: indent does not meet condition 1 but meets condition 2 else: indent the above conditions do not meet what to do

3.4 while cycle

Using the while method in Python is similar to PHP, as shown in the pseudo code below

While a = = b: code executed in a loop

You can exit the loop with the keyword break, or you can skip a step in the loop using continue, as shown in the following code

I = 1 while I

< 10: i += 1 # 非双数时跳过输出 if i%2 >

0: continue # output even numbers 2, 4, 6, 8 print I # exit loop if I = 8: break

IV. PHP grammar

Space indentation does not need to be strictly observed in PHP, but corresponding to Python, it is usually used after each line of code; at the end, the condition of the structure also needs to be used (), and {} is also needed in the execution body.

4.1 Code example

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report