This article is about a memo written by AtCoder gray beginners. It may not be very helpful in some cases. sorry.
All of this code has been verified to work with Python 3 (3.4.3) on AtCoder and the new judge version of Python (3.8.2).
Also, in this article, the code is written as follows.
#input
#code
#output
Also, basically, writing the code short is less readable and more misleading to the reader, ** not recommended </ font> **. Please note that there is.
So basically it's a good idea to write code that is easy to read. This article may be recommended for anyone who wants to aim for Shortest Code in all languages or Shortest Code in Python with Python !!
eval() A function that executes a string. [](Maybe it can be used in ...)
# 1 + 2
a = input()
print(eval(a))
# 3
replace() It replaces the string of the first function with the string of the second argument. In the example below, spaces are replaced with the string "2".
# 1 3 4
a = input()
print(a.replace('4','5'))
# 1 3 5
You can also do this in combination with the eval () function described above.
# 10 3
print(eval(input().replace(' ','-'))) #10-Will do 3
# 7
[](Maybe it can be used in ...)
It's a familiar assignment operator! !! This kind of thing is recommended because it shortens the code and speeds up the process! !!
# 45
a = int(input())
a = a + 5
print(a)
# 50
The code above does exactly the same as the code below.
# 45
a = int(input())
a += 5
print(a)
# 50
~ Is a bit operator that inverts bits. It is generally known that when an integer is bit-inverted, the number becomes negative and -1. For example, in the example below
a = 9
print(~a)
# -10
The number of -1 was output with the negative value. If you want to know more about this, go to "2's complement"! !!
You can take advantage of it to write a-1 and a + 1 in shorter code. However, the caveat is that unlike the assignment operator above, the variable itself does not change.
a = 9
print(~-a)
print(-~a)
print(a)
# 8
# 10
# 9
By the way, the execution order is as follows.
a = 9
print(~(-a))
print(-(~a))
# 8
# 10
In Python you can assign a function to a variable. This allows you to use frequently used long-named functions shorter without using def.
p = print
p(3)
# 3
Basically, in Python, the compiler will read False = 0, True = 1 in such a place. So you can also do the following:
mylist = [0,3,5,4]
print(mylist[True])#mylist[1]Is outputting
print(mylist[False])#mylist[0]Is outputting
# 3
# 0
In Python, if you normally output a list, commas and parentheses will be added, so you can not output it as it is. Therefore, it is necessary to devise the following.
You can get a nice output by adding an asterisk before the list. It can also be written concisely.
mylist = ['Alice','Bob',,4]
print(mylist)
print(*mylist)#I was able to clean it
# [0, 3, 5, 4]
# 0 3 5 4
Actually, this article is my first article > < I think there are still some places where I'm not used to Qiita. So if you find a typographical error, something that is hard to see, or something that is hard to understand, please comment more and more. We may also add a variety of new code and techniques. Finally, thank you for reading this article!