BCA 3 - Introduction to Programming
Unit 1: Introduction to Programming with Python
1. Problem Solving Strategies
Programming begins with problem solving – converting a real-world problem into a step-
by-step solution that a computer can execute.
Steps in Problem Solving:
1. Problem Definition – Understand the problem clearly. Identify input, process, and
expected output.
2. Problem Analysis – Break the problem into smaller parts. Decide the approach
(mathematical, logical, algorithmic).
3. Designing a Solution – Use algorithms or flowcharts/pseudo code.
4. Implementation – Write code in a programming language (Python).
5. Testing and Debugging – Run program with test data and fix errors.
6. Documentation and Maintenance – Comment code, update, and improve.
Example Problem: Find the sum of first 10 natural numbers.
Algorithm: Initialize sum=0, repeat for i=1 to 10: sum=sum+i, print sum.
Python Code Example:
sum = 0
for i in range(1, 11):
sum += i
print("Sum =", sum)
2. Structure of a Python Program
1. Documentation/Comments:
- Single line: # This is a comment
- Multi-line: """ This is a comment """
2. Import Statements: e.g., import math
3. Functions/Classes: Defined using def keyword.
4. Main Program Logic: Written sequentially or under if __name__ == '__main__':
Example Program Structure:
# Program to calculate area of circle
import math
def area_circle(r):
return [Link] * r * r
radius = 5
print("Area =", area_circle(radius))
3. Syntax and Semantics
Syntax: Rules of writing code (grammar of Python).
- Example error: if x > 0 print(x) ❌
- Correct: if x > 0: print(x) ✅
Semantics: Meaning of the code (logic).
- Example: area = length + width (wrong formula). Syntax correct but semantic error.
Common Python Syntax Rules:
- Indentation is mandatory (4 spaces).
- No need for semicolons (though allowed).
- Variables are dynamically typed.
4. Executing Simple Programs in Python
1. Write the program and save with .py extension.
2. Run in Interpreter (IDLE/command line: python [Link]).
3. Interactive Mode: Run statements directly in Python shell.
Examples:
# Hello World
print("Hello, World!")
# Sum of Two Numbers
a=5
b=3
print("Sum =", a + b)
# Check Even or Odd
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")
Summary
Problem solving requires defining, analyzing, designing, implementing, and testing.
A Python program has comments, imports, functions, and logic.
Syntax = rules, Semantics = meaning.
Programs can be executed in script mode or interactive mode.