简体   繁体   English

Python 函数和运算符

[英]Python Functions and Operators

I am a beginner learning Python programming languages and I need you to explain in simple terms how an operator works with the given example.我是初学者,正在学习 Python 编程语言,我需要您用简单的术语解释操作员如何使用给定的示例。

I stumbled upon this thing called assignment operators:我偶然发现了这个叫做赋值运算符的东西:

x=1

y=3

x!=y

From the given example, the example is:从给定的例子,例子是:

x=1 and means x=1 x=1表示x=1

x+=1 and means x=x+1 x+=1表示x=x+1

x-=2 and means x=x-2 x-=2表示x=x-2

x*=5 and means x=x*5 x*=5表示x=x*5

  1. When should assignment operators be used?什么时候应该使用赋值运算符? In what kinds of situation, (with an example/scenario will you use these operators to solve?在什么样的情况下,(用一个例子/场景你会使用这些运算符来解决?

  2. How do you apply a hexidecimal in a given situation or octal string?您如何在给定情况或八进制字符串中应用十六进制? The following three functions I have trouble understanding how they work.以下三个函数我很难理解它们是如何工作的。 If possible, explain it in simple terms.如果可能,请用简单的术语解释。 Thank you!谢谢!

hex(x) or oct(x) hex(x)oct(x)

chr(u)

ord(x)

Assignment operators are just shorthand, and you would usually use them in some sort of reduction operator, eg赋值运算符只是简写,您通常会在某种归约运算符中使用它们,例如

dot_prod = 0
for a,b in zip(vector1,vector2):
  dot_prod += a * b

chr() and ord() are mirror operations to convert characters to their ASCII index and back, eg ord('A') is 65, etc. For any given character, chr(ord(x)) == x chr() 和 ord() 是将字符与其 ASCII 索引相互转换的镜像操作,例如 ord('A') 是 65,等等。对于任何给定的字符, chr(ord(x)) == x

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM