Easy Python Code Snippets
Explore these Python code examples that you can easily copy and use in your own projects.
Basic Hello World
print("Hello, World!")
Simple For Loop
for i in range(1, 11): print(i, end=' ') print()
Basic Calculator
operator = input("Enter an operator (+, -, *, /): ") num1 = float(input("Enter the first number: ")) num2 = float(input("Enter the second number: ")) if operator == '+': result = num1 + num2 elif operator == '-': result = num1 - num2 elif operator == '*': result = num1 * num2 elif operator == '/': if num2 != 0: result = num1 / num2 else: result = "Division by zero is not allowed." else: result = "Invalid operator." print("Result:", result)
Temperature Converter
celsius = float(input("Enter temperature in Celsius: ")) fahrenheit = (celsius * 9/5) + 32 print(f"{celsius:.2f} Celsius is equal to {fahrenheit:.2f} Fahrenheit")
Guess the Number Game
import random number = random.randint(1, 100) attempts = 0 print("Welcome to the Guess the Number Game!") while True: guess = int(input("Enter your guess (1-100): ")) attempts += 1 if guess > number: print("Too high! Try again.") elif guess < number: print("Too low! Try again.") else: print(f"Congratulations! You guessed the number in {attempts} attempts.") break
Simple ATM Interface
account_balance = 1000.0 # Initial account balance def display_balance(): print(f"Your account balance is ${account_balance:.2f}") def withdraw(amount): global account_balance if 0 < amount <= account_balance: account_balance -= amount print(f"Withdrawal successful. Remaining balance: ${account_balance:.2f}") else: print("Invalid amount or insufficient balance.") print("Welcome to the Simple ATM Interface") while True: print("Menu:") print("1. Display Balance") print("2. Withdraw") print("3. Exit") choice = input("Enter your choice: ") if choice == '1': display_balance() elif choice == '2': amount = float(input("Enter the withdrawal amount: $")) withdraw(amount) elif choice == '3': print("Thank you for using the ATM. Goodbye!") break else: print("Invalid choice. Please try again.")
Palindrome Checker
def is_palindrome(string): return string == string[::-1] input_str = input("Enter a string: ") if is_palindrome(input_str): print("It's a palindrome.") else: print("It's not a palindrome.")
Simple Interest Calculator
principal = float(input("Enter principal amount: ")) rate = float(input("Enter rate of interest: ")) time = float(input("Enter time period (in years): ")) interest = (principal * rate * time) / 100.0 print(f"Simple Interest = {interest:.2f}")
Factorial Calculator
def factorial(n): if n == 0 or n == 1: return 1 else: return n * factorial(n - 1) num = int(input("Enter a positive integer: ")) if num < 0: print("Factorial is not defined for negative numbers.") else: fact = factorial(num) print(f"Factorial of {num} = {fact}")
Fibonacci Series Generator
def generate_fibonacci(n): a, b = 0, 1 fibonacci_series = [] for i in range(n): if i == 0: fibonacci_series.append(a) elif i == 1: fibonacci_series.append(b) else: next_term = a + b fibonacci_series.append(next_term) a = b b = next_term return fibonacci_series terms = int(input("Enter the number of terms for the Fibonacci series: ")) fibonacci = generate_fibonacci(terms) print(f"Fibonacci Series (first {terms} terms): {', '.join(map(str, fibonacci))}")
Prime Number Checker
def is_prime(num): if num <= 1: return False for i in range(2, int(num**0.5) + 1): if num % i == 0: return False return True num = int(input("Enter a positive integer: ")) if is_prime(num): print(f"{num} is a prime number.") else: print(f"{num} is not a prime number.")