Menu

Variables and Data Types in Python

Variables and Data Types in Python

Variables store information that can be used and processed throughout a Python program. In data science, variables commonly represent datasets, measurements, customer details, sales figures, and analytical results.

Creating Variables

Python does not require you to declare a variable's data type explicitly. The type is determined by the value assigned to it.

product_name = "Laptop"

price = 55000

rating = 4.5

is_available = True

print(product_name)

print(price)

print(rating)

print(is_available)

You can use the type() function to identify a variable's data type.

print(type(product_name))  # <class 'str'>

print(type(price))         # <class 'int'>

print(type(rating))        # <class 'float'>

print(type(is_available))  # <class 'bool'>

Common Python Data Types

Data type

Description

Example

intWhole numbers25
floatDecimal numbers89.75
strText"Chennai"
boolTrue or false valuesTrue
listOrdered, changeable collection[10, 20, 30]
dictKey-value pairs{"sales": 5000}

Data types matter when analyzing data because different operations work with different kinds of values. For example, calculating an average requires numeric data, while text columns are usually processed differently.

Type Conversion

Sometimes, numerical values are stored as text. You can convert them when appropriate.

sales = "12500"

sales = int(sales)

tax_rate = "0.18"

tax_rate = float(tax_rate)

print(sales * 2)       # 25000

print(sales * tax_rate)  # 2250.0

Before converting data, ensure that the original value can be converted. A string such as "unknown" cannot be directly converted into an integer.


Python for Data Science

VA

Vishalini A



Learn via Video Course

undefined Logo

NaN hrs



Get in Touch For Details