Skip to main content
Python for Data Science
CHAPTER 03 Beginner

Python Basics for Data Science

Updated: May 18, 2026
5 min read

# CHAPTER 3

Python Basics for Data Science

1. Chapter Introduction

You have your environment set up. Now it is time to write code. Python was designed to be highly readable—it looks closer to plain English than almost any other programming language. This chapter covers the absolute fundamentals: how to write syntax, how Python uses indentation instead of brackets, how to store data in variables, and how to display output.

2. Python Syntax and Indentation

Most programming languages use curly braces {} to define blocks of code and semicolons ; to end lines. Python does not.

Python relies on indentation (whitespace) to structure code. You must be consistent!

python
12345678
# CORRECT INDENTATION (4 spaces)
if 5 > 2:
    print("Five is greater than two!")
    print("This line is also inside the if block.")

# INCORRECT INDENTATION (Will throw an IndentationError)
if 5 > 2:
print("This will fail!")

3. Comments

Comments are notes written in the code that the computer completely ignores. They are used to explain *why* the code is doing something to other humans (or yourself in 6 months).

python
12345678
# This is a single-line comment using the hashtag symbol.
print("Hello World") # This is an inline comment

"""
This is a multi-line comment (or docstring).
It uses three double-quotes.
Data scientists use this to explain complex functions.
"""

4. Variables

A variable is a labeled container for storing data. In Python, you do not need to declare what type of data the variable will hold; Python figures it out automatically. You assign data to a variable using the = sign.

python
1234567
# Assigning values to variables
customer_name = "Alice"
age = 28
account_balance = 1500.50

# Variables can be updated later
age = 29 

Naming Rules for Variables:

  • Must start with a letter or an underscore .
  • Cannot start with a number.
  • Can only contain alphanumeric characters and underscores (A-Z, 0-9, and ).
  • Are case-sensitive (Age and age are different).
  • *Best Practice:* Use snakecase for Python variable names (e.g., totalsales).

5. Printing Output

To display the contents of a variable to the screen, use the print() function.

python
123456789101112
first_name = "John"
last_name = "Doe"

# Printing a single variable
print(first_name)

# Printing multiple items (automatically separated by a space)
print("The customer is:", first_name, last_name)

# Formatted Strings (f-strings) - THE MODERN WAY
# Prefix the string with 'f' and use {} for variables
print(f"The customer's full name is {first_name} {last_name}.")

6. User Input

For interactive scripts, you can ask the user to type something using the input() function.

python
1234
# The program will pause here and wait for the user to type
user_city = input("What city are you from? ")

print(f"Wow, {user_city} is a beautiful city!")

*Note: The input() function ALWAYS returns text (a string), even if the user types a number. We will learn how to convert types in the next chapter.*

7. Mini Project: Simple Calculator Input

Let's combine what we've learned into a simple interactive script.

simple_greeting.py
123456789101112

print("--- Welcome to the Data Portal ---")

# Gather variables
user = input("Enter your username: ")
department = input("Enter your department (e.g., Sales, HR): ")

# Output formatted summary
print("\n--- Portal Access Granted ---")
print(f"User: {user}")
print(f"Dept: {department}")
print("Loading analytics dashboard...")

8. Common Mistakes

  • Mixing Tabs and Spaces: Python hates it when you use the Tab key on one line, and 4 Spacebar taps on the next line to indent code. Pick one (spaces are the industry standard) and stick to it. Most modern IDEs convert Tabs to spaces automatically.
  • Forgetting f-string syntax: Writing print("Hello {name}") without the f in front will literally print the text "Hello {name}". It must be print(f"Hello {name}").

9. MCQs

Question 1

How does Python define a block of code?

Question 2

Which symbol is used to create a single-line comment in Python?

Question 3

Which of the following is a valid Python variable name?

Question 4

What is the standard naming convention for variables in Python?

Question 5

How do you assign the value 100 to the variable score?

Question 6

What is the output of print(f"Age is {20+5}")?

Question 7

What does the input() function do?

Question 8

If a user types 50 into an input() prompt, what data type does Python store it as by default?

Q9. Is Python case-sensitive? (e.g., are Data and data different variables?) a) Yes b) No — Answer: a
Question 10

What happens if you forget to indent code that belongs inside an if statement?

10. Interview Questions

  • Q: Why does Python use indentation instead of brackets for code blocks? What are the pros and cons of this design?
  • Q: Explain the difference between = and == in Python syntax (Briefly prep for next chapter).

11. Summary

Python is known for its readability. It relies on indentation rather than curly brackets to organize code blocks. Use the # symbol to write comments to explain your logic. Variables are created instantly upon assignment using =. Modern Python relies heavily on f-strings (f"Text {variable}") to seamlessly print variables combined with text, and you can interact with users using the input() function.

12. Next Chapter Recommendation

In Chapter 4: Variables, Data Types, and Operators, we dive deeper into the specific types of data Python can hold (Integers, Floats, Strings, Booleans) and how to perform mathematical and comparative operations on them.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·