[PYTHON] What I learned at hackerrank on 1/30 days.

This article When working on HackerRank 1/30 using python, This is a summary of what I have investigated.

Data type ~ Handling of numerical values ~

int: Handle integers double: 15 valid digits float: 6 or 7 effective digits string: Handling of string type round: rounding

Data type conversion is called cast. Note that you often forget str when printing numbers with print.

You can recognize character strings one by one with list

Example)

str="Hacker"
char_list=list(str)
print(char_list)

Then, ["H "," a "," c "," k "," e "," r "] is output.

Convert a list to a string

So this time, ["H", "a", "c", "k", "e", "r"] Is displayed as

To Hacker I want to fix it.

① Use for

str_list = ['python', 'list', 'exchange'] 
 mojiretu = ' ' 
for x in str_list:
mojiretu += x
print(mojiretu) 

Execution result: pythonlistexchange

② Use join How to use the join function String ='delimiter'.join (list)

str_list = ['python', 'list', 'exchange']
mojiretu = ','.join(str_list) 
print(mojiretu)

Execution result: python, list, exchange

slice

This is a useful way to partially retrieve the elements of a column.

Basic example) It is quoted from @ ycctw1443.

a = [1, 2, 3, 4, 5]
print(a[0: 4])
print(a[: 4])
print(a[-3:])
print(a[2: -1])

Then [1, 2, 3, 4] [1, 2, 3, 4] [3, 4, 5] [3, 4] Is output.

Develop this, You can also "get elements every nth". ʻA [Start position: End position: Slice increment] `.

a = [1, 2, 3, 4, 5]
print(a[:: 2])
print(a[1:: 2])
print(a[::-1])
print(a[1::-1])

result) [1, 3, 5] [2, 4] [5, 4, 3, 2, 1] [2, 1]

Enter multiple strings

input().split()

print and%

Use% to get a string that contains variables You can output it concisely.

print("My favorite fruit is%s." %'Apple') 
print("My favorite fruit is%s and%s." %('Apple','Orange')) 

x = 'Football' 
y = 'snow board' 
print("what sports do you like,%s and%s."%(x,y)) 

% s stands for str () You can display integers and decimals as strings, but % d is an integer.

% r is repr () Display the passed value as it is.

Star operator

You can expand the array.

reference

https://programming-study.com/technology/python-list-join/ https://code-graffiti.com/print-format-with-string-in-python/

Recommended Posts

What I learned at hackerrank on 1/30 days.
What I learned about Linux
What I thought and learned to study for 100 days at a programming school
What I learned in Python
What I found by deploying Django on EC2
What I learned about AI / machine learning using Python (1)
I learned Python with a beautiful girl at Paiza # 01
This time I learned Python I and II at Progate.
What I did when I stumbled on a Django tutorial
What I learned about AI / machine learning using Python (3)
What I learned by participating in the ISUCON10 qualifying
What I learned about AI / machine learning using Python (2)
A note of what I learned when I thought about using pyenv or virtualenv on Windows
What I thought after working on the "No comment at all" project for a year
What I learned about AI and machine learning using Python (4)
What I learned by solving 30 questions of python Project Euler
I stumbled on TensorFlow (What is Out of GPU Memory)