Learn PYSCRIPT with Real Code Examples
Updated Nov 26, 2025
Code Sample Descriptions
1
Hello World in PyScript
<!DOCTYPE html>
<html>
<head>
<title>Hello PyScript</title>
<link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css">
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
</head>
<body>
<py-script>
print("Hello World")
</py-script>
</body>
</html>
A basic PyScript example that prints 'Hello World' directly into a webpage using <py-script>.
2
PyScript Variables Example
<!DOCTYPE html>
<html>
<head>
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
</head>
<body>
<py-script>
x = 42
name = 'Alice'
print(x, name)
</py-script>
</body>
</html>
Defines variables in PyScript and prints them.
3
PyScript If Statement Example
<!DOCTYPE html>
<html>
<head>
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
</head>
<body>
<py-script>
num = 7
if num > 0:
print('Positive')
else:
print('Non-positive')
</py-script>
</body>
</html>
Checks if a number is positive or negative using PyScript.
4
PyScript Loop Example
<!DOCTYPE html>
<html>
<head>
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
</head>
<body>
<py-script>
for i in range(1, 6):
print(i)
</py-script>
</body>
</html>
Prints numbers from 1 to 5 using a for loop in PyScript.
5
PyScript Function Example
<!DOCTYPE html>
<html>
<head>
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
</head>
<body>
<py-script>
def add(a, b):
return a + b
print(add(3, 4))
</py-script>
</body>
</html>
Defines a function to add two numbers and prints the result in PyScript.
6
PyScript Input Example
<!DOCTYPE html>
<html>
<head>
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
</head>
<body>
<input id="nameInput" value="Alice">
<py-script>
from js import document
name = document.getElementById('nameInput').value
print('Hello', name)
</py-script>
</body>
</html>
Takes user input via <input> and prints it using PyScript.
7
PyScript Math Example
<!DOCTYPE html>
<html>
<head>
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
</head>
<body>
<py-script>
a = 5
b = 7
print('Sum:', a + b)
print('Product:', a * b)
</py-script>
</body>
</html>
Performs basic math calculations in PyScript.
8
PyScript List Example
<!DOCTYPE html>
<html>
<head>
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
</head>
<body>
<py-script>
fruits = ['Apple', 'Banana', 'Cherry']
for f in fruits:
print(f)
</py-script>
</body>
</html>
Creates a list and iterates over it in PyScript.
9
PyScript Conditional Loop Example
<!DOCTYPE html>
<html>
<head>
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
</head>
<body>
<py-script>
for i in range(1, 11):
if i % 2 == 0:
print(i)
</py-script>
</body>
</html>
Loops through numbers and prints only even numbers using PyScript.
10
PyScript Simple Plot Example
<!DOCTYPE html>
<html>
<head>
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
</head>
<body>
<py-script>
import matplotlib.pyplot as plt
plt.plot([1,2,3], [4,5,6])
plt.show()
</py-script>
</body>
</html>
Uses matplotlib to create a simple plot directly in PyScript.