Labs ICT
โญ Pro Login

Basic Syntax

3 min read | Python Tutorial
โญ

Want the full learning experience?

Get structured courses, certificates, projects, and instructor support with LabsICT Pro.

Explore Pro Courses

Click Run to execute any code example on this page. You can also edit the code before running.

No Semicolons, No Ceremony

Unlike many languages, Python does not need semicolons at the end of each line. It also does not require a main() function to run. Python scripts execute top-to-bottom, and you can start writing useful code immediately.

print("Hello")
print("World")

Indentation Is Law

In most languages, curly braces { } define blocks of code. In Python, indentation does the job. This is not optional โ€” it is part of the language. If you indent wrong, your code breaks.

Most Python developers use 4 spaces per indentation level. Do not mix tabs and spaces.

if 5 > 2:
    print("Five is greater than two")
    print("This is inside the if block")
print("This is outside")

Variables Without Type Declarations

You do not need to declare variable types in Python. Just assign a value and Python figures out the type automatically.

name = "Alice"
age = 25
height = 5.6
is_student = True

You can check a variable's type using the type() function:

print(type(name))     # <class 'str'>
print(type(age))      # <class 'int'>
print(type(height))   # <class 'float'>
print(type(is_student)) # <class 'bool'>

Python Data Types

Python has several built-in data types you will use constantly:

Type Description Example
str Text "hello", 'world'
int Whole numbers 42, -7
float Decimal numbers 3.14, -0.5
bool True or False True, False
list Ordered collection [1, 2, 3]
dict Key-value pairs {"name": "Alice"}

String Formatting

Python has a powerful way to embed variables in strings called f-strings. Put an f before the quote and use curly braces {} to insert variables.

name = "Alice"
age = 25

# f-string (recommended)
print(f"My name is {name} and I am {age}")

# Older methods still work
print("My name is {} and I am {}".format(name, age))
print("My name is %s and I am %d" % (name, age))

Getting User Input

Use the input() function to get text from the user. It always returns a string, so you need to convert it for numbers.

name = input("What is your name? ")
print(f"Hello, {name}!")

age = int(input("How old are you? "))
print(f"In 10 years you will be {age + 10}")

Comments

Use the # symbol to write comments. Comments are ignored by Python and are only for humans reading the code.

# This is a comment
print("Comments are useful")

# You can use comments to explain tricky code
result = (42 * 7) + 3  # Why 42? It's the answer to everything

Multiple Assignments

Python lets you assign multiple variables in one line.

# Assign multiple values
x, y, z = 1, 2, 3
print(x, y, z)  # 1 2 3

# Same value to multiple variables
a = b = c = 0
print(a, b, c)  # 0 0 0

# Swap values without a temporary variable
x, y = 10, 20
x, y = y, x
print(x, y)  # 20 10

๐Ÿงช Quick Quiz

What does Python use to define code blocks?