🐍 Python Tutorial: Basics & Syntax
Now that your environment is set up, it's time to begin writing actual Python code. This page introduces Python's basic syntax and common elements like variables, printing to the screen, comments, and indentation.
1. Hello, World!
Let's start with the most famous first program — printing "Hello, World!" to the screen. This simple example introduces how to display output in Python using the print() function.
print("Hello, World!")When you run this code, it will show:
Hello, World!
2. Variables and Data Types
A variable is like a container that stores a value. In Python, you don't need to declare the type of a variable — Python figures it out for you automatically.
name = "Alice" # A string (text)
age = 30 # An integer (whole number)
height = 5.7 # A float (decimal number)
is_student = True # A boolean (True or False)Python is dynamically typed, which means you can assign any type of value to a variable without specifying it explicitly. You can also change the type later:
x = 10 # x is an integer
x = "hello" # now x is a string3. Indentation
Python uses **indentation** (spaces or tabs at the beginning of a line) to define blocks of code. This is different from many other languages that use brackets ({}) for code blocks.
For example, in the next lesson, you'll use indentation to define what happens in an if statement or loop. Here’s what it looks like:
# (Don't worry about 'if' yet — this is just to show indentation)
if True:
print("Indented correctly")
print("This is outside the block")4. Comments
Comments are lines of text that are ignored by Python. They’re used to explain what your code does and help make it easier to understand.
# This is a comment
# The next line prints a greeting
print("Hi there!")Comments begin with the # symbol. Python will ignore anything on the line after the #.
5. Input & Output
In addition to printing output, Python can also take input from the user using the input() function. This is useful when you want your program to interact with people.
name = input("What is your name? ")
print("Nice to meet you, " + name + "!")When run, the program will pause and wait for the user to type something. After they press enter, the input is stored in the name variable and used in the greeting.