Python Basics for Data Science
# 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!
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).
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.
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 (
Ageandageare different).
-
*Best Practice:* Use
snakecasefor Python variable names (e.g.,totalsales).
5. Printing Output
To display the contents of a variable to the screen, use the print() function.
6. User Input
For interactive scripts, you can ask the user to type something using the input() function.
*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.
8. Common Mistakes
-
Mixing Tabs and Spaces: Python hates it when you use the
Tabkey on one line, and 4Spacebartaps 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 thefin front will literally print the text "Hello {name}". It must beprint(f"Hello {name}").
9. MCQs
How does Python define a block of code?
Which symbol is used to create a single-line comment in Python?
Which of the following is a valid Python variable name?
What is the standard naming convention for variables in Python?
How do you assign the value 100 to the variable score?
What is the output of print(f"Age is {20+5}")?
What does the input() function do?
If a user types 50 into an input() prompt, what data type does Python store it as by default?
Data and data different variables?) a) Yes b) No — Answer: a
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.