Basic Python Codes
1. Swapping Two Variables
This logic demonstrates how to exchange values between two variables using a "temporary"
container to hold data during the transfer.
# Original values
a=5
b = 10
# Swap values using a temporary variable to make logic clear
temp = a # save value of a
a=b # assign b to a
b = temp # assign saved value to b
print("a =", a, "b =", b)
2. Check Even or Odd
This uses the modulo operator (%) to check for a remainder.
num = 7
# A number is even if it is divisible by 2 with zero remainder
if num % 2 == 0:
print(num, "is Even")
else:
print(num, "is Odd")
3. Find Maximum of Three Numbers (Logic)
Instead of using a built-in function, this logic assumes the first number is the largest and updates that
assumption if a larger number is found.
a, b, c = 10, 5, 8
# Assume a is the largest
largest = a
# Compare with b
if b > largest:
largest = b
# Compare with c
if c > largest:
largest = c
print("Largest =", largest)
4. Reverse a String (Step by Step)
This logic iterates backwards through the string indices to build a new string.
s = "hello"
reversed_string = ""
# Loop from last character to first
# range(start, stop, step) -> start at length-1, go to -1 (exclusive), step -1
for i in range(len(s)-1, -1, -1):
reversed_string += s[i]
print("Reversed:", reversed_string)
5. Count Vowels in a String
This example demonstrates traversing a string and checking membership against a defined set of
characters.
text = "python"
vowels = "aeiou"
count = 0
# Check each character to see if it is a vowel
for ch in text:
if ch in vowels:
count += 1
print("Number of vowels:", count)
6. Create a List From 1 to 10
This shows how to dynamically add elements to an empty list using a loop.
numbers = []
# Add numbers one by one
for i in range(1, 11):
[Link](i)
print(numbers)
7. Sum of Elements in a List (Manual Logic)
This represents the "accumulator" pattern, where a total variable is updated by every item in a list.
nums = [1, 2, 3, 4, 5]
total = 0
# Add each element to total
for n in nums:
total += n
print("Sum:", total)
8. Factorial of a Number
Similar to the sum logic, but using multiplication to calculate $n!$.
n=5
fact = 1
# Multiply numbers from 1 to n
for i in range(1, n + 1):
fact *= i
print("Factorial:", fact)
9. Check if a Number is Prime (Logic)
This checks if a number is divisible by anything other than 1 and itself. We only need to check up to
the square root of the number ($\sqrt{n}$) for efficiency.
n = 13
is_prime = True # assume number is prime
# Prime number must be >= 2
if n < 2:
is_prime = False
else:
# Try dividing n by numbers from 2 to sqrt(n)
for i in range(2, int(n**0.5) + 1):
if n % i == 0: # divisible -> not prime
is_prime = False
break
if is_prime:
print(n, "is Prime")
else:
print(n, "is NOT Prime")
10. Find Largest Number in a List
This iterates through a list to find the maximum value manually.
nums = [3, 8, 2, 10, 6]
# Start by assuming first number is largest
largest = nums[0]
# Check each number
for n in nums:
if n > largest:
largest = n
print("Largest number:", largest)