Python basics ②

Basic knowledge of Python ②. This is my study memo. Please do not excessive expectations.

list

-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.


dictionary

-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 statement

-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)

break

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

-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

Python basics ⑤
Python basics
Python basics ④
Python basics ③
Python basics
Python basics
Python basics
Python basics ③
Python basics ②
Python basics ②
Python basics: list
Python basics memorandum
#Python basics (#matplotlib)
Python CGI basics
Python basics: dictionary
Basics of Python ①
Basics of python ①
Python slice basics
#Python basics (scope)
#Python basics (#Numpy 1/2)
#Python basics (#Numpy 2/2)
#Python basics (functions)
Python array basics
Python profiling basics
Python #Numpy basics
Python basics: functions
#Python basics (class)
Python basics summary
Python basics ② for statement
Python: Unsupervised Learning: Basics
Basics of Python scraping basics
Python basics 8 numpy test
Errbot: Python chatbot basics
#Python DeepLearning Basics (Mathematics 1/4)
Python basics: Socket, Dnspython
# 4 [python] Basics of functions
Basics of python: Output
python: Basics of using scikit-learn ①
Python basics: conditions and iterations
Paiza Python Primer 4: List Basics
Basics of Python × GIS (Part 1)
kafka python
Basics of Python x GIS (Part 3)
python + lottery 6
Python Summary
Paiza Python Primer 5: Basics of Dictionaries
Python comprehension
Python technique
Studying python
Python memorandum
Python FlowFishMaster
SNS Python basics made with Flask
Python service
python tips
Linux basics
python function ①
Python memo
ufo-> python (3)
Python comprehension
install python
Python Singleton