Skip to content
On this page

Variables and Data Types

Declaring Variables:

In Python, you can declare a variable simply by assigning a value to it. No need to specify the data type explicitly.

Example:

python
# Declaring variables
name = "John"
age = 30
height = 6.2
is_student = True

Basic Data Types: int, float, str, bool

Python supports several basic data types:

  • int: Integer type, e.g., 5, -10.
  • float: Floating-point type, e.g., 3.14, -0.5.
  • str: String type, e.g., "Hello, World!".
  • bool: Boolean type, e.g., True, False.

Example:

python
# Basic data types
num1 = 10
num2 = 3.14
name = "Alice"
is_student = True

Type Conversion:

Python allows type conversion between different data types using built-in functions.

Example:

python
# Type conversion
num_str = "25"
num_int = int(num_str)     # Convert string to integer
num_float = float(num_str) # Convert string to float

result = str(num_int)      # Convert integer to string