[Python] I tried to get the type name as a string from the type function

Motive Paiza has recently released Code Chronicle, which combines RPG games and code learning.

So I tried to solve the problem, but since it is for beginners, the question about the basic syntax was repeatedly asked, so the difficulty level is not difficult, but the following questions were asked. (Not exactly the same.)


#Please output the variable type of pi
pi = 3.14

print("") #int, str,Please enter one of the floats.

The answer is print ("float"), but I wondered if it could be used because there is a type function. : rolling_eyes:

Method

If you simply use the type function, ...

>> type(pi)
#<class 'float'> 

The type of the class is output as shown.

I thought I should simply use str (), but ...

>> str(type(pi))
#"<class 'float'>"

The " <class'float'> " is output as a string, which is different from the expected float.

Read about type functions in Python document Looking at it,

The * name string is the class name and has the __name__ attribute. *

Since it was written, you can get float by getting the attribute of the class.

>> type(pi).__name__
# `float`

Digest

If you get the problem of #Please output variable type, you can use any of integer, string, and decimal point types. type(pi).__name__ It is a silver bullet that will be the correct answer if you write. : gun:

Future Code Chronicle I think it's an app game that matches the program learning that the government is pushing forward from next year: robot :. If you create a problem while considering the game balance and design a mechanism to create a problem, it seems that operation will be difficult unless you have all the development members. This difficulty may be fine for beginners.

Reference -Code Chord -type function

Recommended Posts

[Python] I tried to get the type name as a string from the type function
[Python] I tried substituting the function name for the function name
Get the variable name of the variable as a character string.
I tried to summarize the string operations of Python
I want to get the file name, line number, and function name in Python 3.4
I tried "How to get a method decorated in Python"
I tried to implement the mail sending function in Python
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
I tried to get various information from the codeforces API
I tried to get data from AS / 400 quickly using pypyodbc
A super introduction to Django by Python beginners! Part 6 I tried to implement the login function
I tried to get the index of the list using the enumerate function
[Introduction to Python] How to split a character string with the split function
[Python] A memo that I tried to get started with asyncio
How to get a string from a command line argument in python
I also tried to imitate the function monad and State monad with a generator in Python
I tried to cut out a still image from the video
Get the formula in an excel file as a string in Python
I tried to get data from AS / 400 quickly using pypyodbc Preparation 1
[Python] I tried to summarize the set type (set) in an easy-to-understand manner.
Mayungo's Python Learning Episode 6: I tried to convert a character string to a number
[Python memo] I want to get a 2-digit hexadecimal number from a decimal number
I tried to get the authentication code of Qiita API with Python.
The eval () function that calculates a string as an expression in python
[Introduction to Python] How to write a character string with the format function
Get the song name from the title of the video you tried to sing
I tried to get the movie information of TMDb API with Python
Python: I want to measure the processing time of a function neatly
I made a function to see the movement of a two-dimensional array (Python)
How to create a function object from a string
Get the caller of a function in Python
I tried to generate a random character string
A super introduction to Django by Python beginners! Part 3 I tried using the template file inheritance function
Python programming: I tried to get company information (crawling) from Yahoo Finance in the US using BeautifulSoup4
python I don't know how to get the printer name that I usually use.
I tried to explain what a Python generator is for as easily as possible.
[IBM Cloud] I tried to access the Db2 on Cloud table from Cloud Funtions (python)
[Python] I tried the same calculation as LSTM predict with from scratch [Keras]
I tried to learn the sin function with chainer
I want to see the file name from DataLoader
I tried to graph the packages installed in Python
I want to embed a variable in a Python string
How to get the variable name itself in python
I tried to detect the iris from the camera image
I tried to get started with blender python script_Part 01
I tried to touch the CSV file with Python
I tried running python etc. from a bat file
I tried to solve the soma cube with python
I tried to implement a pseudo pachislot in Python
I tried to get started with blender python script_Part 02
I wanted to use the Python library from MATLAB
I tried to approximate the sin function using chainer
[Python] I tried to graph the top 10 eyeshadow rankings
I tried to automatically generate a password with Python3
Consider a conversion from a Python recursive function to a non-recursive function
I tried to solve the problem with Python Vol.1
python> datetime> From date string (ISO format: 2015-12-09 12:40:08) to datetime type
[Python] I tried to get Json of squid ring 2
[Python] How to call a c function from python (ctypes)
<Python> A quiz to batch convert file names separated by a specific character string as part of the file name
Get the value of a specific key in a list from the dictionary type in the list with Python