if, elif, else Control syntax for branching. By setting a conditional expression, it is possible to use the processing to be performed properly. The if statement in Python can be interpreted in almost the same way as in C language. When writing a control statement, be careful of ** the number of blanks at the beginning of the statement **, not just the if statement!
if age >= 20:
print "adult" elif age >= 12: print "Youth" else: print "boy"
for, in Syntax for iterative processing. The target to be repeated is different from C language, and the process is repeated for each element of list, tuple, dictionary key, each character of character string, each line of file, etc. If you want to specify the number of times you want to repeat, use the range () function.
for a in [1, 2, 3]
print a
Execution result 1 2 3
for b in (1, 2, 3)
print b
Execution result 1 2 3
for c in {'one' : 1, 'two' : 2, 'three' : 3}
print c
Execution result three two one
for d in range(3)
print d
Execution result 1 2 3 When targeting a dictionary with a for statement, note that the order in which keys are retrieved depends on the dictionary implementation method, so it is not retrieved in order! while A syntax for conditional iterative processing. It's almost the same as C language, and there is no difference that needs special mention.
eto = [u'child', u'丑', u'tiger', u'卯', u'dragon', u'巳', u'noon', u'not', u'monkey', u' Rooster', u'戌', u'亥',] n = 0 while n < 3: print eto[n] n += 1
Execution result Child Ox Tiger
When using Japanese in Python, u or U must be added before "..." or'...'. By doing this, the character string can be used as Japanese (Unicode).
a = len ('aiueo') # It is recognized as 15 characters in the byte string because it is not recognized as Japanese. a = len (u'aiueo') #Recognized as 5 Japanese characters
A collection of elements surrounded by [...]. Write as [element 1, element 2, element 3, ...] It can be used like an array in C language, but it is also possible to put different elements in the same list, and by being able to specify the range from what number to what number, it is strengthened compared to the array in C language. Are
a = [1, 2, 3]
b = [1, 'a', 'A']
a = [10, 20, 30, 40, 50]
x1 = a [2] # a third [30] x2 = a [1: 3] # a 2nd to 4th [10, 20, 30] x3 = a [0: 4: 2] Skip the 1st to 5th #a [10, 30, 50] x4 = a [-2] # 2 from the back [40]
y = [1, 2, 3] + [4, 5, 6]
Lists can be concatenated using the # operator #[1, 2, 3, 4, 5, 6]
A collection of elements surrounded by (...). Write as (element 1, element 2, element 3, ...) Same usage as list, but cannot change elements Also, if the tuple has one element, you must add a, (comma) after the element.
a = (10, 20, 30, 40, 50)
a [1] = 60 # Cannot be changed and results in an error
a = ('a') # be the letter'a' instead of a tuple a = ('a',) # Tuple ('a',)
You can also change a list to a tuple and a tuple to a list.
list ((1, 2, 3)) # Becomes [1, 2, 3] in the list tuple ([1, 2, 3]) # Tuple (1, 2, 3)
A collection of elements surrounded by {...}. Write as {key 1: value 1, key 2: value 2, key 3: value 3, ...} Elements are made up of key / value pairs, and there is no order between the elements, so you need to specify the key to call the element.
a = {"sato" : 10, "tanaka" : 20, "suzuki" : 30}
x = a ["tanaka"] # Substitute the element value 20 of "tanaka" into x
In addition to numerical values, character strings and tuples can be used as keys.
a = {"pen" : "apple", "pine apple" : "pen"}
a = {(1, 2) : "x", (3, 4) : "y"}
Recommended Posts