Standard Data Types
Master the four fundamental data types in programming: integer, float, string, and Boolean. Learn when and how to use each type effectively in your programs.
Learning Objectives
- Understand the four standard data types: integer, float, string, and Boolean
- Know the appropriate usage contexts for each data type
- Apply data type selection based on the nature of data being stored
- Recognize how different data types are represented in memory
- Identify common errors related to incorrect data type usage
Standard Data Types
Integer
Whole numbers without decimal points, both positive and negative
Examples:
42-170999Common Uses:
Counting items, array indices, loop counters, age, quantity
Memory Note:
Typically 32 or 64 bits, exact range depends on system
Python Examples:
age = 18
count = 0
temperature = -5
score = 100Float
Numbers with decimal points, used for precise calculations
Examples:
3.14-2.50.001123.45Common Uses:
Measurements, calculations, percentages, coordinates, prices
Memory Note:
Usually 64-bit double precision, limited precision for very large/small numbers
Python Examples:
price = 19.99
pi = 3.14159
percentage = 75.5
coordinate = -12.345String
Sequences of characters including letters, numbers, and symbols
Examples:
"Hello""123 Main St""user@email.com"""Common Uses:
Names, addresses, messages, file paths, user input, display text
Memory Note:
Variable length, each character typically 1-4 bytes depending on encoding
Python Examples:
name = "Alice Smith"
email = "alice@example.com"
message = "Welcome to the system!"
empty_string = ""Boolean
Logical values representing true or false states
Examples:
TrueFalseCommon Uses:
Flags, conditions, status indicators, yes/no questions, switches
Memory Note:
Usually 1 byte, though may be optimized to 1 bit in arrays
Python Examples:
is_student = True
game_over = False
logged_in = True
has_permission = FalseCommon Mistakes & Solutions
Using strings for numbers
Example:
age = "25" instead of age = 25Problem:
Cannot perform mathematical operations
Solution:
Use integer or float for numeric data
Mixing data types in calculations
Example:
"Age: " + 25 without conversionProblem:
Type error - cannot add string and integer
Solution:
Convert types: "Age: " + str(25)
Using float for counting
Example:
Using 5.0 instead of 5 for loop counterProblem:
Unnecessary precision and potential precision errors
Solution:
Use integers for discrete counting
Incorrect Boolean comparisons
Example:
if is_ready == True instead of if is_readyProblem:
Redundant comparison, less readable
Solution:
Boolean variables can be used directly in conditions
Learning Activities
Data Type Selection Challenge
Given various real-world data scenarios, choose the most appropriate data type
Type Error Debugging
Identify and fix common data type errors in Python code samples
Data Type Conversion Practice
Convert between different data types and understand when conversion is needed