PythonIsCool - 2장
2장 - 데이터 유형
목차
Strings and Integers
Floats
문자열과 정수
Strings are the data type which are used to represent characters. They are shown in Python inside either double quotes ( ""
), or single quotes ( ''
). Integers are whole numbers, without the speech marks. Let's carry on from the statement in the previous chapter, one = 1
. If the statement would have been one = "1"
, the variable one
would have been a string, as it was in the speech marks, but since 1 wasn't in the speech marks, one
is an integer. A simple way to figure out if the variable is an integer or not is by using the function type()
. So write the the code print(type(one))
. If the output is <class 'int'>
, the variable is an integer. If the output is <class 'str'>
, the variable is a string. Other output mean other data types, but we will talk about that later. Now if you want to convert one
to a string, you have to do the code in the picture below.
onestring = str(one)
After you write that code and run it, one
will be equal to the integer 1
, but onestring
will be equal to "1"
.
If you ever come across the situation where you have to change a string to an integer, use int(name_of_variable)
. So if you want to convert onestring
to an integer, you could do oneinteger = int(onestring)
.
When you write an input statement, something like number = input("Enter a number")
, number is a string. But if you want number
to be an integer, you have to write the code below.
number = int(input("Enter a number"))
Then, number
will be an integer.
수레
Floats are numbers that are made up of whole numbers and fractions, they are written in decimals. You can also assign a variable the value of a float. There is a also a function called float()
. It coverts the specified value in a floating point number. The function looks like float(name_of_variable)
. For example, if you want to check out the float of 4.600, you will have to do print(float(4.600))
. When you run the code, you will get the result of the picture below in the console.
또 다른 예는 9의 실수를 확인하는 것입니다. 이를 확인하려면 print(float(9))
코드를 작성해야 합니다. 코드를 실행한 후 결과는 '9.0'이어야 합니다.
읽어 주셔서 감사합니다. 여기서 뭔가를 배웠기를 바랍니다.
Reference
이 문제에 관하여(PythonIsCool - 2장), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://dev.to/vulcanwm/pythoniscool-chapter-2-2ecc
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
""
), or single quotes ( ''
). Integers are whole numbers, without the speech marks. Let's carry on from the statement in the previous chapter, one = 1
. If the statement would have been one = "1"
, the variable one
would have been a string, as it was in the speech marks, but since 1 wasn't in the speech marks, one
is an integer. A simple way to figure out if the variable is an integer or not is by using the function type()
. So write the the code print(type(one))
. If the output is <class 'int'>
, the variable is an integer. If the output is <class 'str'>
, the variable is a string. Other output mean other data types, but we will talk about that later. Now if you want to convert one
to a string, you have to do the code in the picture below.onestring = str(one)
one
will be equal to the integer 1
, but onestring
will be equal to "1"
.If you ever come across the situation where you have to change a string to an integer, use
int(name_of_variable)
. So if you want to convert onestring
to an integer, you could do oneinteger = int(onestring)
. When you write an input statement, something like
number = input("Enter a number")
, number is a string. But if you want number
to be an integer, you have to write the code below.number = int(input("Enter a number"))
number
will be an integer.float()
. It coverts the specified value in a floating point number. The function looks like float(name_of_variable)
. For example, if you want to check out the float of 4.600, you will have to do print(float(4.600))
. When you run the code, you will get the result of the picture below in the console.Reference
이 문제에 관하여(PythonIsCool - 2장), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/vulcanwm/pythoniscool-chapter-2-2ecc텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)