Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
In the previous lessons, we prepared our Python environment.
Now we are ready to understand how Python code is written.
Just like a language has grammar and rules, Python also has rules for writing code.
These rules are called syntax.
In this lesson, we will learn:
: is importantDon't worry if these terms are new.
We will understand them with simple examples.
Syntax means the rules for writing code correctly.
Think about a normal sentence.
For example:
This sentence follows the basic rules of a language.
But if we write:
the words are still there, but the sentence structure is confusing.
Programming languages work in a similar way.
Python expects code to follow its syntax rules.
For example:
is valid Python.
But:
is incomplete because the closing ) is missing.
Python will report an error.
A statement is an instruction written in Python.
For example:
This is one Python statement.
Another example:
This is another statement.
So:
contains two statements.
You can think of a statement as:
Sometimes one instruction is not enough.
We may want several instructions to belong together.
For example:
Here, these two lines:
belong to the if statement.
Together, they form a code block.
Python uses indentation to understand this relationship.
Indentation means adding spaces at the beginning of a line.
Look at this:
The two print() statements are moved slightly to the right.
That space is called indentation.
Python uses indentation to understand which lines belong to a block.
When Python starts a new block, the lines inside that block must be indented consistently.
For example:
Both statements belong to the if block.
But:
is incorrect because the print() statement is not indented.
Python will produce an indentation-related error.
:You will often see a colon at the end of Python statements that begin a new block.
For example:
Notice the colon:
The colon tells Python:
You will see this pattern with:
For example:
Later, you will learn each of these concepts in detail.
For now, remember:
Python's recommended style is:
4 spaces per indentation level.
For example:
Here, the print() statement is indented by one level.
If there is another block inside it:
Now we have two indentation levels.
Visually:
You don't need to manually count spaces every time.
VS Code can automatically help you with indentation.
Look at this example:
The indentation of the second print() does not match the first one.
Python may report an indentation error.
A better version is:
A block can contain another block.
This is called nesting.
For example:
Look carefully at the indentation:
There are two levels:
This is one reason indentation is so important in Python.
Python treats uppercase and lowercase letters as different.
For example:
These are two different variable names.
Python sees:
as different names.
The same applies to Python keywords.
For example:
is correct.
But:
is not the same thing.
Similarly:
is correct, while:
is not the Python boolean value True.
Try this:
Output:
Python treats student and Student as different names.
Although Python allows names such as:
it is better to use clear and consistent naming.
For example:
This is easier to understand.
Usually, we write one Python statement on one line.
For example:
This makes the code easier to read.
You may sometimes see multiple statements on one line using semicolons:
Python allows this in some situations, but beginners should generally avoid it.
Prefer:
Sometimes an expression can become very long.
For example:
A long expression can be split across multiple lines.
The recommended approach is to use parentheses.
Python understands that the expression is continuing because it is inside parentheses.
Parentheses make long expressions easier to read.
For example:
This is easier to read than putting everything on one very long line.
\.\?Python also allows explicit line continuation using \.
For example:
This can work, but it is usually better to use parentheses when possible.
Prefer:
This is generally easier to read and maintain.
Here are some important rules to remember.
| Rule | Example |
|---|---|
| Use indentation for code blocks | if age >= 18: followed by indented code |
Use : before a new block | if, for, while, def, etc. |
| Use consistent indentation | Usually 4 spaces |
| Python is case-sensitive | name ≠ Name |
| Keep code readable | Prefer one statement per line |
| Use parentheses for long expressions | total = ( ... ) |
| Save your code before running it | Ctrl + S |
Incorrect:
Correct:
Incorrect:
Correct:
Incorrect:
Correct:
Incorrect:
Correct:
True and truePython uses:
not:
Remember that Python is case-sensitive.
Whenever you see code like this:
think:
This pattern will appear again and again throughout your Python journey.
In this lesson, you learned:
: is used before many new code blocks.name and Name are different.Remember this:
The three important parts are:
Once you understand this pattern, many later Python concepts will become easier.
name and Name the same variable in Python?A.
B.
C.
D.
Create a file named:
Now type the following program:
Save the file and run:
You don't need to understand every line yet.
The important things to observe are:
Python treats them as different names.
The print() statement belongs to the if block because it is indented.
The second if is inside the first if.
Parentheses allow the expression to continue across multiple lines.
In the next lesson, we will continue your Python learning journey with Comments & Best Practices (1: Introduction and Setup).
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Python Setup | Comments & Best Practices |
Test your understanding of this lesson with 6 questions. Each question has one correct answer.