KEMBAR78
LECTURE 4 Programming | PDF
0% found this document useful (0 votes)
7 views4 pages

LECTURE 4 Programming

This lecture covers the basics of programming in Python, focusing on indentation, control structures, and selection statements. It explains how to use if, if-else, if-elif-else, and nested if statements, along with boolean expressions and operators for condition checking. Additionally, it discusses compound conditions and string checks that can be used in conditional statements.

Uploaded by

Gelo Maranan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

LECTURE 4 Programming

This lecture covers the basics of programming in Python, focusing on indentation, control structures, and selection statements. It explains how to use if, if-else, if-elif-else, and nested if statements, along with boolean expressions and operators for condition checking. Additionally, it discusses compound conditions and string checks that can be used in conditional statements.

Uploaded by

Gelo Maranan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

LECTURE 4 programming

Indentation

• Python uses indentation to group code blocks

• Use the same number of spaces in a block (usually 4)

• Lines starting with if, while, def, etc. must end with :

• ; at the end is optional

Control Structures

• Python runs code line by line

• Control structures change the flow using:

o Sequence – step-by-step

o Selection – choose based on a condition

o Iteration – repeat code using loops

Selection (Conditional) Structure

• Allows choosing between two or more paths

• Has 3 parts:

1. Condition to check

2. Statement if condition is True

3. Statement if condition is False

Types of Selection Statements

1. if

2. if-else

3. if-elif-else

4. Nested if
f Statement

• Checks a condition. If it's true, runs the code under it.

Syntax:

if condition:

statement(s)

• The condition must be a boolean (True/False)

• You can write it in one line: if x > 10: print("Yes")

Boolean Expressions & Operators

Used to compare values. They return True or False

• == equal to

• != not equal

• < less than

• <= less than or equal

• > greater than

• >= greater than or equal

if-else Statement

Runs one block if condition is True, another if False

if condition:

do_this()

else:

do_that()

You can also write this in one line:

print("Yes") if x > 10 else print("No")


if-elif-else Statement

• Use when checking many conditions

• Python runs from top to bottom and stops at the first True

Syntax:

if condition1:

statement1

elif condition2:

statement2

...

else:

final_statement

• Only one block runs.

Nested if

An if inside another if

if outer_condition:

if inner_condition:

statement

• Used when a decision depends on another condition

Compound Conditions

Use more than one condition with:

• and → True if both are True

• or → True if at least one is True

• not → Reverses the result

Examples:

if age >= 18 and has_license:

print("You can drive")


String Checks in Conditions

• isupper() – all uppercase → True

• islower() – all lowercase → True

• isdigit() – all digits → True

• isalpha() – all letters → True

Used to check string types in conditions.

You might also like