Development

파이썬의 원시 유형.

sonpro 2023. 3. 15. 16:11
반응형

Primitive types

Primitive Types in Python

Python is a high-level programming language that is widely used in various fields such as data science, web development, and artificial intelligence. One of the fundamental concepts in Python is primitive types, which are the building blocks of any program. In this article, we will discuss the different primitive types in Python and their characteristics.

Summary

Primitive types in Python are the basic building blocks of any program. They include integers, floats, booleans, strings, and None. Each primitive type has its own set of characteristics and operations that can be performed on them. Understanding these types is essential for any Python developer.

Integers

Integers are whole numbers that can be positive, negative, or zero. They are represented in Python using the int keyword. Integers have unlimited precision, which means they can be as large or as small as needed. Some common operations that can be performed on integers include addition, subtraction, multiplication, and division.

x = 10
y = 5
print(x + y) # Output: 15
print(x - y) # Output: 5
print(x * y) # Output: 50
print(x / y) # Output: 2.0

Floats

Floats are decimal numbers that can be positive, negative, or zero. They are represented in Python using the float keyword. Floats have a limited precision, which means they can only represent a certain number of decimal places. Some common operations that can be performed on floats include addition, subtraction, multiplication, and division.

x = 3.14
y = 2.71
print(x + y) # Output: 5.85
print(x - y) # Output: 0.43
print(x * y) # Output: 8.5094
print(x / y) # Output: 1.159

Booleans

Booleans are a type of variable that can only have two values: True or False. They are represented in Python using the bool keyword. Booleans are often used in conditional statements to control the flow of a program.

x = True
y = False
print(x and y) # Output: False
print(x or y) # Output: True
print(not x) # Output: False

Strings

Strings are a sequence of characters that are enclosed in quotes. They are represented in Python using the str keyword. Strings can be concatenated using the + operator and repeated using the * operator.

x = "Hello"
y = "World"
print(x + " " + y) # Output: Hello World
print(x * 3) # Output: HelloHelloHello

None

None is a special type in Python that represents the absence of a value. It is often used as a placeholder or default value.

x = None
print(x) # Output: None

In conclusion, primitive types are the basic building blocks of any program in Python. Understanding these types and their characteristics is essential for any Python developer. By mastering these types, you will be able to write more efficient and effective code.

반응형