Python learning basics ~ What is type conversion? ~

This is a memorandum of learning Python. We hope that it will be helpful for beginners in programming and those who are also interested in other languages.

What is type conversion?

Before we explain type conversion, we need to explain the data type. A data type is a type of data handled by programming.

For example, the value (object) assigned to a variable determines the data type of this variable. In other words, what type of data is it, such as numbers and character strings?

In addition, there are various types of data types. So-called "character string type", "numeric type", etc. I will omit it this time because it seems to be quite long if I write it in detail.

And if this data type is different, the code behaves differently. Therefore, type conversion is required.

print(2 + 2)  #Numerical calculation
#Execution result
print('2' + '2')  #String concatenation
#Execution result

An error will occur if you concatenate the "character string type" and "numeric type" that are different data types.

price = 1000
print('The price of this book is' + price + 'It's a yen')  
#The above is(String type+Numeric type+String type)Concatenation of different data types

#Execution result
#error:You cannot connect different types
TypeError: Can't convert 'int' object to str implicitly

Type conversion str

However, if you convert from "numeric type" to "character string type" when outputting the variable price as shown below, it will be treated as a concatenation of character strings, so it will be possible to concatenate and change this data type. This is called "** type conversion **".

Use "** str () **" to convert "numeric type" to "character string type".

price = 1000
print('The price of this book is' + str(price) + 'It's a yen')  
#The above is(String type+Convert to string type+String type)Can be connected next to

#Execution result
The price of this book is 1000 yen

Type conversion int

Contrary to the previous, when converting "character string type" to "numeric type", use "** int () **".

price = 1000
count = '5'  #String type
total_price = price * int(count)  #Convert to numeric type

#Execution result

in conclusion

The content is really rudimentary, so I would like to write an article again when I proceed with my studies. Also, please do not hesitate to point out anything! !!

Recommended Posts

Python learning basics ~ What is type conversion? ~
Basics of Python learning ~ What is a string literal? ~
What is python
What is Python
[Python] What is Pipeline ...
What is ensemble learning?
[Python] What is virtualenv
What is machine learning?
[Python] Python and security-① What is Python?
[Python] * args ** What is kwrgs?
Image data type conversion [Python]
What is a python map?
Python Basic Course (1 What is Python)
What is Python? What is it used for?
[Python] What is a zip function?
[Python] What is a with statement?
[Python] What is @? (About the decorator)
[python] What is the sorted key?
Python for statement ~ What is iterable ~
What is the python underscore (_) for?
Python> What is an extended slice?
(python) Deep Learning Library Chainer Basics Basics
Python basics ⑤
Python basics
Python basics ④
Python basics ③
Python basics
python learning
Python basics
Python basics
Python basics ③
Python basics ②
Python basics ②
What to do when the value type is ambiguous in Python?
[Python] What is pandas Series and DataFrame?
[Python] What is inherited by multiple inheritance?
What is NaN? NaN Zoya (Python) (394 days late)
[Learning memo] Basics of class by python
What kind of programming language is Python?
Why Python is chosen for machine learning
[Golang] Go language basics What is Goroutine?
What is "mahjong" in the Python library? ??
What is a dog? Python installation volume
[python] [meta] Is the type of python a type?
Python: Preprocessing in machine learning: Data conversion
[Machine learning] What is the LP norm?
[Example of Python improvement] What is the recommended learning site for Python beginners?
What is namespace
[What is an algorithm? Introduction to Search Algorithm] ~ Python ~
What is copy.copy ()
Python basics: list
[Python] Learning Note 1
What is "functional programming" and "object-oriented" in Python?
Python is easy
Python learning notes
What is Django? .. ..
What is dotenv?
What is POSIX?
python learning output
What is wheezy in the Docker Python image?
What is Linux