Python Basic syntax

Python Basic Syntax

Jashan
October 29th, 2026
1196
05:00 Minutes

The basic syntax of the Python programming language is an essential stepping Stone for creating a cleanly structured, high-performance program. Anyone who has used the Python programming language a few times is likely to have a good understanding of just how easy it is to read and write code because the syntax of Python is extremely simple and well-organised.

From my experience writing and reviewing Python programs for a wide range of projects, both at a beginner level as well as at an intermediate level, I have experienced firsthand the benefits of learning these foundational principles that facilitate smooth coding.

This guide on Python basic syntax will help you understand the core principles or fundamental rules that underlie all Python programs in a straightforward, hands-on manner suitable for a novice user. Let’s dive in!

Learn Python Online with Expert-Led Training.

Build Python programming skills through live instructor-led training, practical exercises, and hands-on coding.

What is Python Syntax?

Python syntax refers to the set of rules that defines how you can write and organize your Python code so that the Python interpreter can understand and run it correctly. It is known for its clean, simple and readable structure, which is sometimes described as "executable pseudocode”.

Basic Python Syntax Explained

Understanding basic Python syntax helps you learn how to write instructions that Python can execute. Python code uses variables to store data, functions to perform tasks, operators to perform calculations, and indentation to organize code blocks. Unlike many other programming languages, Python does not require a semicolon at the end of every statement. This makes the code cleaner and easier to read.

Let's understand the basic elements of Python syntax with a simple example:

# Store information in variables
name = "Alice"
age = 25

# Display the information
print("Name:", name)
print("Age:", age)

# Check a condition
if age >= 18:
    print("You are an adult.")

Explanation of the code:

  • Comments: The # symbol introduces a comment that Python ignores during execution.
  • Variables: The name and age variables store a string and an integer, respectively.
  • Assignment operator (=): It assigns values to variables.
  • print() function: It displays information on the output screen.
  • Conditional statement (if): It checks whether the age is greater than or equal to 18.
  • Colon (:): It marks the beginning of the indented code block following the if statement.
  • Indentation: The spaces before print("You are an adult.") indicate that the statement belongs to the if block.

Now let's understand essential elements of the basic Python syntax in detail.

Indentation in Python

Indentation in Python refers to the spaces or tabs that are used at the beginning of a line of code to define a block of code. Many other programming languages use curly braces; Python uses indentation to show structure and hierarchy.

Example: Login Check System

username = "admin"
password = "1234"

input_user = input("Enter username: ")
input_pass = input("Enter password: ")

if input_user == username:
    if input_pass == password:
        print("Login successful!")
    else:
        print("Incorrect password")
else:
    print("Username not found")


How indentation works here:

  • The first if checks the username
  • Inside it, another if (indented) checks the password
  • Each level of indentation shows a nested condition

Why is indentation important in Python?

It is very essential due to the following reasons:

  • It tells Python which lines belong together
  • It defines blocks like:
  • loops (for, while)
  • conditions (if, else)
  • functions (def)
  • Incorrect indentation leads to errors

The print() Function in Python

The print() function is one of the most basic and commonly used functions in Python. It is used to display output on the screen, making it an essential tool for beginners as well as experienced developers. Whether you want to show a message, display the value of a variable, or check the result of a calculation, print() helps you do it easily.

In simple words, it takes all the information that you have provided, and then it prints it in the console/output screen.

Basic Syntax:

print("your message")

For example:

name = "Justin"
age = 22
print("My name is", name, "and I am", age, "years old.")


Key Points to Remember:

  • Use quotes (" " or ' ') for text.
  • You can print multiple items using commas.
  • print() can display text, numbers, variables and expressions.
  • It is very useful for debugging and understanding your code.

Start Learning Python for Data Science with Expert-Led Training.

Learn Python for data analysis, data visualization, and machine learning through live instructor-led training and practical exercises.

Comments in Python

Comments in Python are notes written inside the code that the computer ignores. They are only there to help humans understand what the code is doing. You can think of comments like side notes or explanations for your code.

  • Single-line comments: Use the # symbol to write a comment on a single line. For example:
# This is a comment
name = "Alice"  # This explains the variable

