- Example 1: Integer (int)
- Example 2: Floating-Point Number (float)
- Example 3: Complex Number (complex)
- Example 4: Boolean (bool)
- Example 5: String (str)
- Example 6: Multiple Data Types
- Example 7: Scientific Notation
- Example 8: Type Conversion Between Data Types
- Example 9: Using Different Data Types Together
- Example 10: Checking the Data Type
- Common Mistakes
- Best Practices
- Key Points to Remember
A data type defines the kind of value that a variable can store and the operations that can be performed on that value.
Python provides several built-in data types. The most commonly used basic data types are:
int(Integer)float(Floating-Point Number)complex(Complex Number)bool(Boolean)str(String)
Every value in Python belongs to one of these data types.
Example 1: Integer (int) #
main.py
age = 25
print(age)
print(type(age))
Output #
25
<class 'int'>
Explanation #
25is an integer value.- Integers are whole numbers without a decimal point.
- The
type()function confirms that the value belongs to theintdata type.
Example 2: Floating-Point Number (float) #
main.py
price = 99.95
print(price)
print(type(price))
Output #
99.95
<class 'float'>
Explanation #
99.95contains a decimal point.- Therefore, Python stores it as a floating-point number.
- Floating-point numbers are represented by the
floatdata type.
Example 3: Complex Number (complex) #
main.py
number = 3 + 4j
print(number)
print(type(number))
Output #
(3+4j)
<class 'complex'>
Explanation #
- A complex number contains a real part and an imaginary part.
- The imaginary part is represented using the letter
j. - Python provides built-in support for complex numbers through the
complexdata type.
Example 4: Boolean (bool) #
main.py
result1 = True
result2 = False
print(result1)
print(result2)
print(type(result1))
Output #
True
False
<class 'bool'>
Explanation #
- Boolean values can only be
TrueorFalse. - They are commonly used in decision-making and comparisons.
- The data type of Boolean values is
bool.
Example 5: String (str) #
main.py
language = "Python"
print(language)
print(type(language))
Output #
Python
<class 'str'>
Explanation #
- A string is a sequence of characters enclosed in quotation marks.
- Strings are represented by the
strdata type. - Strings can contain letters, numbers, symbols, and spaces.
Example 6: Multiple Data Types #
main.py
age = 25
height = 5.9
name = "Alice"
is_student = True
print(type(age))
print(type(height))
print(type(name))
print(type(is_student))
Output #
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
Explanation #
- Python variables can store different kinds of data.
- Each variable has its own data type.
- The
type()function displays the type of every variable.
Example 7: Scientific Notation #
main.py
distance = 3.5e6
print(distance)
print(type(distance))
Output #
3500000.0
<class 'float'>
Explanation #
- Scientific notation uses
eorE. 3.5e6means3.5 ร 10โถ.- Scientific notation values are stored as floating-point numbers.
Example 8: Type Conversion Between Data Types #
main.py
number = 25
decimal = float(number)
print(decimal)
print(type(decimal))
Output #
25.0
<class 'float'>
Explanation #
numberis an integer.float()converts it into a floating-point number.- The converted value has the
floatdata type.
Example 9: Using Different Data Types Together #
main.py
name = "Alice"
age = 22
height = 5.4
print(name, age, height)
Output #
Alice 22 5.4
Explanation #
- Python allows different data types to be used together.
- The
print()function can display values of different types in a single statement. - By default,
print()separates the values with spaces.
Example 10: Checking the Data Type #
main.py
value = False
print(type(value))
Output #
<class 'bool'>
Explanation #
- The variable
valuestores the Boolean valueFalse. - The
type()function identifies its data type. - The returned type is
bool.
Common Mistakes #
1. Assuming Numbers with Decimal Points are Integers #
Incorrect Assumption
number = 10.0
Incorrect Expectation #
<class 'int'>
Actual Output #
<class 'float'>
Reason
Any numeric value containing a decimal point is a floating-point number.
2. Forgetting Quotes Around Strings #
Incorrect
name = Python
Output #
NameError: name 'Python' is not defined
Reason
Text values must be enclosed in quotation marks.
Correct
name = "Python"
3. Using true and false Instead of True and False #
Incorrect
status = true
Output #
NameError: name 'true' is not defined
Reason
Python Boolean values are written as True and False with uppercase first letters.
Correct
status = True
4. Confusing Strings with Numbers #
Incorrect
age = "25"
print(age + 5)
Output #
TypeError: can only concatenate str (not "int") to str
Reason
"25" is a string, not an integer.
Correct
age = int("25")
print(age + 5)
Best Practices #
- Choose the appropriate data type for the information being stored.
- Use
type()while learning to verify data types. - Keep numeric and string data separate unless conversion is required.
- Use meaningful variable names that indicate the type of data being stored.
- Convert values explicitly when needed using functions such as
int(),float(), andstr().
Key Points to Remember #
- A data type specifies the kind of value stored by a variable.
- Python’s common basic data types include
int,float,complex,bool, andstr. - Integers represent whole numbers.
- Floating-point numbers represent numbers with decimal points.
- Complex numbers use the
jsuffix for the imaginary part. - Boolean values are
TrueandFalse. - Strings are sequences of characters enclosed in quotation marks.
- The
type()function returns the data type of a value or variable.