the 4 data types are:
int - integer numbers (can be positive or negative)
float - floating point numbers (can be positive or negative)
string - strings can be a single letter or a sentence.
bool - Boolean numbers are either True or False.
100
-1000
10_000
-25_000
10.1
-100.25
11.0
-200.0
'h'
'hello'
"hello"
"hello world"
True
False
The above types are the most commonly encountered. However, most modern programming languages require other types.
Yes, we need a type for when nothing exists.
None is a special constant in Python that represents the absence of a value or a null value. It is an object of its own datatype, the NoneType.
You can use None as a placeholder for an object that you don't want to instantiate yet, or as a default value for a function argument that has no default value specified.
x = None
if x:
print("x is not True")
elif x is False:
print ("x is not False")
else:
print("None is not True, or False, None is just None...")
In Python, the complex type represents complex numbers, which are numbers with a real and imaginary component. A complex number has the form a + bj, where a is the real component and b is the imaginary component, and j is the imaginary unit.
# Using the complex function
z1 = complex(3, 4) # 3 + 4j
z2 = complex(1, -2) # 1 - 2j
# Using the j notation
z3 = 3 + 4j
z4 = 1 - 2j
There are other types too, the most common are the list and the dict (short for dictionary).
These are collections of all of the above types and more.
the 4 built int data types are:
list
tuple
set
dict
In Python, a list is a collection of items that are ordered and changeable. Lists are written with square brackets, and the items are separated by commas.
# Create a list of integers
numbers = [1, 2, 3, 4, 5]
# Create a list of strings
names = ['Alice', 'Bob', 'Charlie']
# Create a list of mixed data types
mixed = [1, 'Alice', 2.5, True]
In Python, a dictionary is a collection of items that are stored as key-value pairs. Dictionaries are written with curly braces, and the items are separated by commas. The keys are used to access the values, and they must be unique within a dictionary.
# Create a dictionary with string keys and integer values
ages = {'Alice': 25, 'Bob': 30, 'Charlie': 35}
# Create a dictionary with integer keys and string values
colors = {1: 'red', 2: 'green', 3: 'blue'}
# Create a dictionary with mixed data types
mixed = {'Alice': 25, 1: 'one', 2.5: [1, 2, 3]}
In Python, everything is an object, and every object has a type. The type of an object determines the kind of value it represents and the operations that can be performed on it.