Skip to content
C

Python Basics

Complete learning notes


1. Introduction

Every AI/ML journey starts with a programming language, and Python is the language almost every data scientist and machine learning engineer uses. Before we can build models, clean data, or train neural networks, we need to be comfortable writing and running simple Python code. This lesson builds that foundation from zero.

Think of this topic as learning the alphabet before writing sentences. You won't build anything "AI-related" yet, but every single ML topic later in this course depends on you being fluent in what's covered here.


2. What is Python Basics?

Simple definition: Python Basics means understanding how to write, run, and read simple Python programs — things like printing output, writing comments, following indentation rules, taking input, and using basic operators.

Technical explanation: Python is a high-level, interpreted, general-purpose programming language. "High-level" means it reads closer to plain English than to machine code. "Interpreted" means your code is executed line-by-line by a program called the Python interpreter, rather than being compiled into a separate executable file before running.


3. Why is it Important?

In simple words: you cannot process data, build models, or use ML libraries like NumPy, Pandas, or Scikit-learn without knowing basic Python syntax first.

  • Almost all popular ML libraries (Scikit-learn, TensorFlow, PyTorch, Pandas) are Python-based.
  • Python's simple syntax lets beginners focus on learning ML concepts instead of fighting complicated language rules.
  • Data cleaning, automation, and experimentation for ML all happen through short Python scripts.

4. Prerequisites

None. This is the true starting point of the course. You only need a computer and a way to run Python (installed locally, or an online notebook environment).


5. Core Concepts

Break Python Basics into these smaller building blocks:

  1. Running Python code
  2. The print() function
  3. Comments
  4. Indentation
  5. Taking input from the user
  6. Basic arithmetic operators
  7. Basic string operations

6. Detailed Explanation

a) Running Python Code

Python code can be run in two common ways:

  • Writing code in a .py file and running it from a terminal.
  • Writing code in a notebook (like Jupyter) cell-by-cell.

Both approaches use the exact same Python language — only the environment differs.

b) The `print()` Function

print() is how a Python program communicates with the outside world. Whatever you place inside the parentheses gets displayed on the screen.

c) Comments

A comment is a line in your code that Python completely ignores when running. Comments exist purely for humans to read — to explain what the code does.

In simple words: think of a comment as a sticky note you leave for yourself or teammates.

d) Indentation

Unlike many other programming languages that use curly braces {} to group code, Python uses indentation (spaces) to define blocks of code. This means spacing is not optional style — it is a grammar rule in Python.

e) Taking Input

The input() function lets a running Python program pause and wait for the user to type something.

f) Basic Arithmetic Operators

Python supports the usual mathematical operators: addition, subtraction, multiplication, division, and a few Python-specific ones like floor division and modulus (remainder).

g) Basic String Operations

Text in Python is called a "string." You can join (concatenate) strings, repeat them, and combine them with other data using print().


7. How It Works

Step-by-step, when you run a Python file:

  1. The Python interpreter reads your code from top to bottom.
  2. It executes each line in order (skipping comments).
  3. When it hits print(), it displays the given content immediately.
  4. When it hits input(), execution pauses until the user types something and presses Enter.
  5. The program ends when there are no more lines to execute.

8. Real-World Example

Imagine a shop billing counter. The cashier (the interpreter) reads each item on your bill one at a time, in order, and processes it — they don't jump around randomly. Python code execution works the same way: line by line, top to bottom.


9. Technical Example

A Python statement like:

python
print(5 + 3)

is processed as: Python first evaluates the expression 5 + 3 (which becomes 8), and then passes that result to print(), which displays it.


10. Python Example

python
# This is a comment - Python ignores this line # Printing simple text print("Welcome to AI/ML learning!") # Printing the result of a calculation print(5 + 3) # Taking input from the user student_name = input("Enter your name: ") # Combining text and a variable in one print statement print("Hello,", student_name, "- let's start learning Python!") # Basic arithmetic operators print(10 + 4) # Addition print(10 - 4) # Subtraction print(10 * 4) # Multiplication print(10 / 4) # Division (gives decimal result) print(10 // 4) # Floor division (removes decimal part) print(10 % 4) # Modulus (remainder after division)

