2.3.1.1 Operators - data manipulation tools Público

2.3.1.1 Operators - data manipulation tools

David Khieu
Curso por David Khieu, actualizado hace más de 1 año Colaboradores

Descripción

2.3.1.1 Operators - data manipulation tools

Información de los módulos

Descripción

Python as a calculator
Sin etiquetas
Python as a calculator Now, we're going to show you a completely new side of the print() function. You already know that the function. You already know that they function is able to show you the values of the literals passed to it by arguments.  In fact, it can do something more. Take a look at the snippert:  print(2+2) retype the code in the editor and run it. Can you guess the output?  4 You should see the number four. Feel free to experiment with other operators.  Without taking this too seriously, you've just discovered that Python can be used as a calculator. Not a very handy one, and definitely not a pocket one, but a calculator nonetheless.  Taking it more seriously, we are now entering the province of operators and expressions.  Basic operators An operator is a symbol of the programming language, which is able to operate on the values.  For example, just as in arithmetic, the + (plus) sign is the operator which is able to add two numbers, giving the result of the addition.  Not all Python operators are as obvious as the plus sign, through, so let's go through some of the operators available in Python, and we'll explain which rules govern their use, and how to interpret the operations they perform. We'll begin with the operators which are associated with the most widely recognizable arithmetic operations:  +, -, *, /, //, %, ** The order of their appearance is not accidental. We'll talk more about it once we've gone through them all.  Remember: Data and operators when connected together form expressions.  The simplest expression is the literal itself.
Mostrar menos

Descripción

Arithmetic operators: exponentiation
Sin etiquetas
Arithmetic operators: exponentiation  A ** (double asterisk) sign is an exponentiation (power) operator. Its left argument is the base, its right, the exponent. Classical mathematics prefers notation with superscripts, just like this: 2^3. Pure text editors don't accept that, so Python uses ** instead, e.g., 2 ** 3.  Take a look at our examples in the editor window.  Note: we've surrounded the double asterisks with spaces in our examples. It's not compulsory, but it improves the readability of the code.  The examples show a very important feature of virtually all Python numerical operators.  Run the code and look carefully at the results it produces. Can you see any regularity here?  Remember:  It's possible to formulate the following rules based on this result:  when both ** aruguments are integers, the result is an integer, too when at least one ** argument is a float, the result is a float, too This is an important distinction to remember.
Mostrar menos

Descripción

Arithmetic operators: multiplication
Sin etiquetas
Arithmetic operators: multiplication An * (asterisk) sign is a multiplication operator. Run the code below and check if our integer vs. float rule is still working.    print(2 * 3) print(2 * 3.) print(2. * 3) print(2. * 3.) Arithmetic operators: division A / (slash) sign is a divisional operator.  The value in front of the slash is a dividend, the value behind the slash, a divisor. Run the code below and analyze the results.  print(6 / 3) print(6 / 3.) print(6. / 3) print(6. / 3.) You should see that there is an exception to the rule.  The result produced by the division operator is always a float, regardless of whether or not the result seems to be a float at first glance:  1 / 2 , or if it looks like a pure integer: 2  /  1.  Is this a problem? Yes, it is. It happens sometimes that you really need a division that provides an integer value, not a float.  Fortunately, Python can help you with that.
Mostrar menos

Descripción

Arithmetic operators: integer division
Sin etiquetas
Arithmetic operators: integer division A // (double slash) sign is an integer divisional operator. It differs from the standard / operator in two details.  its result lacks the fractional part- it's absent (for integers), or is always equal to zero (for floats); this means that the results are always rounded it conforms to the integer vs. float rule.  Run the example below and see the results: print(6 // 3) print(6 // 3.) print(6. // 3) print(6. // 3.) As you can see, integer by integer division gives an integer result. All other cases produce floats.  Let's do some more advanced tests.  Look at the following snippet:  print(6 // 4) print(6. // 4) Imagine that we used / instead of // - could you predict the results?  Yes, it would be 1.5 in both cases. That's clear.  But what results should we expect with the // division?  Run the code and see for yourself.  What we get is two ones - one integer and one float.  The result of integer division is always rounded to the nearest integer value that is less than the real (not rounded) result. This is very important: rounding always goes to the lesser integer.  Look at the code below and try to predict the results once again:  print(-6 // 4) print(6. // -4) Note: some of the values are negative. This will obviously affect the result. But how?  The result is two negative twos. The real (not rounded) result is -1.5 in both cases. However, the results are the subjects of rounding. The rounding goes toward the lesser integer value, and the lesser integer value is -2, hence -2 and -2.0. NOTE Integer division can also be called floor division. You will definitely come across this term in the future.
Mostrar menos
Mostrar resumen completo Ocultar resumen completo