SIMPLE PYTHON PROGRAMS USING FLOWCHART AND PSEUDOCODE
1. Pseudocode
2. Flowchart (described textually)
3. Python Code
Example 1: Add Two Numbers
Aim
To write a simple python program for using flowchart and pseudocode.
Algorithm
1. Start
2. Declare variables: num1, num2, sum.
3. Prompt for and read num1 from the user.
4. Prompt for and read num2.
5. Compute sum = num1 + num2.
6. Display the result: "The sum is: ", sum.
7. End
Pseudocode:
START
Input num1
Input num2
sum = num1 + num2
Display sum
END
Flowchart (Text Description):
Python Code:
# Program to add two numbers
# Input
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
# Processing
sum = num1 + num2
# Output
print("The sum is:", sum)
Example 2: Check Even or Odd
🔹 Pseudocode:
START
Input number
IF number % 2 == 0 THEN
Display "Even"
ELSE
Display "Odd"
END IF
END
🔹 Flowchart:
🔹 Python Code:
# Program to check even or odd
# Input
num = int(input("Enter a number: "))
# Decision
if num % 2 == 0:
print("The number is Even")
else:
print("The number is Odd")
Example 3: Find the Largest of Two Numbers
🔹 Pseudocode:
START
Input a, b
IF a > b THEN
Display "a is larger"
ELSE
Display "b is larger"
END IF
END
🔹 Flowchart:
[Start]
|
[Input a, b]
|
[Is a > b?]
/ \
Yes No
| |
[a is larger] [b is larger]
\ /
[End]
Python Code:
# Program to find the larger of two numbers
# Input
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
# Decision
if a > b:
print("The first number is larger.")
else:
print("The second number is larger.")
Each example follows:
✅ Pseudocode → outlines the logic step-by-step
🔄 Flowchart → explains the visual flow of decisions and processes
🐍 Python Code → actual working program
PROGRAMS USING IF CONDITION ELSE IF CONDITON FORLOOP WHILE
LOOP AND PASS STATEMENTS
[Link] if a number is Positive, Negative, or Zero
# Program to check whether a number is positive, negative or zero
# Input
num = float(input("Enter a number: "))
# Conditional logic
if num > 0:
print("The number is positive.")
elif num == 0:
print("The number is zero.")
else:
print("The number is negative.")
2. Check if a Number is Even or Odd
# Program to check if the number is even or odd
# Input
num = int(input("Enter an integer: "))
# Conditional logic
if num % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
#[Link] to find the largest of three numbers
# Input
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))
# Conditional logic
if a >= b and a >= c:
print("The largest number is:", a)
elif b >= a and b >= c:
print("The largest number is:", b)
else:
print("The largest number is:", c)
WHILE LOOP
[Link] of digits of a number using while loop
# Program to find the sum of digits of a number
num = int(input("Enter a number: "))
sum = 0
while num > 0:
digit = num % 10
sum += digit
num = num // 10
print("Sum of digits is:", sum)
[Link] while loop: Print a pattern
# Program to print a pattern using nested while loops
rows = 5
i=1
while i <= rows:
j=1
while j <= i:
print("*", end=" ")
j += 1
print() # Newline
i += 1
Output:
*
**
***
****
*****
Break Loop with Condition + Pass Example
# Program using break and pass together
i=1
while i <= 5:
if i == 4:
pass # Placeholder
print("At i =", i, "-> pass executed (no action)")
elif i == 6:
break
else:
print("i =", i)
i += 1
FOR LOOP PROGRAMS
[Link] to print numbers from 1 to 10 using for loop
print("Numbers from 1 to 10:")
for i in range(1, 11):
print(i)
[Link] to print all even numbers from 1 to 20
print("Even numbers from 1 to 20:")
for i in range(1, 21):
if i % 2 == 0:
print(i)
USAGE OF FUNCTIONS
[Link] to Check if Number is Prime
# Function to check prime
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
# Main program
num = int(input("Enter a number: "))
if is_prime(num):
print("It is a prime number.")
else:
print("It is not a prime number.")
2. Function to calculate factorial
def factorial(n):
result = 1
for i in range(1, n + 1):
result *= i
return result
# Main program
num = int(input("Enter a number: "))
print(f"Factorial of {num} is:", factorial(num))
RECURSIVE AND LAMDA FUNCTIONS
Factorial using Recursion
# Recursive function to calculate factorial
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
# Main program
num = int(input("Enter a number: "))
print(f"Factorial of {num} is: {factorial(num)}")
Recursive function to get nth Fibonacci number
def fibonacci(n):
if n <= 0:
return 0
elif n == 1:
return 1
else:
return fibonacci(n - 1) + fibonacci(n - 2)
# Main program
terms = int(input("Enter number of terms: "))
print("Fibonacci sequence:")
for i in range(terms):
print(fibonacci(i), end=' ')
Lambda function to check even or odd
check_even_odd = lambda x: "Even" if x % 2 == 0 else "Odd"
# Main program
num = int(input("Enter a number: "))
print("The number is:", check_even_odd(num))
Lambda with map to square all items in a list
numbers = [1, 2, 3, 4, 5]
squares = list(map(lambda x: x ** 2, numbers))
print("Original list:", numbers)
print("Squares:", squares)
STRING MANIPULATIONS AND OPERATIONS IN PYTHON
1. Find Length of a String
# Program to find the length of a string
text = input("Enter a string: ")
length = len(text)
print("Length of the string is:", length)
✅ 2. Convert String to Uppercase and Lowercase
# Program to convert string to uppercase and lowercase
text = input("Enter a string: ")
upper_text = [Link]()
lower_text = [Link]()
print("Uppercase:", upper_text)
print("Lowercase:", lower_text)
✅ 3. Check if a Substring Exists
# Program to check if a substring exists in a string
text = input("Enter a string: ")
substring = input("Enter substring to search: ")
if substring in text:
print(f"'{substring}' is found in the string.")
else:
print(f"'{substring}' is NOT found in the string.")
✅ 4. Reverse a String
# Program to reverse a string
text = input("Enter a string: ")
reversed_text = text[::-1]
print("Reversed string:", reversed_text)
✅ 5. Count Number of Vowels in a String
# Program to count vowels in a string
text = input("Enter a string: ")
vowels = "aeiouAEIOU"
count = 0
for char in text:
if char in vowels:
count += 1
print("Number of vowels:", count)
✅ 6. Replace Substring in a String
# Program to replace a substring with another substring
text = input("Enter a string: ")
old_sub = input("Enter substring to replace: ")
new_sub = input("Enter new substring: ")
new_text = [Link](old_sub, new_sub)
print("Updated string:", new_text)
✅ 7. Split String into Words
# Program to split string into words
text = input("Enter a string: ")
words = [Link]()
print("List of words:", words)
✅ 8. Join List of Words into a String
# Program to join list of words into a string
words = ['Hello', 'world', 'Python', 'is', 'awesome']
sentence = " ".join(words)
print("Joined sentence:", sentence)