Expected Output (assuming the user types Riya when asked for their name):

text
Welcome to AI/ML learning! 8 Enter your name: Riya Hello, Riya - let's start learning Python! 14 6 40 2.5 2 2

11. Code Explanation

  • print("Welcome to AI/ML learning!") displays the text exactly as written, because it is wrapped in quotes (a string).
  • print(5 + 3) first calculates 5 + 3, then prints the number 8 — no quotes means it's treated as a math expression, not text.
  • input("Enter your name: ") shows the message, waits for the user, and stores whatever they type into student_name.
  • print("Hello,", student_name, ...) shows how multiple items separated by commas inside print() are displayed together, separated by spaces automatically.
  • 10 / 4 returns 2.5 because normal division always gives a decimal answer in Python.
  • 10 // 4 returns 2 because floor division drops anything after the decimal point.
  • 10 % 4 returns 2 because that is the remainder left over after dividing 10 by 4 (4 × 2 = 8, remainder 2).

12. Advantages

  • Simple, readable syntax close to plain English.
  • Huge ecosystem of AI/ML libraries built specifically for Python.
  • Large, active community — easy to find help and resources.
  • Works the same way across Windows, macOS, and Linux.

13. Limitations

  • Slower execution speed compared to lower-level languages like C++ (though this rarely matters for learning or most ML workflows, since core ML libraries are optimized internally).
  • Indentation-based structure can cause errors if spacing is inconsistent.

14. Common Mistakes

  • Mixing tabs and spaces for indentation, which can cause errors.
  • Forgetting quotes around text, causing Python to treat it as code instead of a string.
  • Assuming input() returns a number — it always returns text (a string) by default, even if the user types digits.
  • Forgetting parentheses when calling print or input.

15. Best Practices

  • Use consistent indentation (commonly 4 spaces) throughout your code.
  • Write comments to explain why something is done, not just what is done.
  • Use clear, descriptive names when you eventually start using variables (covered in the next topic).
  • Test small pieces of code frequently instead of writing large blocks before running anything.

16. Real-World Applications

  • Writing small scripts to automate repetitive tasks (renaming files, processing text).
  • Building the very first line of any data science or ML pipeline — reading and displaying data.
  • Creating simple command-line tools that request and respond to user input.

17. Interview-Oriented Points

  • Be ready to explain the difference between an interpreted and a compiled language.
  • Know that Python is dynamically typed — you don't need to declare a variable's type in advance.
  • Understand why indentation matters in Python, unlike in languages such as Java or C++.
  • Be able to explain the difference between / and //.

18. Exam-Oriented Points

  • Python is a high-level, interpreted, general-purpose language.
  • print() displays output; input() collects user input as a string.
  • Comments start with # and are ignored during execution.
  • Indentation is mandatory and defines code blocks in Python.

19. Comparison Table — Compiled vs Interpreted Languages

AspectCompiled Language (e.g., C++)Interpreted Language (e.g., Python)
ExecutionConverted fully to machine code before runningExecuted line-by-line while running
SpeedGenerally fasterGenerally slower
Error DetectionErrors caught before running (at compile time)Errors caught while running (at run time)
Ease for BeginnersHarder to set up and debugEasier to write and test quickly

20. Quick Revision

  • Python is a high-level, interpreted, beginner-friendly language widely used in AI/ML.
  • print() displays output; comments (#) are notes for humans, ignored by Python.
  • Python uses indentation (not curly braces) to define code blocks.
  • input() always returns text, even if the user types numbers.
  • Key arithmetic operators: +, -, *, / (division), // (floor division), % (remainder).

Mock Test

  • Python Basics — Quick Test

    A 10-question multiple-choice check on Python Basics.

    10 questions · 10 min · Easy
    Start Mock Test

Coding Problems