Back to Data Science Notes
Topic #45

Python Basics

By the end of this lesson, you will be able to store data in variables, identify basic Python data types, and perform arithmetic operations essential for initial data exploration.

What it is

In Python, a variable is a named reference to an object stored in memory. Unlike statically typed languages, Python uses dynamic typing, meaning you do not declare the type explicitly; the interpreter infers it based on the assigned value. The core data types relevant to early-stage data science include int (integers), float (decimal numbers), str (strings/text), and bool (True/False). Operators are symbols that perform calculations or comparisons between these values.

Why it matters

  • Data Cleaning: You must understand types to convert messy strings into usable numbers.
  • Feature Engineering: Arithmetic operators allow you to create new metrics from existing columns.
  • Debugging: Knowing how Python handles implicit type conversion prevents silent errors in calculations.
  • Memory Management: Understanding references helps when working with large datasets where copying data is expensive.

Syntax or steps

  1. Assignment: Use the equals sign = to bind a name to a value. Example: x = 10.
  2. Type Inspection: Use the built-in function type() to check what kind of data a variable holds.
  3. Arithmetic: Use standard operators like +, -, *, / (true division), and // (floor division).
  4. Comparison: Use == for equality checks, returning a boolean result.

Example

# Define variables representing sales data
revenue = 5000.75       # float
units_sold = 120        # int
product_name = "Widget" # str
is_active = True        # bool

# Perform calculations
average_price = revenue / units_sold
total_units_next_month = units_sold * 2

# Check types and results
print(f"Average Price: {average_price}")
print(f"Type of average_price: {type(average_price)}")
print(f"Is active? {is_active}")

This code initializes four distinct data types. It then calculates the average price using true division (/), which always returns a float even if the inputs were integers. Finally, it prints the results along with their inferred types, demonstrating how Python dynamically manages data classification.

Common mistakes

  • Confusing = and ==: Using = inside an if statement causes a syntax error because it attempts assignment rather than comparison. Always use == for checking equality.
  • Integer Division Expectations: In Python 3, 5 / 2 results in 2.5. If you need whole numbers, use 5 // 2 which results in 2. Mixing these up can skew statistical averages.
  • Mutable Default Arguments: While less common in basics, modifying lists or dictionaries assigned to variables without understanding references can lead to unintended side effects elsewhere in your code.
  • String Concatenation Errors: Trying to add a string and an integer directly (e.g., "Age: " + 25) raises a TypeError. You must cast the number to a string first: "Age: " + str(25).

When to use it

Basic Python variables are used for scalar values and simple logic. For tabular data analysis, you should transition to libraries like pandas immediately after mastering these basics.

ScenarioUse Basic VariablesUse Pandas/DataFrame
Single calculationYesNo
Looping through rowsSlow/InefficientVectorized/Fast
Handling missing dataManual checksBuilt-in methods

Practice

Guided Exercise: Create two variables, hours_worked (float) and hourly_rate (float). Calculate total_pay by multiplying them. Print the result formatted to two decimal places.

Challenge: Take the total_pay from above. If total_pay is greater than 1000, set a variable bonus_eligible to True, otherwise False. Print the boolean value.

Quick check

Question: What is the output of print(type(10 / 2))?

Answer: <class 'float'>. Even though the mathematical result is a whole number, the division operator / always produces a float in Python 3.

Summary

Variables act as containers for data, while types define how that data behaves during operations. Mastering the distinction between integer and float division, as well as proper type casting, forms the foundation for reliable data manipulation in Python.

Want to go beyond the notes?

Join Coding Now Tech Institute's Data Science course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Python Basics – FAQs

Quick answers about learning Python Basics in Data Science.

This free note from Coding Now Tech Institute explains Python Basics in Data Science — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Data Science topic on Coding Now Tech Institute, including Python Basics, is 100% free with no signup required.
With focused practice, most students grasp Python Basics in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now