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'이어야 합니다.


읽어 주셔서 감사합니다. 여기서 뭔가를 배웠기를 바랍니다.

좋은 웹페이지 즐겨찾기