Archive:Python/Operators: Difference between revisions
wikademia>JWSchmidt |
wikademia>Wikademia m moved Python operators to Archive:Python/Operators |
||
| (13 intermediate revisions by 9 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 27: | Line 27: | ||
>>> '''a = 4''' | >>> '''a = 4''' | ||
>>> '''if a % 2 == 0:''' | >>> '''if a % 2 == 0:''' | ||
... '''" | ... '''print "a is even"''' | ||
'''a is even''' | '''a is even''' | ||
| Line 42: | Line 42: | ||
>>> '''x + y''' | >>> '''x + y''' | ||
'''6''' | '''6''' | ||
| Line 53: | Line 54: | ||
>>> '''x[1] + x[2]''' | >>> '''x[1] + x[2]''' | ||
'''5''' | '''5''' | ||
| Line 172: | 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 243: | 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. | ||
| Line 259: | Line 267: | ||
What the percent sign does is hold the place in the string and let you tell Python what to put there at the end of the string. The '''s''' behind the '''%''' | What the percent sign does is hold the place in the string and let you tell Python what to put there at the end of the string. The '''s''' behind the '''%''' | ||
[[Category: | |||
[[Category:Computer science]] | |||
[[Category:Programming]] | |||
[[Category:Python]] | |||