Data Types and Variables

Learn about fundamental data types in Python such as integers, floats, strings, and booleans, and how to declare and use variables.


Data Types and Variables in Python

Introduction

In Python, data types classify the kind of value that a variable can hold. Understanding data types is fundamental to writing effective and error-free code. Variables are used to store these values in memory, allowing us to refer to and manipulate them throughout our program.

Fundamental Data Types

Python has several built-in data types. We'll focus on the most common ones:

1. Integers (int)

Integers represent whole numbers (without a fractional part). They can be positive, negative, or zero.

# Example: Integer
age = 30
temperature = -5
count = 0

print(type(age))  # Output: <class 'int'> 

2. Floats (float)

Floats represent numbers with a decimal point (i.e., floating-point numbers). They are used for representing real numbers.

# Example: Float
price = 99.99
pi = 3.14159
distance = 123.45

print(type(price)) # Output: <class 'float'> 

3. Strings (str)

Strings represent sequences of characters. They are used to store text. Strings are enclosed in single quotes (') or double quotes (").

# Example: String
name = "Alice"
message = 'Hello, World!'
empty_string = ""

print(type(name)) # Output: <class 'str'> 

4. Booleans (bool)

Booleans represent truth values: True or False. They are often used in conditional statements and logical operations.

# Example: Boolean
is_active = True
is_valid = False

print(type(is_active)) # Output: <class 'bool'> 

Declaring and Using Variables

In Python, you don't need to explicitly declare the data type of a variable. You simply assign a value to a variable name, and Python automatically infers the type.

# Assigning values to variables
x = 10          # Integer
y = 2.5         # Float
name = "Bob"    # String
is_adult = True # Boolean

# Printing the values of variables
print(x)
print(y)
print(name)
print(is_adult)

# Performing operations with variables
sum_result = x + y
print("Sum:", sum_result)

greeting = "Hello, " + name + "!"
print(greeting) 

Important Note: Variable names are case-sensitive. myVar is different from myvar.

Type Conversion (Casting)

Sometimes you need to convert a value from one data type to another. Python provides built-in functions for type conversion:

  • int(): Converts a value to an integer.
  • float(): Converts a value to a float.
  • str(): Converts a value to a string.
  • bool(): Converts a value to a boolean.
# Type Conversion Examples
num_str = "10"
num_int = int(num_str)  # Convert string to integer
print(num_int + 5)     # Output: 15

num_float = float("3.14") #Convert String to Float
print(num_float * 2) #Output: 6.28

num = 5
num_str = str(num)  # Convert integer to string
print("The number is: " + num_str)

value = 0
boolean_value = bool(value)  #Convert to boolean - 0 evaluates to False
print(boolean_value)
value = 1
boolean_value = bool(value) #Convert to boolean - non-zero evaluates to True
print(boolean_value) 

Be careful when converting between data types, as invalid conversions can raise errors (e.g., trying to convert the string "abc" to an integer).