Python Datatypes And Numbers
- Read
- Discuss
Python has several built-in data types that allow you to store and manipulate different kinds of data. Here is a list of the most common data types in Python:
Data Types
- Integers: Integers are whole numbers that can be positive, negative, or zero. For example 10, -5, 0. In Python, you can use the int data type to represent integers.
- Floats: Floats are numbers with decimal points. For example 3.14, -2.5, 0.0. In Python, you can use the float data type to represent floats.
- Strings: Strings are sequences of characters that can be used to represent text. In Python, you can use the str data type to represent strings. You can use single or double quotes to define a string, like this: ‘hello’ or “world”.
- Booleans: Booleans represent one of two values: True or False. In Python, you can use the bool data type to represent booleans.
- Lists: Lists are ordered collections of items of different types. In Python, you can use the list data type to represent lists. You can define a list by enclosing a comma-separated sequence of items in square brackets, like this: [1, 2, 3, 4, 5].
- Tuples: Tuples are similar to lists, but they are immutable, meaning you cannot modify their contents once they are created. In Python, you can use the tuple data type to represent tuples. You can define a tuple by enclosing a comma-separated sequence of items in parentheses, like this: (1, 2, 3, 4, 5).
- Dictionaries: Dictionaries are unordered collections of key-value pairs. In Python, you can use the dict data type to represent dictionaries. You can define a dictionary by enclosing a comma-separated sequence of key-value pairs in curly braces, like this: {‘name’: ‘John’, ‘age’: 30}.
Numbers
Python has three types of numbers: integers, floating-point numbers, and complex numbers.
Integers
An integer is a whole number that can be positive, negative, or zero. In Python, you can use the int type to represent an integer. Here are some examples of integers:
0
1
-1
1000
-1000
You can perform arithmetic operations on integers using the standard operators: +, -, *, /, and %. Here are some examples:
# Addition
print(1 + 2) # 3
# Subtraction
print(1 - 2) # -1
# Multiplication
print(2 * 3) # 6
# Division
print(7 / 3) # 2.3333333333333335
# Modulus (remainder)
print(7 % 3) # 1
The following will be the output:
3
-1
6
2.3333333333333335
1
Floating-Point Numbers
A floating-point number is a number with a decimal point. You can use the float type in Python to represent a floating-point number. Here are some examples of floating-point numbers:
0.0
1.0
-1.0
1.5
-1.5
3.14
You can perform arithmetic operations on floating-point numbers using the standard operators just like with integers. Here are some examples:
# Addition
print(1.0 + 2.5) # 3.5
# Subtraction
print(1.0 - 2.5) # -1.5
# Multiplication
print(2.5 * 3.0) # 7.5
# Division
print(7.5 / 3.0) # 2.5
#Note that floating-point arithmetic is not always exact, and you may see some rounding errors. For example:
print(0.1 + 0.2) # 0.30000000000000004
The following will be the output:
-1.5
7.5
2.5
0.30000000000000004
Complex Numbers
A complex number is a number with a real and imaginary part. In Python, you can use the complex type to represent a complex number. The real part is specified first and the imaginary part is specified second, separated by a + or – sign. The imaginary part is followed by the letter j. Here are some examples of complex numbers:
1 + 2j
-1 + 2j
1 - 2j
-1 - 2j
You can perform arithmetic operations on complex numbers using the standard operators just like with real numbers.
Here are some examples:
# Addition
print((1+2j) + (3+4j)) # (4+6j)
# Subtraction
print((1+2j) - (3+4j)) # (-2-2j)
# Multiplication
print((1+2j) * (3+4j)) # (-5+10j)
# Division
print((1+2j) / (3+4j)) # (0.44+0.08j)
The following is the output.
(4+6j)
(-2-2j)
(-5+10j)
(0.44+0.08j)