Archive:Python/Operators: Difference between revisions
compare with ==, not with =. By the way, I think that the box is OK. |
wikademia>Wikademia m moved Python operators to Archive:Python/Operators |
||
| (15 intermediate revisions by 11 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. | ||
Type: | Type: | ||
>>>'''9 % 3''' | >>> '''9 % 3''' | ||
0 | 0 | ||
| Line 17: | Line 17: | ||
Now Type: | Now Type: | ||
>>>'''9 % 2''' | >>> '''9 % 2''' | ||
1 | 1 | ||
| Line 25: | Line 25: | ||
Type: | Type: | ||
>>>'''a = 4''' | >>> '''a = 4''' | ||
>>>'''if a % 2 == 0:''' | >>> '''if a % 2 == 0:''' | ||
... | ... '''print "a is even"''' | ||
'''a is even''' | '''a is even''' | ||
| Line 35: | Line 35: | ||
Type: | Type: | ||
>>>'''x = 1''' | >>> '''x = 1''' | ||
>>>'''y = 5''' | >>> '''y = 5''' | ||
Now Type: | Now Type: | ||
>>>'''x + y''' | >>> '''x + y''' | ||
'''6''' | '''6''' | ||
| Line 48: | Line 49: | ||
Type: | Type: | ||
>>>'''x = [1, 2, 3]''' | >>> '''x = [1, 2, 3]''' | ||
Now Type: | Now Type: | ||
>>>'''x[1] + x[2]''' | >>> '''x[1] + x[2]''' | ||
'''5''' | '''5''' | ||
| Line 59: | Line 61: | ||
Type: | Type: | ||
>>>'''x = {'a':1, 'b':2}''' | >>> '''x = {'a':1, 'b':2}''' | ||
=You can also add and multiply strings, tuples, and lists. | =You can also add and multiply strings, tuples, and lists. | ||
Now Type: | Now Type: | ||
>>>'''x['a'] + x['b'] | >>> '''x['a'] + x['b'] | ||
'''3''' | '''3''' | ||
| Line 75: | Line 77: | ||
Type: | Type: | ||
>>>'''"Python" + " is awesome"''' | >>> '''"Python" + " is awesome"''' | ||
You can do the same with variables refering to strings. | You can do the same with variables refering to strings. | ||
| Line 81: | Line 83: | ||
Type: | Type: | ||
>>>'''x = "Python"''' | >>> '''x = "Python"''' | ||
>>>'''y = " is awesome"''' | >>> '''y = " is awesome"''' | ||
Now Type: | Now Type: | ||
>>>'''x + y''' | >>> '''x + y''' | ||
''''Python is awesome'''' | ''''Python is awesome'''' | ||
| Line 95: | Line 97: | ||
Type: | Type: | ||
>>>'''x = ('I', 'Love') | >>> '''x = ('I', 'Love') | ||
>>>'''y = ('Wikiversity',)''' ''DON'T FORGET THE COMMA AFTER '''WIKIVERSITY''''' | >>> '''y = ('Wikiversity',)''' ''DON'T FORGET THE COMMA AFTER '''WIKIVERSITY''''' | ||
Now Type: | Now Type: | ||
>>>'''print x + y''' | >>> '''print x + y''' | ||
'''('I', 'Love', 'Wikiversity')''' | '''('I', 'Love', 'Wikiversity')''' | ||
| Line 109: | Line 111: | ||
Type: | Type: | ||
>>>'''x = ('I', 'Love')''' | >>> '''x = ('I', 'Love')''' | ||
>>>'''y = 'Python'''' | >>> '''y = 'Python'''' | ||
Now Type: | Now Type: | ||
>>>'''print x + y''' | >>> '''print x + y''' | ||
'''Traceback (most recent call last):''' | '''Traceback (most recent call last):''' | ||
| Line 131: | Line 133: | ||
Type: | Type: | ||
>>>'''x = 'dog'''' | >>> '''x = 'dog'''' | ||
Now type: | Now type: | ||
>>>'''x * 5''' | >>> '''x * 5''' | ||
''''dogdogdogdogdog'''' | ''''dogdogdogdogdog'''' | ||
| Line 143: | Line 145: | ||
Type: | Type: | ||
>>>'''x = ('dog',)''' | >>> '''x = ('dog',)''' | ||
Now Type: | Now Type: | ||
>>>'''x * 5''' | >>> '''x * 5''' | ||
'''('dog', 'dog', 'dog', 'dog', 'dog')''' | '''('dog', 'dog', 'dog', 'dog', 'dog')''' | ||
| Line 155: | Line 157: | ||
Type: | Type: | ||
>>>'''x = {'dog':1}''' | >>> '''x = {'dog':1}''' | ||
Now Type: | Now Type: | ||
>>>'''x * 5''' | >>> '''x * 5''' | ||
'''Traceback (most recent call last):''' | '''Traceback (most recent call last):''' | ||
| 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: | ||
>>>'''"I Love Wikipedia"'''[5] | >>> '''"I Love Wikipedia"'''[5] | ||
'''e''' | '''e''' | ||
| Line 188: | Line 190: | ||
Type: | Type: | ||
>>>'''x = "Wikipedia and Python are awesome"''' | >>> '''x = "Wikipedia and Python are awesome"''' | ||
Now Type: | Now Type: | ||
>>>'''print x[14:32] | >>> '''print x[14:32] | ||
'''Python are awesome''' | '''Python are awesome''' | ||
| Line 200: | Line 202: | ||
Type: | Type: | ||
>>>'''x = '1234'''' | >>> '''x = '1234'''' | ||
Now type: | Now type: | ||
| Line 216: | Line 218: | ||
Type: | Type: | ||
>>>'''x = "0123456789"''' | >>> '''x = "0123456789"''' | ||
Now Type: | Now Type: | ||
>>>'''x[1:-1:3]''' | >>> '''x[1:-1:3]''' | ||
''' '147' ''' | ''' '147' ''' | ||
| Line 228: | Line 230: | ||
Type: | Type: | ||
>>>'''x[::3]''' | >>> '''x[::3]''' | ||
''''0369'''' | ''''0369'''' | ||
| Line 236: | Line 238: | ||
Type: | Type: | ||
>>>'''x='0123456789'''' | >>> '''x='0123456789'''' | ||
Now Type: | Now Type: | ||
>>>'''x[-2:1:-1] | >>> '''x[-2:1:-1] | ||
''''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 254: | Line 262: | ||
Type: | Type: | ||
>>>'''print 'I Love %s' % 'Linux'''' | >>> '''print 'I Love %s' % 'Linux'''' | ||
'''I Love Linux''' | '''I Love Linux''' | ||
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:Computer science]] | |||
[[Category:Programming]] | |||
[[Category:Python]] | |||