In this, Python ignores the comments and only runs the code.

  • Multi-line comments: Python doesn’t have a special rule for multi-line comments, but we can sometimes use the triple quotes (''' or """) to write them. For example:
'''
This is a comment
written in multiple lines
'''
print("Hello!")

Technically, this is a string and not a real comment. But since it is not stored or used, it works like a comment.

Variables and Data Types in Python

Variables in Python are used to store data that can be used later in a program. You can think of a variable as a container that holds a value such as a number, text or other data type.

How to Create a Variable

In Python, you can create variables by assigning a value to a name using the = operator.

For example:

name = "Alice"
age = 25

In this example:

  • name stores the value "Alice"
  • age stores the value 25

Rules for Naming Variables

When you are naming variables in Python, you must follow these rules:

  • Variable names can contain letters, numbers, and underscores
  • Variable names must not start with a number
  • Python is case-sensitive (uppercase and lowercase are different)
  • Variable names should not be Python keywords (reserved words)

Identifiers and Naming Rules

Identifiers are the names we give to variables, functions and other objects. They usually follow specific rules to ensure that our code is valid and easily understood.

Rules for naming identifiers

  • An identifier can contain letters (a to z and A to Z), numbers (0 to 9) and underscores (_)
  • It must start with a letter or an underscore
  • It cannot start with a number
  • It cannot have spaces
  • It cannot use special symbols like @, #, $, %, etc.
  • It cannot be a Python keyword (like if, else, for, etc.)
  • Python is case sensitive (name and Name are different)

Python Keywords

In Python, there are certain words that are reserved for specific programming functions and cannot be used as identifiers. These keywords are part of the Python syntax and are reserved for specific programming purposes only.

List of Python keywords

Here is a list of common Python keywords:

False await else import
None break except is
True class finally in
and continue for lambda
as def from nonlocal
assert del global not
async elif if or

Python Operators

Operators in Python are sort of special symbols that are used to perform operations on variables and values. Python supports a wide range of operators based on their functionality.

Types of Python operators

The following are the types of Python operators:

Operator Type Description Operators
Arithmetic Used to perform basic math operations. +, -, *, /, %, //, **
Assignment Used to assign values to variables. =, +=, -=, *=, /=, %=
Comparison Used to compare two values (gives True/False). ==, !=, >, <, >=, <=
Logical Used to combine conditions. and, or, not
Bitwise Used to perform operations on binary numbers. &, `
Membership Used to check if a value exists in a sequence. in, not in
Identity Used to check if two variables refer to the same object. is, is not

Learn Python for AI with Expert-Led Training.

Learn Python programming for artificial intelligence through live instructor-led training, practical exercises, and real-world applications.

Simple Python Program Example

A basic Python program usually includes instructions like printing output, taking input, and performing simple calculations. The following are some examples that will help you understand how Python programs work:

Example 1: Print a Message

This will display a message on the screen.

print("Hello, World!")


Example 2: Add Two Numbers

a = 5
b = 3

sum = a + b
print(sum)

What it does:

  • Stores numbers in variables.
  • Adds them and prints the result.


Example 3: Even or Odd Check

This example will check whether the number is even or odd using a condition.

num = int(input("Enter a number: "))

if num % 2 == 0:
    print("Even number")
else:
    print("Odd number")


Type Conversion in Python

Type Conversion means changing one data type into another in Python. Python has two types of type conversion:

1. Implicit Type Conversion (Automatic)

There are some situations where Python automatically converts one data type to another. This is known as implicit type conversion.

For example:

x = 5      # int
y = 2.5    # float

result = x + y
print(result)        # 7.5
print(type(result))  # float


Explanation:

  • Python converts 5 (int) into 5.0 (float)
  • Then performs addition

2. Explicit Type Conversion (Type Casting)

In this, users convert the data type of an object to required data type.

For example:

x = "10"   # string
y = int(x)

print(y)        # 10
print(type(y))  # int


Explanation: String "10" is converted into the integer 10

Taking Input from Users in Python

In Python, we can collect data from users with the help of the built-in input() function. By default, this function accepts the entered value as a string, which can later be changed into other data types if required.

Example:

city = input("Enter your city: ")
print("You live in " + city)


Input with Type Conversion

Since the input() function always returns text (string), we often convert it into the needed type, like integer or float, using functions such as int() or float().

Example:

num = input("Enter a number: ")
num = float(num)
print("Double of the number is:", num * 2)


Common Mistakes Beginners Make in Python Syntax

When beginners start learning Python, they often overlook basic syntax rules, leading to common errors. In this section, I will explain some mistakes, explain why they happen and help you avoid them to write cleaner Python code.

1. Incorrect Indentation: Beginners often forget that Python uses indentation to define code blocks, so missing or inconsistent spacing can lead to errors.

2. Missing Colons: Many beginners forget to add a colon at the end of statements like if, for, or def, which causes syntax errors.

3. Case Sensitivity Issues: Python is case-sensitive, so writing Print() instead of print() or changing variable cases can break the code

4. Forgetting to Convert Input Types: Python is case-sensitive, so writing Print() instead of print() or changing variable cases can break the code

Wrapping Up

In this guide, I have explained Python basic syntax, which has covered topics like indentation, variables, data types, operators and user input. These topics help you understand how Python code is structured and executed. By learning these fundamentals, you can write clear, readable, and error-free programs. You can also build a strong foundation that makes it easier to understand and work with more advanced Python concepts.

FAQs

1. Why can’t I use Python keywords to name variables?

You cannot use Python keywords as variable names because they are reserved words with predefined meanings in the language (like if, for, while).

2. Is Python easier than Java?

Yes, Python is considered easier to learn and use than Java

3. Is 101 illegal in Python?

Yes, 101 is an illegal variable name as Python variable names cannot start with a digit, as they must begin with a letter.

Want to learn more? Keep reading:
Ready to go from reading to doing?
About the Author
Jashan | igmGuru
About the Author

Jashan is a software developer with experience writing production code in multiple programming languages, with a focus on Python and R for data-intensive applications. Jashan's expertise includes automation scripting, data pipelines, debugging, and maintainable code practices. Apart from the developer part, he also writes well researched content to engage with learners.

Drop Us a Query
Fields marked * are mandatory
×

Your Shopping Cart


Your shopping cart is empty.