Skip to main content

Online Python Compiler & Visualizer

main.py
Ready

How to take input in Python

input() reads one line of text from the Input tab and always returns a string. Convert it when you need a number:

age = int(input())              # one whole number
price = float(input())          # one decimal number
a, b = map(int, input().split())  # two numbers on one line
nums = list(map(int, input().split()))  # a whole list on one line

Put each value on its own line when your program calls input() several times, or all on one line when it uses split().

How to use the Python visualizer

The visualizer runs your program one step at a time, so you can see exactly what Python does on each line. It helps most with loops, recursion and code that doesn't do what you expected.

  1. Write your code, put any input in the Input tab, then press Visualise. The first time, Python downloads into your browser (about 5 MB); after that it starts in a moment.
  2. Use the arrows or the slider to move forward and back. The line about to run is highlighted in the editor.
  3. Read the call stack: each function call gets its own frame with its variables, and lists, tuples and dictionaries are drawn as boxes. A variable that just changed is shown in pink.
  4. Watch Output so far to see when each line is printed.

The visualizer runs Python in your browser, so it doesn't use up your runs. It shows up to 1,000 steps and stops a program that loops forever.

Python programs to try

Copy one into the editor above, paste the input into the Input tab and press Run.

Read two numbers and add them

a = int(input())
b = int(input())
print(a + b)

Input: 7 ⏎ 5

Read a list of numbers on one line

nums = list(map(int, input().split()))
print(nums)
print("Count:", len(nums))

Input: 4 8 15 16 23 42

Factorial with a loop

n = int(input())
result = 1
for i in range(2, n + 1):
    result *= i
print(result)

Input: 6

Reverse a string and check for a palindrome

word = input().strip()
print(word[::-1])
print("Palindrome" if word == word[::-1] else "Not a palindrome")

Input: level

Common Python errors and how to fix them

EOFError: EOF when reading a line
Your program called input() more times than there are lines in the Input tab. Add one line of input for every input() call.
ValueError: invalid literal for int()
int(input()) got text that isn't a whole number, often two numbers on one line. Read the line with input().split() and convert each part.
IndentationError: expected an indented block
The line after a colon (if, for, def, while) must be indented. Use 4 spaces and keep the same indentation inside one block.
NameError: name 'x' is not defined
The name is misspelled or used before it gets a value. Python is case-sensitive, so Count and count are different names.

Frequently asked questions

Is there a Python code visualizer on this page?

Yes. Press Visualise instead of Run to step through your program one line at a time. You see which line runs next, the variables in each function, the call stack and what has been printed so far. It runs in your browser, reads input() from the Input tab and doesn't use up your runs.

Is this online Python compiler free?

Yes. Write and run Python code in your browser for free. Nothing to install and no setup.

How do I give input to my Python program?

Type it in the Input tab before pressing Run. Each input() call reads one line, so put each value your program asks for on its own line, or several values on one line if you read them with input().split().

Does it work on my phone?

Yes. The editor, input box and output work in a mobile browser, so you can practise Python without a laptop.

Can I use it to practise Python for placements and DSA?

Yes. Use it to try interview programs and data structure exercises, then follow the Python roadmap and the Python interview programs on Techsnap for what to practise next.