Easy understanding of Python for & arrays (for super beginners)

I am self-taught and participating in AtCoder in Python. So today, I'm going to give a brief explanation of for and arrangement, which is a point that people who have started learning (people like me in the past) will not understand! I will also explain about input using AtCoder for. It is just a brief explanation. If you can know at least this far, you will also acquire google ability (vocabulary ability when google), please google various functions by yourself and learn little by little! (I'm a fledgling brown coder and I'm not good at short notations. Also, if you should lie, please tell me> <)

What is a for sentence Yo!

How to write normally

for i in range(3):
    print(i)

The for statement looks like this. ** The number of numbers in the parentheses of range () and the processing inside the for statement are repeated. ** ** In this, ʻi` is called a loop counter variable (I also googled it and now I know it w I don't care about the name) ** It increases by 1 each time it is repeated. ** **

The result of executing this code is

0
1
2

It will be.

As you can see here, ʻiof the first loop is 0. This will be a very important point later. You can see that0,1,2is output 3 times in total. That's because I wroterange (3)`.

How to decide the beginning and the end

for i in range(1,3):
    print(i)

Actually, there is also such a way of writing. The output result of this is

1
2

is. You can see the rules by comparing it with the code above.

Development ~ Repeat for sentences ~

The for statement can be overwritten. Well, for now, just look at it.

for i in range(2):
    for j in range(3):
        print(i,j)

Output result

0 0
0 1
0 2
1 0
1 1
1 2

You may be a little confused, but you can get used to it! (By the way, if you stack many for statements, it will take a lot of execution time.)

What a day of array!

Roughly speaking, an array (list) is like a variable that can store multiple elements together.

yasai = ["Carrots","Daikon radish","Burdock"]

The elements are separated by commas and surrounded by []. In this case, think of yasai as a hotel with three rooms. Normally, each room in a hotel has a room number. The array is the same.

The arrays are numbered ** 0,1,2 .... `in order from the first element. ** This number is called index.

I'll say it again! !! ** The number of the first element is 0! !! !! ** ** It's the same as ʻi`. It's a very important point, so let's hold it down.

Let's specify the index

yasai = ["Carrots","Daikon radish","Burdock"]
print(yasai[1])

You can also get the element with that number in the array by writing array name [number] like this. Do you know the result of executing this code?

Do you think it's a carrot? It's wrong!

Daikon radish

** The number of the first element is 0 **, so be careful not to make a mistake.

The length of the array is len!

The length of the array can be obtained with len (array).

yasai = ["Carrots","Daikon radish","Burdock"]
print(len(yasai))

Output result

3

I also use this often. Let's remember.

Based on the above

You can now write code like this.

yasai = ["Carrots","Daikon radish","Burdock"]
for i in range(len(yasai)):
    print(yasai[i])

Output result ↓

Carrots
Daikon radish
Burdock

how is it! If you can understand so far, you will be able to solve the C problem. If you don't understand, please read the above again.

Try inputting using the for statement and inputting the array. (AtCoder)

With AtCoder, I think that input using for will come out from around the C problem. For example, it looks like this ↓

C - Peaks

Looking at the input field, it looks like this. スクリーンショット 2020-05-15 14.27.39.png

You may not understand the meaning. I didn't understand the meaning at first either. Especially subscripts. But if you can understand the above, you will understand it immediately.

N,M = map(int,input().split())
H = list(map(int,input().split()))
for i in range(M):
    A,B = map(int,input().split())

Jajan. To enter list, just cover the usual map () input with list (). In the for statement below, the loop is turned M times and A and B are input M times.

As you can see here, Python doesn't need to specify the length of the array when inputting the array, so N is not used like M when inputting. It's easy.

From the input specification (see gray image), N == len (H) holds.

Everyone hates out-of-array reference ~ Let's read the error message ~

One of the most frustrating errors for beginners is out-of-array references.

yasai = ["Carrots","Daikon radish","Burdock"]
print(yasai[3])

Well, there is a code like this. Do you know what is output?

In the unlikely event that someone thinks "burdock", go back to the page above and keep my words in mind.

