Data Types
Introduction to Python
Getting Started
Comments
Variables
Data Types
Type Casting
Input and Output
Operators
Strings
Booleans
Conditional Statements
While Loop
For Loop
Loop Control Statements
Lists
Tuples
Sets
Dictionaries
Functions
Function Arguments and Return Values
Variable Scope
Recursion
Lambda Functions
Arrays and Python Lists
Modules
Dates and Math
String Formatting
File Handling
Try and Except
Classes and Objects
Inheritance
Iterators
JSON
RegEx
Data Types
Definition: Data types represent the kind of value a variable holds. Python supports several basic data types such as integers, floating-point numbers, strings, and booleans.
Why: Data types are a core foundation topic because they determine what kind of operations you can perform on a piece of data (for example, you can multiply integers, but you cannot multiply strings in the same way).
Common Data Types
- Integer (int): Whole numbers without decimals (e.g., 10, -5).
- Float: Numbers with decimal points (e.g., 3.14, 2.0).
- String (str): Text wrapped in quotes (e.g., "Ravi", 'Python').
- Boolean (bool): Represents logical values:
TrueorFalse.
Example
x = 10
pi = 3.14
name = "Ravi"
is_open = True
print(type(x))
print(type(pi))
print(type(name))
print(type(is_open))
Explanation
- The
type()function is used to check the data type of any variable. xis identified as<class 'int'>.piis identified as<class 'float'>.nameis identified as<class 'str'>.is_openis identified as<class 'bool'>.
Key Notes
- Python is dynamically typed, meaning you don't have to explicitly state the type; Python figures it out for you.
- Booleans must always start with a capital letter (
True/False).
Example
Executing...
❌ Error:
✅ Output:
// Click Run ▶ to execute