[Python] How to use checkio

1. Purpose

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.

2. How to use checkio

(1) What is checkio?

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

(2) How to read the task screen

Let's start with Elementary Island. (I'm still Elementary level 55)

キャプチャ1.PNG

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.

キャプチャ2.PNG

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

Supplement 1

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

Supplement 2

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.

(3) How to read the coding screen

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. キャプチャ3.PNG

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.

(I) The part we code

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

(Ii) About def at the beginning

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.

(Iii) About "return 〇〇" described by default

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

(Iv) about if __ name __ =='__ main __':

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

(V) About assert statements

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

(4) Try to execute

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.

キャプチャ4.PNG

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.

キャプチャ5.PNG

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

(5) Submit the assignment

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!

キャプチャ6.PNG

(6) See other people's code

If you want to see someone else's code, you can see it in "Best Soludions" on the opening screen.

キャプチャ7.PNG

3. Conclusion

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.

Recommended Posts

[Python] How to use checkio
python3: How to use bottle (2)
[Python] How to use list 1
How to use Python argparse
Python: How to use pydub
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
Python: How to use async with
[Python] How to use Pandas Series
How to use Requests (Python Library)
How to use SQLite in Python
[Python] How to use list 3 Added
How to use Mysql in python
How to use OpenPose's Python API
How to use ChemSpider in Python
How to use FTP with Python
Python: How to use pydub (playback)
How to use PubChem in Python
How to use python zip function
[Python] How to use Typetalk API
How to use xml.etree.ElementTree
How to use Python-shell
[Introduction to Python] How to use class in Python?
How to use tf.data
How to use virtualenv
How to use image-match
How to use shogun
How to install Python
How to use Pandas 2
How to install and use pandas_datareader [Python]
How to use Virtualenv
How to use numpy.vectorize
How to use pytest_report_header
[python] How to use __command__, function explanation
How to install python
How to use partial
How to use Bio.Phylo
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to use IPython
[Python] Organizing how to use for statements
Memorandum on how to use gremlin python
How to use virtualenv
[Python2.7] Summary of how to use unittest
How to use Matplotlib
How to use iptables
python: How to use locals () and globals ()
How to use numpy
How to use __slots__ in Python class
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
How to use list []
How to use "deque" for Python data
How to use python-kabusapi