This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Assume some existing list a, a list of strings. | |
| # Determine whether a contains the string "blue". | |
| a = ["red", "orange", "yellow", | |
| "green", "blue", "indigo", "violet"] | |
| i = 0 | |
| while i < len(a) and a[i] != "blue": | |
| i = i + 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| i = 0 | |
| while i < len(a): | |
| a[i] = a[i] + 1 | |
| i = i + 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| i = 0 | |
| while i < len(a): | |
| print a[len(a) - i - 1] | |
| i = i + 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| a = ["red", "orange", "yellow", | |
| "green", "blue", "indigo", "violet"] | |
| i = 0 | |
| while i < len(a): | |
| print a[i] | |
| i = i + 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| a = ["red", "pink", "yellow", | |
| "green", "blue", "indigo", "violet"] | |
| b = a | |
| a[1] = "orange" | |
| print a[1] # "orange" | |
| print b[1] # also "orange" ! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Strings are immutable. | |
| # Lists are mutable. | |
| # "Immutable" means not able to change, and "mutable" means able to change. | |
| # Given a string s, replace the character at position 4 with a "4". | |
| s = "0123056789" | |
| s = s[:4] + "4" + s[5:] | |
| # Instead, we have to build a new string. | |
| # This distinction is important: in Python, strings are immutable, we cannot change string values, we can, however, create a new string and assign that to s. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # In literals: | |
| # we use square brackets denote a list, and the elements are comma separated. | |
| a = [] # An empty list. | |
| b = [1] | |
| c = [3, 2, 1] | |
| d = ["ab", "cd", "ef"] | |
| a = ["red", "orange", "yellow", | |
| "green", "blue", "indigo", "violet"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # The syntax for accessing the individual elements of a list is the same as that for accessing the characters of a string. | |
| s = "Mary had a little lamb." | |
| print a[4] # " " | |
| print a[6] # "a" | |
| # The elements of a list can be values of any type. | |
| a = [1, 1, 2, 3, 5, 8, 13, 21] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| s = raw_input() | |
| # Test whether s[i] is a digit. | |
| print "0" <= s[i] and s[i] <= "9" | |
| # Test whether s[i] is not a digit. | |
| print s[i] < "0" or "9" < s[i] | |
| # Test whether s[i] is not a digit (alternative, possibly better). | |
| print not ("0" <= s[i] and s[i] <= "9") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| n = input() | |
| i = 2 # Linear search. | |
| while i < n and n % i != 0: # | |
| i = i + 1 # | |
| if n < 2 or i < n: | |
| print "not prime" |