The answer is "nothing is output". I get an error message.

Traceback (most recent call last):
  File "Main.py", line 2, in <module>
    print(yasai[3])
IndexError: list index out of range

I get this error.

Even if you can't speak English, it's not difficult at all if you read it calmly.

From line 2, you can see that the code on the second line is wrong. See ʻIndexError: list index out of range`. This is the index error I hate most (often), ** out-of-array references **.

In other words, if you read this error message, you can see that "** The index of the codeprint (yasai [3])on the second line refers to the outside of the array **"!

This out-of-order reference may seem simple at first glance, but as the code gets more complicated, it becomes ** horror out-of-order reference on parade hell **. be careful.

Finally

If you can solve the C problem at a certain speed, you can definitely aim for a brown coder! Python is a language that has short code to write and is suitable for basic fast solving, and above all, it is easy to read, so it took me a lot of time to write, but it may be relatively quick to go to brown.

Execution time (competition pro has a limit on execution time, let's write code as efficient as possible, for details on the web) is the biggest problem, but if you get used to various calculations To some extent, you can feel it.

**Please do your best! ** **

Recommended Posts

Easy understanding of Python for & arrays (for super beginners)
Python for super beginners Python for super beginners # Easy to get angry
Python #function 2 for super beginners
Python for super beginners Python #functions 1
Python #list for super beginners
Python for super beginners Python # dictionary type 1 for super beginners
Python #index for super beginners, slices
Python #len function for super beginners
Python #Hello World for super beginners
Python for super beginners Python # dictionary type 2 for super beginners
[Python] Minutes of study meeting for beginners (7/15)
Let's put together Python for super beginners
python textbook for beginners
Summary of frequently used Python arrays (for myself)
Installing TensorFlow on Windows Easy for Python beginners
OpenCV for Python beginners
Basics of pandas for beginners ② Understanding data overview
About Python external module import <For super beginners>
Basic story of inheritance in Python (for beginners)
How to convert Python # type for Python super beginners: str
Summary of pre-processing practices for Python beginners (Pandas dataframe)
Python # How to check type and type for super beginners
Learning flow for Python beginners
python> Handling of 2D arrays
Python3 environment construction (for beginners)
Overview of Docker (for beginners)
Python Beginner's Guide (Variables / Arrays)
Basic Python grammar for beginners
100 Pandas knocks for Python beginners
~ Tips for beginners to Python ③ ~
Full understanding of Python debugging
[For beginners] Summary of standard input in Python (with explanation)
■ Kaggle Practice for Beginners --Introduction of Python --by Google Colaboratory
How to convert Python # type for Python super beginners: int, float
A beginner's summary of Python machine learning is super concise.
[Python] The biggest weakness / disadvantage of Google Colaboratory [For beginners]
[For beginners] Basics of Python explained by Java Gold Part 1
Easy encryption of file contents (Python)
Python Exercise for Beginners # 2 [for Statement / While Statement]
[Python] Understanding the potential_field_planning of Python Robotics
[Must-see for beginners] Basics of Linux
Beginners use Python for web scraping (1)
[Python] Reasons for overriding using super ()
Run unittests in Python (for beginners)
Introductory table of contents for python3
Memorandum of beginners Python "isdigit" movement
Beginners use Python for web scraping (4) ―― 1
Record of Python introduction for newcomers
Learn the basics of Python ① Beginners
A memorandum of understanding for the Python package management tool ez_setup
[Python machine learning] Recommendation of using Spyder for beginners (as of August 2020)
PyPI package for super easy use of Cotoha on Google colab
Python techniques for those who want to get rid of beginners
[For beginners] Recursive function (Tower of Hanoi is easy to understand!)
Easy keyword extraction with TermExtract for Python
[Python] Super easy test with assert statement
INSERT into MySQL with Python [For beginners]
[Kaggle for super beginners] Titanic (Logistic regression)
Summary of various for statements in Python
Easy introduction of speech recognition with Python
[Example of Python improvement] What is the recommended learning site for Python beginners?