Last updated on September 3rd, 2025 at 10:43 am

Part of mastering Python is understanding Python syntax (the grammar of the language). Because Python is so close to the spoken language, this is pretty easy to learn.
In lesson 1, we learned to write a simple print statement in Python. As with most languages, the first program that all beginners learn to write is called “Hello World”.
To recap, we write this code using the print function. This function is used to display the text or output to the computer screen. Here is an example of the text being written:
print("Hello, world!")
This program displays “Hello, world” on the computer screen.
Comments are another set of instructions used by coders. This helps humans to understand what a specific part of the code is doing, for example:
#program greeting
#this is a greeting
print("hello, world!")
The first lines with the hashtags are called comments.
As shown above, the comment was used to describe what the code will output when executed.
Basic Python Syntax & Rules
There are many syntax and rules associated with programming; however, as a beginner, these are the basic ones that you would need to pay attention to when writing a Python program:
Case sensitivity
Python is case-sensitive, meaning it distinguishes between upper and lowercase letters. Elements such as myVariable, MyVariable, and MYVARIABLE will be treated as three distinct identifiers, each holding its potential value or referring to a completely different entity.
Here are two examples in code form:
Example 1
Name1 = "Luis"
name1 = "Manny"
print (name2)
print (Name1)
In this example, the value contained in name1 prints first, followed by the one contained in Name1. So name1 prints Manny while Name1 prints Luis in that order.
Now look at example 2.
Example 2
name1 = "Luis"
name1 = "Manny"
print (name1)
print (name1)
Here, because the variable’s name is exactly the same, Luis is stored first. It is then overwritten by Manny. So, when name1 is printed both times, the printout is Manny.
As you can see by these examples, the capital letter and the common letter were used as distinct identifiers when it comes to the computer reading the command.
Since example 1 had the first print with n being common – print (name1), the computer automatically knows to look for the name associated with that identifier, so Manny was printed first.
Single-line Python comment
We use a hashtag to write a single-line Python comment. An example of this would be:
# Program greeting
This single-line Python comment indicates to the programmer reading your code that the program is named greeting.
Multi-line Python comment
Multiline Python comments could be executed by using triple-quoted commas or triple quotes. An example of this would be:
"""
Program greeting
Description: This program allows a user to enter two numbers. It compares the numbers and print the bigger number.
"""
Indentation in Python programming
This refers to the whitespace or tab at the beginning of a line of code. In Python, unlike many other languages, indentation is part of the Python syntax. Let’s look at the code below:
"""
Program greeting
Description: This program stores two numbers 5 and 4. It compares the numbers and print the bigger number.
"""
num1 = 5
num2 = 4
if num1>num2:
print(num1, " is greater than ", num2,".")
In this example, the indentation indicates to the Python compiler that the second line is only executed if the first line is true.
Python syntax for data types
Several data types are built into Python, each of which represents different values. Here are some examples:
Floating point variables (float): represent a real number, which is a number with decimal points. E.g.
z = 1.43 # float
String variable: Sequences of characters, enclosed in double or single line quotes, are represented by str (string):
name = "Ali"
Integers are positive and negative numbers. They do not include fractions or decimals. Some examples of integers are: -2, -1, 0, 1, 2, 3. In Python syntax, integers are represented by int(integer):
number3 = 15
Naming conventions in programming
While naming conventions are not a part of the Python syntax, and will not be flagged by the Python syntax checker, they are a good practice that makes your program uniform, easier to read, and write.
In Mathematics, variables are typically represented by letters. We may see x = 5 or y = 12. However, in programming, we prefer to name variables using relevant variable names.
For instance, if we are creating a program with a variable that will hold a student’s age, we may choose to name the variable ‘age’ rather than ‘x’. Likewise, if your program has a variable that stores student grades, you may have a variable called ‘grades’.
As a new programmer, you will find this practice useful since it makes your program easier to follow, especially if your program is very long and has many variables.
Now, aside from using relevant variable names, below are some naming styles that programmers use:
Personalized naming conventions used by programmers
Here are four typical naming conventions in programming:
- Camel case: Camel case is a naming convention that pushes together two words where the first letter in the first word is a lower-case character and the first letter in the second word is upper-case. Here are a few examples: firstName, lastName, and studentAge. This use of the uppercase character in the second word makes the variable name easily readable.
- Snake case: Snake case is a naming convention that uses lowercase characters with an underscore indicating where the second word begins. Here are a few examples of snake case in use: first_name, last_name, and student_name.
- Pascal case: Pascal case is a naming convention similar to camel case, but in this instance, the words are pushed together and each word begins with an upper-case letter. Here is an example: FirstName, LastName, and StudentAge.
- Kabeb case: This naming convention is similar to the snake case but uses a hyphen rather than an underscore. Here are a few examples of the kabeb case: first-name, last-name, and student-age.
Now that you are aware of these naming conventions in programming, be sure to choose one and stick with it. You will find coding easier, your programs will be easier to read, and the code will be uniform.
In the next article (Lesson 3), we will be learning about operators in Python, printing output, and taking input.
Before you go
In order to learn Python syntax and become an expert programmer, regular practice is important. The following two exercises will help you to practice what we learned in this lesson.
If you do not have a compiler on your computer, you may use this online compiler.
Exercise 1
Write a program that takes the ages of two students. It compares their ages and prints the higher age.
Exercise 2
The following program has several errors. Copy and paste the program into the online compiler, then edit it until you have 0 errors.
# this program checks to see which number is greater
The information will be printed to screen.
if 5>4:
print(5 is greater than 4)
In the next article (Lesson 3), we will be learning about operators in Python, printing output, and taking input.