Basic knowledge of Python ②. This is my study memo. Please do not excessive expectations.
-Manage multiple data at once Example: ['orange','banana','apple']
-It is also possible to make a mixture of character strings and numbers Example: ['banana','apple', 100, 200]
-Assign list to variable Example: fruits = ['orange','banana','apple']
-The numbers "0, 1, 2, ..." are assigned to the elements of the list in order from the front.
-When outputting the element of index number 0 of the list (fruits)
print(fruits[0])
-Update list elements
List [index number] = value
Example: fruits [0] ='grape'
fruits = changed to ['grape','banana','apple']
-Add element to list
List.append (value)
Example: fruits.append ('grape')
fruits = ['grape','banana','apple','grape'] is added
-Get all the elements of the list
Get all list elements by writing for variable name in list:
for statement → Repeat processing
Example
fruits = ['orange', 'banana', 'apple']
for fruit in fruits: #Colon required at end of line
print('My favorite fruit is'+ fruit + 'is.')
#Align the indents (4 half-width spaces)
Output result
My favorite fruit is orange.
My favorite fruit is banana.
My favorite fruit is apple.
-Used to manage multiple data together like a list
Create a dictionary like {key 1: value 1, key 2: value 2,…}, and enclose the dictionary in {}
.
Example: fruits = {'orange':'orange','banana':'yellow','apple':'red'}
-Retrieve dictionary values
Example
fruits = {'orange':'orange', 'banana':'yellow', 'apple':'red'}
print('The color of banana is'+ fruits['banana']+ 'is.')
#→ The color of banana is yellow.
-Update dictionary elements
fruits['banana'] = black
Element update by writing dictionary name [key name] = value
result
fruits = {'orange':'orange', 'banana':'yellow', 'apple':'red'}
fruits['banana'] = black
print(fruits)
# → {'orange':'orange', 'banana':'black', 'apple':'red'}
-Add elements to the dictionary
fruits['melon'] = green
Add a new element to the dictionary by writing dictionary name [new key name] = value
result
fruits = {'orange':'orange', 'banana':'yellow', 'apple':'red'}
fruits['melon'] = green
print(fruits)
# → {'orange':'orange', 'banana':'yellow', 'apple':'red', 'melon:green'}
-Get all the elements of the dictionary
for variable name in dictionary:
Example
fruits = {'orange':'orange', 'banana':'yellow', 'apple':'red'}
for fruit_key in fruits: #Colon required at end of line
print(fruit_key+ 'The color of'+ fruits[fruit_key] + 'is.')
#Align the indents (4 half-width spaces)
Output result
The color of orange is orange.
The color of banana is yellow.
The color of apple is red.
-while conditional expression: Write as
.
Repeat the processing in the while statement while the result of the conditional expression is True
Example
x = 1
while x <= 10: #Colon required at end of line
print(x)
x+=1
#Align the indents (4 half-width spaces)
Use -break to end the iterative process
Example
numbers = [765, 921, 777, 256]
for number in numbers: #Colon required at end of line
print(number)
if number == 777: #Colon required at end of line
break
#Align the indents (4 half-width spaces)
-continue skips only the processing of that week
Example.Skip numbers divisible by 3
numbers = [765, 921, 777, 256]
for number in numbers: #Colon required at end of line
if number % 3 == 0:
continue
print(number)
#Align the indents (4 half-width spaces)
Recommended Posts