IQ Bot Custom Logic: 5 things you might be surprised when someone who speaks another language first touches Python

Hello.

The custom logic of IQ Bot is Python after all, so anyone who can write Python should be able to handle it without difficulty.

And Python is said to be easier for beginners to enter than programming languages such as Java and C.

For those who used other programming languages such as Java and C, I think you can master Python faster than someone who starts from scratch,

Because I was speaking in another language, there may be some points that surprise me, "What? Is there such a thing?"

In this article, I will summarize the Python surprise points unique to those who have dealt with other languages.

No need to compile

Python is a dynamic language, so you don't need to compile it to run it.

So you don't have to worry about compiling IQ Bot custom logic. If you press "Test Run" immediately after writing, it will work on the spot.

For a description of dynamic languages, see [Wikipedia](https://en.wikipedia.org/wiki/%E5%8B%95%E7%9A%84%E3%83%97%E3%83%AD%E3% 82% B0% E3% 83% A9% E3% 83% 9F% E3% 83% B3% E3% 82% B0% E8% A8% 80% E8% AA% 9E) Please refer to that. ..

Variables can be used without declaring a type

Python is typed dynamically, so you don't have to declare the type of a variable.

In Java, if you write the following code, I get angry at the second line and get a compile error.

For java


int num = 3;       //Declare the type of the variable num here!
num = "Yahho";   //I get angry when I do this

In the int type, that is, the box (= variable called num) that says "only integers can be stored" That's because I tried to insert a playful character string (String type) such as " Yahho ".

Oh, I'm sorry, the problem is that the type is different, so even if the contents of the character string are serious, I get angry.

On the other hand, in the case of Python, the following code works without any problem.

For Python


num = 3           #By doing this, the variable num becomes an int type without permission.
num = "Yahho"   #Here, the variable num transforms into a String type without permission ...! !!

This feature is very much appreciated by a person with a sloppy personality like me, For those accustomed to well-behaved coding in other programming languages,

It may be quite surprising to say that the type of a variable changes in the middle.

Even in Python, the concept of type itself is not gone, It just has a function that dynamically performs typing on the programming language side.

A list of Python types, how to check types, how to cast (type conversion), etc. It was organized in a very easy-to-understand page on the here page.

I sometimes use type casting when applying custom logic to IQ Bot.

Indentation has a practical meaning

In other languages, aligning indents in if statements and functions is just a matter of alignment. "Best practice for improving readability for humans"

The general specification is that indentation has no meaning in executing a program.

In other words, as shown below, most languages say that the program works fine even if the indentation is dirty.

In the case of java ①: It's better to write like this ...


class xxx
{
  public static void main (String[] args) throws java.lang.Exception
  {
    int num = 3;
    if (num % 2 == 0) 
    {
      System.out.println(num + "Is an even number");
    }
    else {
      System.out.println(num + "Is odd");
    }
  }
}

In the case of java (2): It still works.


class xxx
{
public static void main (String[] args) throws java.lang.Exception
{
int num = 3;
if (num % 2 == 0) 
{
System.out.println(num + "Is an even number");
}
else {
System.out.println(num + "Is odd");
}
}
}

Python, on the other hand, is based on the idea of "ensuring that all programmers write code that is completely readable." If you don't align the indents, the code won't work in the first place.

In case of Python ①: If you write like this, it will work, but ...


num = 3
if num % 2 == 0:
	print(str(num) + "Is an even number")
else:
	print(str(num) + "Is odd")

In case of Python ②: It doesn't work with this


num = 3
if num % 2 == 0:
print(str(num) + "Is an even number")
else:
print(str(num) + "Is odd")

If the indentation is not aligned correctly, you will basically get an "Indentation Error" error.

This article because the indentation level is incorrect Please note that there may be cases where the result is different from what you expected!

Easy to loop

Since there was an article that summarized Python and Java loop processing in an easy-to-understand manner, I will introduce that.

Comparison of basic grammar between Java and Python

There are various grammars other than loop processing, so please refer to them.

Can handle strings like an array

For those who originally used languages such as Java, You may be quite surprised to see the code below in the link above.

A fairly common loop in Python


for char in 'Hello':
    print(char)

When I do the above process, the following result is returned ...

Processing result of the above loop


>>>H
>>>e
>>>l
>>>l
>>>o

The reason why you can do this is that in Python you can use strings, This is because there is an idea of ** treating each character as a sequence with elements as elements **.

So you don't have to do something like Java by first dividing it into arrays and then looping around. You can loop or slice the string as it is (a process that gets the number from the number of the string).

Slices are used unexpectedly (?) Even in IQ Bot's custom logic, so I will link the article that was easy to understand.

[Python] Summary of slicing operations

Summary

How was it?

The following are some of the points in Python that may surprise anyone who has spoken another language.

--Python is a dynamic language so you don't need to compile it --Python can be dynamically typed so you don't have to declare the type of the variable (the concept of type allows you to cast) ――If you don't align the indents, you won't be angry and can't do it. --The way to write loops may be quite different from Java --You can treat strings like arrays and loop or slice them directly.

I will add it if I find anything else.

Well then!

Recommended Posts

IQ Bot Custom Logic: 5 things you might be surprised when someone who speaks another language first touches Python
IQ Bot Custom Logic (Python): Streamline exclusions in loops
IQ Bot Custom Logic (Python): Efficient replacement process in a loop
IQ Bot Custom Logic (Python): Efficient replacement process in a loop