🚀 Join our community of 100,000+ learners worldwide! 📚 2000+ free tutorials available 🎓 Get certified today! 💡 Learn at your own pace 🌟 New courses added weekly 🤝 Connect with expert instructors
Python - Basic Syntax - Ngahtech Tutorials

Python - Basic Syntax

0 min read Python Fundamentals / Python Basics
Free Text

Introduction

Python syntax refers to the set of rules that define how Python programs are written and interpreted. Python is known for its simple and readable syntax, making it one of the easiest programming languages for beginners to learn.

Unlike many programming languages, Python uses indentation (spaces) instead of curly braces {} to define blocks of code.

1. Writing Your First Python Program

The traditional first program in Python is the Hello World program.

Example

print("Hello, World!")

Output

Hello, World!

The print() function displays text or values on the screen.

2. Python Statements

A statement is an instruction that Python can execute.

Example

print("Welcome to NgahTech")
name = "Gabriel"
age = 25

Each line represents a separate statement.

3. Python Indentation

Indentation refers to the spaces at the beginning of a line.

Python uses indentation to indicate a block of code.

Correct Example

age = 18

if age >= 18:
    print("You are an adult")

Output

You are an adult

Incorrect Example

age = 18

if age >= 18:
print("You are an adult")

Output

IndentationError: expected an indented block

4. Python Comments

Comments are notes added to code for explanation. Python ignores comments during execution.

Single-Line Comment

# This is a comment
print("NgahTech Group")

Output

NgahTech Group

Multi-Line Comment

"""
This is a multi-line comment.
It can span multiple lines.
"""

print("Python Tutorial")

Output

Python Tutorial

5. Python Variables

Variables store data values.

Example

name = "John"
age = 20

print(name)
print(age)

Output

John
20

6. Python Keywords

Keywords are reserved words that have special meanings in Python.

Examples:

if
else
for
while
def
class
return
True
False
None

Example

if True:
    print("Welcome")

Output

Welcome

7. Python Identifiers

Identifiers are names used for variables, functions, classes, and objects.

Valid Identifiers

student_name = "Peter"
age1 = 18
_total = 100

Invalid Identifiers

1name = "Peter"
student-name = "John"
class = "Python"

Rules for Naming Identifiers

  • Must begin with a letter or underscore (_)
  • Cannot begin with a number
  • Cannot contain spaces
  • Cannot use Python keywords
  • Case-sensitive

Example

name = "John"
Name = "Peter"

print(name)
print(Name)

Output

John
Peter

8. Multiple Statements on One Line

Python allows multiple statements on one line using a semicolon (;).

Example

name = "John"; age = 20; city = "Yaounde"

print(name)
print(age)
print(city)

Output

John
20
Yaounde

9. Multi-Line Statements

Long statements can be split across multiple lines using the backslash (\).

Example

total = 10 + 20 + 30 + \
        40 + 50

print(total)

Output

150

Python also allows line breaks inside brackets.

Example

numbers = [
    10,
    20,
    30,
    40
]

print(numbers)

Output

[10, 20, 30, 40]

10. Python Input and Output

Output Using print()

print("Welcome to NgahTech")

Output

Welcome to NgahTech

Input Using input()

name = input("Enter your name: ")

print("Hello", name)

Example Output

Enter your name: Gabriel
Hello Gabriel

11. Python is Case Sensitive

Python treats uppercase and lowercase letters differently.

Example

age = 20
Age = 30

print(age)
print(Age)

Output

20
30

The variables age and Age are different variables.

12. Using Quotes in Python

Python supports:

Single Quotes

name = 'NgahTech'

Double Quotes

name = "NgahTech"

Triple Quotes

message = """
Welcome to
NgahTech Group
Python Training
"""

print(message)

Output

Welcome to
NgahTech Group
Python Training

Practical Example

# Student Information Program

name = input("Enter your name: ")
age = int(input("Enter your age: "))

print("Student Name:", name)
print("Student Age:", age)

if age >= 18:
    print("Adult Student")
else:
    print("Minor Student")

Example Output

Enter your name: Gabriel
Enter your age: 25

Student Name: Gabriel
Student Age: 25
Adult Student

Summary

In this lesson, you learned:

  • What Python syntax is
  • How to write a Python program
  • Python statements
  • Indentation rules
  • Comments
  • Variables
  • Keywords
  • Identifiers
  • Multi-line statements
  • Input and output
  • Case sensitivity
  • Working with quotes

These concepts form the foundation of Python programming and are essential for every beginner learning Python at NgahTech Group.