In Python, variables are dynamically typed, meaning that you do not need to explicitly declare the data type of a variable when you create it. The data type is determined by the value assigned to the variable. Here’s how variables handle different data types:
-
Integer: Represents whole numbers.
age = 25 # Integer -
Float: Represents decimal numbers.
height = 1.75 # Float -
String: Represents text.
name = "Alice" # String -
Boolean: Represents
TrueorFalsevalues.is_student = True # Boolean -
List: Represents an ordered collection of items.
scores = [90, 85, 88] # List -
Tuple: Represents an immutable ordered collection of items.
coordinates = (10.0, 20.0) # Tuple -
Dictionary: Represents a collection of key-value pairs.
student = {"name": "Alice", "age": 25} # Dictionary -
Set: Represents an unordered collection of unique items.
unique_numbers = {1, 2, 3} # Set
You can also convert between data types using built-in functions like int(), float(), str(), etc. For example:
num_str = "100"
num_int = int(num_str) # Converts string to integer
This flexibility allows you to work with different data types easily and perform various operations on them.
