Can be used with AtCoder! A collection of techniques for drawing short code in Python!

Introduction

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 !!

1.0 Basic input operation

1.1 Commonly used functions

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

1.2 Commonly used operators

Assignment operators such as + =,-=

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
~-and-~

~ 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

1.3 Other techniques

Assign a function to a variable

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
Use bool as an integer

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

2.0 Basic output operation

list output

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.

How to use an asterisk

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

10.0 Finally

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!

Recommended Posts

Can be used with AtCoder! A collection of techniques for drawing short code in Python!
A collection of code often used in personal Python
Japanese can be used with Python in Docker environment
Python knowledge notes that can be used with AtCoder
I made a familiar function that can be used in statistics with Python
A collection of Excel operations often used in Python
A collection of competitive pro techniques to solve with Python
Understand the probabilities and statistics that can be used for progress management with a python program
I created a template for a Python project that can be used universally
Mathematical optimization that can be used for free work with Python + PuLP
Code reading of Safe, a library for checking password strength in Python
I wrote a tri-tree that can be used for high-speed dictionary implementation in D language and Python.
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
Functions that can be used in for statements
Techniques often used in python short coding (Notepad)
Introduction of drawing code for figures with a certain degree of perfection of meteorological data
A class for PYTHON that can be operated without being aware of LDAP
Python scikit-learn A collection of predictive model tips often used in the field
A personal memo of Pandas related operations that can be used in practice
Python scikit-learn A collection of predictive model tips often used in the field
[Python3] Code that can be used when you want to cut out an image in a specific size
Article that can be a human resource who understands and masters the mechanism of API (with Python code)
Specific sample code for working with SQLite3 in Python
VS Code settings for developing in Python with completion
A collection of commands frequently used in server management
List of Python code used in big data analysis
Can be used in competition pros! Python standard library
Run the output code with tkinter, saying "A, pretending to be B" in python
Summary of statistical data analysis methods using Python that can be used in business
[Python] Building an environment for competitive programming with Atom (input () can be used!) [Mac]
[Python] Introduction to web scraping | Summary of methods that can be used with webdriver
List of tools that can be used to easily try sentiment analysis of Japanese sentences in Python (try with google colab)
NumPy zeros can be defined even with a size of 0
Create a child account for connect with Stripe in Python
Techniques for sorting in Python
A sample for drawing points with PIL (Python Imaging Library).
[Python3] Code that can be used when you want to resize images in folder units
SSD 1306 OLED can be used with Raspberry Pi + python (Note)
Scripts that can be used when using bottle in Python
A collection of methods used when aggregating data with pandas
Turn an array of strings with a for statement (Python3)
[Python] Create a screen for HTTP status code 403/404/500 with Django
Here's a summary of things that might be useful when dealing with complex numbers in Python
[Python] A program that finds the maximum number of toys that can be purchased with your money
Create a Python environment for professionals in VS Code on Windows
[Python] A memo of frequently used phrases (by myself) in Python scripts
[Python] Make a graph that can be moved around with Plotly
Wrap (part of) the AtCoder Library in Cython for use in Python
A brief summary of Graphviz in python (explained only for mac)
Be careful of LANG for UnicodeEncodeError when printing Japanese with Python 3
Make a Spinbox that can be displayed in Binary with Tkinter
A timer (ticker) that can be used in the field (can be used anywhere)
I made a lot of files for RDP connection with Python
The story of making a standard driver for db with python.
Investigating what could be used as a Markdown parser in Python
I made a shuffle that can be reset (reverted) with Python
New features in Python 3.9 (1)-Union operators can be used in dictionary types
Python standard input summary that can be used in competition pro
Make a Spinbox that can be displayed in HEX with Tkinter
Get a list of files in a folder with python without a path
[Python] Basic knowledge used in AtCoder