Write code, run it, and master Python — all in the browser!
Write your Python code here and see output instantly!
Click to load code into playground!
Simple arithmetic calculator
First N fibonacci numbers
Triangle star pattern
Table of any number
Level-wise problems — click to see solution!
Q1. Swap two numbers without using a third variable.
a, b = 10, 20
print(f"Before: a={a}, b={b}")
a, b = b, a
print(f"After: a={a}, b={b}")
Q2. Check if a number is even or odd.
num = int(input("Number: "))
print(f"{num} is {'Even' if num%2==0 else 'Odd'}")
Q3. Find the largest of three numbers.
a, b, c = 15, 42, 28
largest = max(a, b, c)
print(f"Largest: {largest}")
Q4. Sum of digits of a number (e.g., 123 → 1+2+3 = 6).
num = int(input("Number: "))
digit_sum = sum(int(d) for d in str(num))
print(f"Sum of digits: {digit_sum}")
Q5. Reverse a string.
text = input("Enter string: ")
print(f"Reversed: {text[::-1]}")
Q6. Count vowels in a string.
text = input("String: ").lower()
vowels = sum(1 for c in text if c in "aeiou")
print(f"Vowels: {vowels}")
Q7. Print multiplication table of any number.
n = int(input("Number: "))
for i in range(1, 11):
print(f"{n} x {i} = {n*i}")
Q8. Simple interest calculator (SI = P*R*T/100).
p = float(input("Principal: "))
r = float(input("Rate (%): "))
t = float(input("Time (years): "))
si = (p * r * t) / 100
print(f"Simple Interest: ₹{si:.2f}")
Q1. Check if a number is prime.
def is_prime(n):
if n < 2: return False
for i in range(2, int(n**0.5)+1):
if n % i == 0: return False
return True
num = int(input("Number: "))
print(f"{num} is {'Prime ✅' if is_prime(num) else 'Not Prime ❌'}")
Q2. Check if a string is palindrome.
text = input("String: ").lower().replace(" ", "")
print(f"Palindrome: {'Yes ✅' if text == text[::-1] else 'No ❌'}")
Q3. Sort a list without using sort() function.
# Bubble Sort
nums = [64, 34, 25, 12, 22, 11, 90]
n = len(nums)
for i in range(n):
for j in range(0, n-i-1):
if nums[j] > nums[j+1]:
nums[j], nums[j+1] = nums[j+1], nums[j]
print(f"Sorted: {nums}")
Q4. Find the second largest number in a list.
nums = [45, 12, 78, 34, 90, 23]
unique = list(set(nums))
unique.sort()
print(f"Second Largest: {unique[-2]}")
Q5. Count word frequency in a sentence.
text = "apple banana apple cherry banana apple"
freq = {}
for word in text.split():
freq[word] = freq.get(word, 0) + 1
for word, count in sorted(freq.items(), key=lambda x: -x[1]):
print(f"{word}: {count}")
Q6. Matrix addition (2D list).
A = [[1,2,3],[4,5,6],[7,8,9]]
B = [[9,8,7],[6,5,4],[3,2,1]]
result = [[A[i][j]+B[i][j] for j in range(3)] for i in range(3)]
for row in result:
print(row)
# [10,10,10] [10,10,10] [10,10,10]
Q1. Implement a Stack using a class.
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop() if self.items else "Stack Empty!"
def peek(self):
return self.items[-1] if self.items else None
def size(self):
return len(self.items)
s = Stack()
s.push(10); s.push(20); s.push(30)
print(s.pop()) # 30
print(s.peek()) # 20
Q2. Implement Binary Search algorithm.
def binary_search(arr, target):
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target: return mid
elif arr[mid] < target: low = mid + 1
else: high = mid - 1
return -1
nums = [2, 5, 8, 12, 16, 23, 38, 56, 72, 91]
idx = binary_search(nums, 23)
print(f"Found at index: {idx}") # 5
Q3. Create a Decorator to measure function execution time.
import time
def timer(func):
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
print(f"⏱️ {func.__name__}: {time.time()-start:.4f}s")
return result
return wrapper
@timer
def slow_sum(n):
return sum(range(n))
slow_sum(10_000_000)
Q4. Implement Linked List.
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def append(self, data):
new = Node(data)
if not self.head:
self.head = new; return
curr = self.head
while curr.next: curr = curr.next
curr.next = new
def display(self):
curr = self.head
while curr:
print(curr.data, end=" → ")
curr = curr.next
print("None")
ll = LinkedList()
for x in [10,20,30,40]: ll.append(x)
ll.display() # 10 → 20 → 30 → 40 → None