Looking For Anything Specific?

Free Demo

📚 Book a Free Online Demo Class Now!

Learn with clarity, personal mentorship & expert guidance.

🚀 Book Now

10 Important basic python codes

 


1. Hello World Program

print("Hello, World!")

Explanation:

  • print() is used to display output.
  • "Hello, World!" is a string (text inside quotes).
  • This is the first program to check if Python is working.

2. Add Two Numbers

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Sum:", a + b)

Explanation:

  • input() takes user input (as string).
  • int() converts input into integer.
  • a + b adds both numbers.
  • print() displays the result.

3. Check Even or Odd

num = int(input("Enter a number: "))

if num % 2 == 0:
print("Even")
else:
print("Odd")

Explanation:

  • % is modulus (remainder operator).
  • If a number divided by 2 gives remainder 0, it's even.
  • if-else is used for decision making.

4. Largest of Three Numbers

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))

if a >= b and a >= c:
print("Largest:", a)
elif b >= a and b >= c:
print("Largest:", b)
else:
print("Largest:", c)

Explanation:

  • and checks multiple conditions.
  • First condition checks if a is largest.
  • elif checks second condition.
  • else handles the remaining case.

5. Simple Calculator

a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
op = input("Enter operation (+, -, *, /): ")

if op == "+":
print(a + b)
elif op == "-":
print(a - b)
elif op == "*":
print(a * b)
elif op == "/":
print(a / b)
else:
print("Invalid operation")

Explanation:

  • float() allows decimal numbers.
  • op stores the operation symbol.
  • Based on the symbol, different operations are performed.

6. Factorial of a Number

num = int(input("Enter a number: "))
fact = 1

for i in range(1, num + 1):
fact *= i

print("Factorial:", fact)

Explanation:

  • Factorial = 1 × 2 × 3 × ... × n
  • range(1, num+1) loops from 1 to num.
  • fact *= i means fact = fact * i.

7. Multiplication Table

num = int(input("Enter a number: "))

for i in range(1, 11):
print(num, "x", i, "=", num * i)

Explanation:

  • Loop runs from 1 to 10.
  • Each time it multiplies num * i.

8. Reverse a String

text = input("Enter a string: ")
print("Reversed:", text[::-1])

Explanation:

  • [::-1] is slicing.
  • It reverses the string.
  • Very powerful Python shortcut.

9. Count Vowels

text = input("Enter a string: ")
vowels = "aeiouAEIOU"
count = 0

for char in text:
if char in vowels:
count += 1

print("Vowel count:", count)

Explanation:

  • Loop checks each character.
  • if char in vowels checks if it is a vowel.
  • count += 1 increases counter.

10. Check Prime Number

num = int(input("Enter a number: "))

if num > 1:
for i in range(2, num):
if num % i == 0:
print("Not Prime")
break
else:
print("Prime")
else:
print("Not Prime")

Explanation:

  • Prime numbers are divisible only by 1 and itself.
  • Loop checks divisibility from 2 to num-1.
  • break stops loop if divisor found.
  • else runs only if loop completes fully.

Post a Comment

0 Comments