0% found this document useful (0 votes)
10 views47 pages

Python Control Flow & String Operations

Uploaded by

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

Python Control Flow & String Operations

Uploaded by

M Narasimhareddy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

CONDITIONAL & LOOPING

CONSTRUCTS AND STRING Module 2

OPERATIONS
CONTROL FLOW
Control flow in Python determines the order in which statements
are executed in a program. It consists of conditional statements,
loops, and function calls.
1. Conditional Statements (if, elif, else)
These allow the program to make decisions based on conditions.
CONTROL FLOW
2. Loops (for, while)
Loops help execute a block of code multiple times.
For Loop
Used to iterate over sequences (lists, tuples, strings, ranges, etc.).
Control statements

Conditional
Jumping(Loop
Statements(Decision Looping statements
Control statements
Making statements)

if If-else elif While For Break Continue Pass


CONTROL FLOW
2. Loops (for, while)
Loops help execute a block of code multiple times.
While Loop
Executes as long as a condition remains true.
CONTROL FLOW
3. Loop Control Statements
•break → Exits the loop completely.
•continue → Skips the current iteration and moves
to the next one.
•pass → Does nothing; used as a placeholder.
CONTROL FLOW
4. Function Calls
Functions control the flow by breaking the program into reusable
blocks.
DECISION MAKING
STATEMENTS
In Python, decision-making statements allow the program to
execute different code blocks based on conditions. These
statements include:
1. if Statement
Executes a block of code if the given
condition is True.
DECISION MAKING
STATEMENTS
2. if-else Statement
Executes one block of code if the condition is True and
another if it is False.
DECISION MAKING
STATEMENTS
3. if-elif-else Statement
Allows checking multiple conditions. The first True
condition is executed.
DECISION MAKING
STATEMENTS
4. Nested if-else Statement
An if statement inside another if or else block.
LOOPING STATEMENTS IN
PYTHON
In Python, loops are used to execute a block of code multiple times.
There are two main types of loops:
1️⃣for Loop
The for loop is used to iterate over a sequence (such as a list, tuple,
string, or range).
LOOPING STATEMENTS IN
PYTHON
Examples:
LOOPING STATEMENTS IN
PYTHON
In Python, loops are used to execute a block of code multiple times.
There are two main types of loops:
2️⃣while Loop
A while loop runs as long as the condition
is True.
LOOPING STATEMENTS IN
PYTHON
Examples:
LOOPING STATEMENTS IN
PYTHON
In Python, loops are used to execute a block of code multiple times.
There are two main types of loops:

3️⃣Loop Control Statements


•break → Exits the loop completely
•continue → Skips the current iteration and moves
to the next one
•pass → Does nothing (used as a placeholder)
LOOPING STATEMENTS IN
PYTHON
In Python, loops are used to execute a block of code multiple times.
There are two main types of loops:
NESTED LOOP
A nested loop is a loop inside another loop. The inner loop
executes completely for each iteration of the outer loop.

1️⃣Syntax of Nested Loops


NESTED LOOP
A nested loop is a loop inside another loop. The inner loop
executes completely for each iteration of the outer loop.

1️⃣Syntax of Nested Loops


NESTED LOOP
A nested loop is a loop inside another loop. The inner loop
executes completely for each iteration of the outer loop.

Examples:
JUMP STATEMENTS
Jump statements control the flow of loops and conditional
structures in Python. They help manage program execution
effectively.

1. break Statement
•Purpose: Exits a loop immediately when a specific condition is met.
•Use Case in Cybersecurity: Stopping a brute-force attack simulation once a
correct password is found.
JUMP STATEMENTS
Jump statements control the flow of loops and conditional
structures in Python. They help manage program execution
effectively.

2. continue Statement
•Purpose: Skips the current iteration of a loop and moves to the next
iteration.
•Use Case in Cybersecurity: Skipping invalid login attempts while
processing logs.
JUMP STATEMENTS
Jump statements control the flow of loops and conditional
structures in Python. They help manage program execution
effectively.
3. pass Statement
•Purpose: Acts as a placeholder where code is required syntactically but
no action is needed.
•Use Case in Cybersecurity: Reserving space for future security
features without breaking code execution.
STRING AND Part 2
OPERATIONS
STRINGS AND OPERATIONS
A string is a sequence of characters enclosed in single ('), double
("), or triple quotes (''' or """ """) in Python. Strings are immutable,
meaning they cannot be changed after creation.
STRINGS AND OPERATIONS
String Operations
A. String
Concatenation (+)
Combining two or more strings.
STRINGS AND OPERATIONS
String Operations
B. String
Repetition (*)
Repeating a string multiple times.
STRINGS AND OPERATIONS
String Operations
C. String Slicing
([:])
Extracting a substring.
STRINGS AND OPERATIONS
Updating a String
STRINGS AND OPERATIONS
Updating a String
STRINGS AND OPERATIONS
Updating a String
STRINGS AND OPERATIONS
Updating a String
STRINGS AND OPERATIONS
Updating a String
MEMBERSHIP (IN AND NOT
IN)
In Python, membership operators are used to check if a value is present in a
sequence (like a list, tuple, dictionary, set, or string). The two membership
operators are:

1. in (checks if a value exists in a sequence)

2. not in (checks if a value does not exist in a sequence)


MEMBERSHIP (IN AND NOT
IN)
Examples:
MEMBERSHIP (IN AND NOT
IN)
Examples:
REVERSE
In Python, you can reverse sequences (like lists, strings, and
tuples) using different methods. Here are some common ways:
REVERSE
In Python, you can reverse sequences (like lists, strings, and
tuples) using different methods. Here are some common ways:
REVERSE
In Python, you can reverse sequences (like lists, strings, and
tuples) using different methods. Here are some common ways:
REVERSE
In Python, you can reverse sequences (like lists, strings, and
tuples) using different methods. Here are some common ways:
COMPARISON OPERATORS ( ==,
>,<,>=,<=,!=)
Comparison operators are used to compare two values. They return True or False
based on the condition.

Examples:
COMPARISON OPERATORS ( ==,
>,<,>=,<=,!=)
Comparison operators are used to compare two values. They return True or False
based on the condition.

Examples:
STRING METHODS AND BUILT
IN FUNCTIONS
Python provides several built-in functions and string methods to
manipulate text efficiently.
STRING METHODS AND BUILT
IN FUNCTIONS
Python provides several built-in functions and string methods to
manipulate text efficiently.

text = "python is
fun!“
result =
[Link]()
print(result)
STRING METHODS AND BUILT
IN FUNCTIONS
Python provides several built-in functions and string methods to
manipulate text efficiently.
STRING METHODS AND BUILT
IN FUNCTIONS
Python provides several built-in functions and string methods to
manipulate text efficiently.
STRING METHODS AND BUILT
IN FUNCTIONS
Python provides several built-in functions and string methods to
manipulate text efficiently.

def greet(name):
""" Greeting Message
"""
return f"Hello,
{name}!“
print(greet("Alice"))
print(greet.__doc__)

You might also like