Archive:Python/Operators: Difference between revisions
wikademia>Wikademia m moved Python operators to Archive:Python/Operators |
|||
| (5 intermediate revisions by 4 users not shown) | |||
| Line 6: | Line 6: | ||
===Simple Math=== | ===Simple Math=== | ||
{{?}} | |||
Math is very straight forward in Python. + adds, - subtracts, / divides, and believe it or not * multiplies. The main thing to comment on is %. % divides a problem and then returns the remainder. This is called the modulo operation. | Math is very straight forward in Python. + adds, - subtracts, / divides, and believe it or not * multiplies. The main thing to comment on is %. % divides a problem and then returns the remainder. This is called the modulo operation. | ||
| Line 174: | Line 174: | ||
===Indexing=== | ===Indexing=== | ||
You have already learned how to index in lesson 2. When you typed x[3] you | You have already learned how to index in lesson 2. When you typed x[3] you were indexing. You may also index a string without first making a variable represent it. | ||
Type: | Type: | ||
| Line 245: | Line 245: | ||
''''8765432'''' | ''''8765432'''' | ||
To reverse a string (or any sequence): | |||
>>> '''x[::-1] | |||
''''9876543210'''' | |||
When the step indice is negative it is important to remember to make sure that the first indice is calling an element that comes later in the string, tuple or list than the second element. Just as in the last example '''-2''' is representing '''8''' while '''1''' is representing 2. | When the step indice is negative it is important to remember to make sure that the first indice is calling an element that comes later in the string, tuple or list than the second element. Just as in the last example '''-2''' is representing '''8''' while '''1''' is representing 2. | ||