6.1 Fundamentals of Programming
6.1 Fundamentals of Programming
Overview:
Unit 6 introduces the basics of programming, focusing on flow control, conditionals, loops, and
the Python programming environment. It emphasizes understanding the structure and syntax of
Python programs, as well as debugging techniques.
Key Concepts:
1. Program Flow Control:
Defines the order in which statements are executed in a program. Python primarily uses three
constructs:
Sequential: Statements run in the order they appear.
Branching (Conditional Statements): Allows the program to make
decisions based on conditions.
Iteration (Loops): Enables repeated execution of a block of code.
Example:
# Sequential flow
print("Start")
print("End")
2. Conditionals:
Conditional statements evaluate conditions and execute code blocks based on whether
the conditions are true or false.
If Statement:
Executes a block if the condition is true.
Example:
age = 20
if age >= 18:
print("You are an adult.")
If...Else Statement:
Provides an alternative block of code if the condition is false.
Example:
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
If...Elif...Else Statement:
Allows multiple conditions to be checked sequentially.
Example:
score = 85
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
else:
print("Grade: C")
3. Loops:
Used for repeating tasks until a condition is met.
For Loop:
Iterates over a sequence (like a list or string).
Example:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
While Loop:
Continues executing as long as the condition is true.
Example:
count = 0
while count < 5:
print(count)
count += 1
Break and Continue Statements:
Break: Exits the loop immediately.
Continue: Skips the current iteration and proceeds to the next.
Example:
for num in range(10):
if num == 5:
break
print(num) # Prints numbers 0 to 4