Python Tutorial

Writing Your First Python Program and Basic Python Syntax

TGTeam GyaniPanditNovember 23, 20264 min read

Once Python is installed, the next step is to write and run your first program. In this lesson you will run Python code in three ways: in the interactive shell (terminal), in IDLE, and in Visual Studio Code. You will also learn the basic syntax rules every Python program follows and how to read the errors beginners usually see.

Your First Python Program

The traditional first program simply prints a message:

print("Hello from GyaniPandit")

Output:

Hello from GyaniPandit

print() is a built-in function that displays whatever you put inside the brackets. The text inside the quotes is called a string.

Way 1: The Interactive Shell (Terminal)

  1. Open Command Prompt or PowerShell on Windows, or Terminal on macOS/Linux.
  2. Type python and press Enter. On Windows you can also type py; on macOS and Linux you may need python3.
  3. You will see the Python version and the prompt >>>, which means Python is waiting for your code.
  4. Type print("Hello from GyaniPandit") and press Enter.
C:UsersStudent> python
Python 3.x.x (details vary)
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello from GyaniPandit")
Hello from GyaniPandit
>>> 2 + 3
5
>>> exit()

The shell runs each line as soon as you press Enter, which makes it perfect for quick experiments. Type exit() to leave it.

Way 2: IDLE

IDLE (Integrated Development and Learning Environment) is installed along with Python on Windows and macOS. It is written in Python itself.

  1. Search for IDLE in the Start menu and open it. The Python shell appears.
  2. Go to File > New File. An empty editor window opens.
  3. Type your program and save it with Ctrl + S, for example as first.py.
  4. Press F5 or choose Run > Run Module. The output appears in the shell window.
Running the first Python program in IDLE

Way 3: Visual Studio Code

For bigger programs, a code editor like VS Code is more convenient. This course uses VS Code from here on.

  1. Create a folder for your Python files, for example python-course.
  2. In VS Code, choose File > Open Folder and open that folder.
  3. Create a new file named basic.py. The .py extension tells everyone it is a Python file.
  4. Write print("Hello from GyaniPandit") and save.
  5. Open the built-in terminal with Terminal > New Terminal and type python basic.py (or py basic.py on Windows).
Running a Python file from the terminal in VS Code

If you install Microsoft’s Python extension for VS Code, you can also run the file with the ▶ button at the top right.

Basic Python Syntax Rules

1. One statement per line, no semicolons needed

name = "Riya"
age = 15
print(name, age)

Output:

Riya 15

2. Indentation is part of the syntax

Python uses indentation (spaces at the start of a line) to group code into blocks. The usual style is 4 spaces.

age = 15
if age >= 13:
    print("Teenager")
print("Done")

Output:

Teenager
Done

3. Python is case-sensitive

print works, but Print gives an error. Likewise, marks and Marks are two different variables.

4. Comments start with #

# This line is ignored by Python
print("Comments help humans understand code")  # comment at the end of a line

5. Strings can use single or double quotes

print('Hello')
print("It's easy")

6. Variables need no type declaration

Python works out the type from the value: x = 10 is an integer, x = "ten" is a string.

7. print() has useful options

print("a", "b", sep="-")
print("x", end="")
print("y")

Output:

a-b
xy

Common Beginner Errors

Error Typical cause
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)? Writing print "hi" (old Python 2 style). Use print("hi").
SyntaxError: unterminated string literal A missing closing quote.
IndentationError Missing or uneven spaces inside an if, loop or function.
NameError: name 'Print' is not defined Wrong capitalisation or a misspelt name.
'python' is not recognized... (Windows) Python is not on the PATH. Try py, or reinstall with “Add python.exe to PATH” ticked.

Frequently Asked Questions

What is the extension of a Python file?

.py, for example basic.py.

Do I need a semicolon at the end of a line in Python?

No. A new line ends a statement. Semicolons are allowed but almost never used.

Which is better for beginners: IDLE or VS Code?

IDLE is simple and comes with Python, so it is good for the first few days. VS Code is better once you work with several files and want features like auto-complete and debugging.

Next: Python program to find the first and last digit of a number

Turn reading into skillsCertificate courses in AI, Data Science and programming.
Explore Courses