Python Syntax, Comments, and Variables | Python for Analysis Tutorial - Learn with VOKS
Back Next

Python Syntax, Comments, and Variables


To analyze data effectively, you must first speak the language. Python’s syntax is designed to be "clean," meaning it strips away the clutter found in other programming languages to let the logic of your data shine through.


1. Python Syntax: The Rule of Indentation

In most programming languages, you use curly braces { } or semicolons ; to show where a block of code begins and ends. Python does not use these. Instead, it uses Indentation (whitespace).

  • The Rule: Code that belongs together must be indented by the same number of spaces (usually 4 spaces or 1 tab).
  • Why it matters for Data: When you are writing complex logic to filter a dataset, indentation makes it visually obvious which lines of code are part of that logic.
Warning: Incorrect indentation will result in an IndentationError. Consistency is key.


2. Comments: Documenting Your Analysis

A "better detailed" tutorial must emphasize that code is read more often than it is written. Comments are notes in plain English that explain why you are doing a specific calculation. The Python interpreter ignores these lines.

  • Single-line Comments: Start with the hash symbol #.
  • Example: # Calculate the average monthly revenue
  • Multi-line Comments: Use triple quotes """ at the start and end. These are often used for "Docstrings" to describe what a whole script or function does.

Pro-Tip for Data Analysts: Use comments to explain the source of your data or the reason you removed specific "dirty" records. This makes your work reproducible for others.


3. Variables: The Containers for Data

Variables are reserved memory locations to store values. In data analysis, you can think of a variable as a labeled box where you keep a piece of information so you can use it later.

A. Creating Variables

In Python, you create a variable the moment you assign a value to it using the = sign (the assignment operator).

Python


# Storing a single data point
customer_age = 25
average_score = 88.5
company_name = "DataCorp"

B. Naming Rules (Best Practices)

To keep your analysis professional and readable, follow these naming conventions:

  • Snake Case: Use lowercase letters and underscores (e.g., total_sales_2026). This is the Python standard.
  • Descriptive Names: Avoid vague names like x or y. Use sales_tax or employee_list so anyone reading your code understands the data inside.
  • Start with a Letter: Variables cannot start with a number or a special symbol like @ or $.


4. Dynamic Typing

Python is "dynamically typed." This means you do not have to tell Python what kind of data you are storing (number vs. text); it figures it out automatically.

You can even change the type of data stored in a variable by simply assigning it a new value:

Python


data_point = 100       # Initially an integer (number)
data_point = "Empty"   # Now it's a string (text)

Note: While Python allows this, in data analysis, it is best practice to keep a variable's data type consistent to avoid errors during mathematical operations.


5. Case Sensitivity

Python is case-sensitive. This means the following are three different variables:

  1. Sales
  2. sales
  3. SALES


Python for Analysis
What is Python? Python Syntax, Comments, and Variables Python Data Types — Numeric, Strings, and Sequences Mapping Data Types — The Power of Dictionaries The Boolean Data Type — The Logic of Data Analysis Numbers and Type Casting Conditional Statements — If and Else Python Modules — Organizing and Reusing Code Number Arrays (NumPy) — The Foundation of Data Analysis Pandas; Pandas series, Dataframe, Read CSV, cleaning data, dealing with empty data, removing duplicates, pandas plotting Mastering Pandas for Data Analysis Data Visualization with Matplotlib Statistical Data Visualization
All Courses
Advance AI Bootstrap C C++ Computer Vision Content Writing CSS Cyber Security Data Analysis Deep Learning Email Marketing Excel Figma HTML Java Script Machine Learning MySQLi Node JS PHP Power Bi Python Python for AI Python for Analysis React React Native SEO SMM SQL