I started learning using a site called checkio to improve my Python skills. https://checkio.org/
However, there is no polite explanation for learning, so In the first place, at the beginning of learning, it often asks "what should I do?" I was very troubled, so the purpose is to introduce how to use checkio.
It's simple, but if you study while thinking carefully, it will be very studying, so We recommend using it.
A lot of small tasks are prepared for each level, and if you repeat clearing It is a tool that allows you to learn Python as if you were playing a game where you can level up (move to another island).
Let's start with Elementary Island. (I'm still Elementary level 55)
There are multiple tasks on each island, and we will capture them in order from the top. When you select one issue, the following screen will be displayed.
The content of the assignment is explained at the beginning. Let's take the above "First Word (simplified)" as an example. From the first line, "You are given a string where you have to find its first word." Find the first word! I understand that it is a typical content.
However, it is difficult for me to understand only this English, so if you look at the frame of Example, I realized that I wanted to say that if I enter "Hello world", I can create a function that returns "Hello". ** Image the content of the assignment with this concrete example **.
When I first saw the Example, I was too ignorant I had no idea what "first_word (" Hello world ") ==" Hello "" was. Normally, when writing code in Python, I don't write it like this, what do you mean? When,? Was full.
This is ** "what to put in" and "what kind of result you want to return" **, here we named it first_word so that if you enter "Hello world", it will return "Hello". Please make a function. "
If there is JA in addition to English in the upper right of the screen, you can read the assignment in Japanese, So far, there are fewer issues with JA.
However, if you look at a concrete example, you can understand what you have to do, so even if you are not good at English, there should be no problem.
There is a button called "solve it" at the bottom of the screen in (2), so when you press it, it's finally the coding page.
I was confused by this screen. ** "What should I do ...?" "While writing" your code here "on the 5th line," return text [0: 2] "is written on the 6th line ..." "What's on the 13th line and below ...?" **, my heart broke before I went to the main subject.
I will explain one by one.
-The function is set in advance (the first line here), so we can write the code under "#your code here", which is commented out. ・ The default "return 〇〇" can be commented out once.
Then, what is "return 〇〇" written by default? !! I have a question, but this will be explained in (iii).
The function is set at the beginning of each task (I am still in the middle of Elementary, so it may change in the middle).
In this case, it is set at the beginning as ``` def first_word (text: str)-> str:` ``. This is a setting ** with the function name "first_word", the argument is text, and that text is str (string) **.
At first, I didn't know what "-> str:" was, but it ** specifies the type that the function wants the function to finally return (= return) **.
When developing, it seems that you've written code that isn't what you wanted to do, so what type does this function eventually return? Seems to define.
This is closely tied to (ii). This story itself is a little difficult for beginners to understand (I didn't understand at all at first), so ** "Return 〇〇〇〇 that is included by default is not related to the task (= must be used) (There is no such thing), so it's okay if you think about "OK to erase it **".
-------- For the time being, I will explain from here -------- In the function setting at the beginning, "This function returns as str (character string)" is set, so if you do not return the str type when debugging, an error will occur. Therefore, it seems that the contents that return ** str type are included by default **.
→ In this case, it is written as text [0: 2], which has nothing to do with this issue (so it is complicated), but if you write this for the time being, it will be returned as str type. Therefore, it is coded by default.
However, it is not necessary for the original task, so you can comment it out when you start the task, or delete the text [0: 2] and write the correct code. ――――――――――――――――――――――
I had no idea what this meant.
However, there are many sites that explain this by searching Google, so please refer to that as well.
Roughly speaking, ** this is to prevent the function from being executed at the same time as importing when importing this function ** (this code doesn't have much benefit, so it may seem unnecessary).
I was completely broken by the list of assert statements from the 14th line. However, I will regain my mind and investigate.
Roughly speaking, the assert statement is ** a setting that "if you put this in, it will return this result" in advance, and it will check each time if the code you wrote works as it is **.
The example of the task explanation at the beginning also points to the content of this assert statement.
In this case, if you enter "" Hello world "", it will return "" Hello "", if you enter "" a word "", it will return "" a "", and if you enter "" hi "", it will return "" hi "". That is.
The reason for setting the assert statement is that if you notice it during development, you may have written code that is different from what you want to make, so in order to notice the error, set the assert statement first. is.
So, ** I try to give a better image by looking at the contents of this assert statement if I find it a little difficult to understand in the example of the task explanation at the beginning **.
There were too many things I didn't know before I started the task, but let's say I wrote the code for the task safely.
First, click "Run" at the top of the screen. If it moves safely, it will be displayed at the bottom of the screen as shown in the red frame below.
-"Example:" is the execution result of the code on the 11th line (just a print statement) -"Hello" is the execution result of the code on the 12th line (Since this task was this, it is displayed as Hello safely) ・ "Coding complete? Click'Check' to earn cool rewards!" Is the execution result of the code on the 18th line (just a print statement).
The fact that no error occurred here means that all three of the assert statements are working!
On the contrary, if you get an error in red, correct the code according to the contents.
Click "check" at the top of the screen. If it works, a screen like this (Task solved) will be displayed and the problem will be cleared!
If you want to see someone else's code, you can see it in "Best Soludions" on the opening screen.
What did you think. checkio itself isn't that major, but I recommend it because it's a spartan that you have to give an answer on your own, so if you work properly, your ability will improve considerably.
However, as I tried, I hope that this article will help beginners in Python even a little because it is difficult to understand in the first part of the main subject, which hinders beginners.