Introduction to Python
What Python is
Python is a programming language: a set of rules for writing instructions that a computer can carry out. You write plain text in a file, and a program called the Python interpreter reads that text and does what it says.
That is the whole idea. There is no magic in it. The hard part of programming is not the language — it is learning to break a problem into steps small enough that a machine can follow them. Python simply gets in your way less than most languages while you learn to do that.
Why Python is a reasonable first language
A few honest reasons, not marketing ones:
- The syntax is close to English.
if marks > 40: print("Pass")reads almost like a sentence. You spend your attention on the logic, not on remembering punctuation. - You don't have to declare types or manage memory. In C you must say
int marks;before usingmarks, and in C++ you may have to free memory yourself. Python handles both. Fewer things to get wrong early. - You get feedback quickly. Write three lines, run them, see the result. Short feedback loops are how people actually learn.
- It is used for real work. Data analysis, web backends, automation scripts, machine learning. What you learn here is not a toy that you throw away later.
Python also has real downsides, and you should know them. It is slower than C, C++ or Java for heavy computation. Its flexibility means some mistakes only show up when the program runs, not before. For a first language, those trade-offs are worth it.
Many Indian universities teach C in the first semester and Python in the second, or the other way round. The ideas transfer either way — variables, conditions, loops and functions exist in both. Learning one makes the next one much faster.
How your code actually runs
When you run a Python file, roughly this happens:
- Python reads your source file, top to bottom.
- It translates your code into bytecode, a compact internal form.
- The Python Virtual Machine executes that bytecode, one instruction at a time.
You do not need to think about steps 2 and 3 day to day, but one consequence matters a lot: Python runs your file line by line, in order. If line 5 has an error, lines 1 to 4 have already run before Python stops and complains. Programs are sequences, and order is meaningful.
You can run Python three common ways:
- Save code in a file such as
hello.pyand runpython hello.pyin a terminal. - Type code into the interactive shell (
pythonwith no filename) and get answers immediately. - Use a browser-based runner, like the Run button on the code blocks in this tutorial.
Start with the last one. Come back to files and terminals once the ideas are comfortable.
Your first program
print("Hello, world!")
print("My name is Ananya.")
print("I am learning Python.")
print is a function — a named piece of built-in behaviour. You call it by writing its name followed by round brackets, and you put inside those brackets whatever you want it to work with. Here that is text, wrapped in quotes.
Text in quotes is called a string. The quotes are not part of the text; they are how you tell Python "this is text, not code". Single quotes and double quotes both work, as long as you close with the same one you opened with.
print can also handle numbers and calculations:
print(7)
print(3 + 4)
print("3 + 4")
print("Total:", 12 * 5)
Look carefully at lines 2 and 3. 3 + 4 without quotes is arithmetic, so Python prints 7. "3 + 4" with quotes is text, so Python prints the characters exactly. This distinction between data and text describing data will come up constantly.
The last line shows another useful detail: give print several values separated by commas and it prints them all, with a single space between each.
Comments
A comment is a note for humans that Python ignores completely. Start it with #.
# Work out the total marks for three subjects
maths = 78
physics = 65
chemistry = 82
total = maths + physics + chemistry # add them up
print("Total marks:", total)
print("Average:", total / 3)
Write comments to explain why, not what. total = maths + physics + chemistry already says what it does; a comment saying "adds the marks" adds nothing. A comment saying "the university counts only the best three of five subjects" tells a reader something they could not work out from the code.
You will also use comments to temporarily switch off a line while testing. That is legitimate and everyone does it.
Common mistakes
Forgetting the quotes around text. print(Hello) makes Python look for something named Hello, and it fails with NameError: name 'Hello' is not defined. Text needs quotes: print("Hello").
Mismatched or missing brackets. print("Hello" gives SyntaxError, often reported on the following line, because Python keeps reading, waiting for the closing bracket. When a syntax error points at a line that looks fine, check the line above it.
Capitalising function names. Print("Hi") fails. Python is case-sensitive: print, Print and PRINT are three different names, and only the first exists.
Smart quotes pasted from a document. Copying code from WhatsApp, Word or a PDF often brings curly quotes (" ") instead of straight ones ("). Python rejects them. Retype the quotes in your editor.
Stray spaces at the start of a line. print("Hi") with a leading space gives IndentationError. Python treats indentation as meaningful — you will see why in the tutorial on if/else. For now, keep top-level lines flush against the left margin.
Practice
Try the exercises for this topic on the practice page — write a few print statements before moving on.