0% found this document useful (0 votes)
20 views4 pages

Indentation and If Statements

The document covers control statements, lists, and tuples in Python programming. It explains control flow, syntax, and the importance of indentation, along with examples of if, if-else, and if-elif-else statements for decision-making. Additionally, it discusses lists and tuples, including their properties and methods.

Uploaded by

Dept CSCA
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)
20 views4 pages

Indentation and If Statements

The document covers control statements, lists, and tuples in Python programming. It explains control flow, syntax, and the importance of indentation, along with examples of if, if-else, and if-elif-else statements for decision-making. Additionally, it discusses lists and tuples, including their properties and methods.

Uploaded by

Dept CSCA
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

V SURESH KUMAR, HEAD & AP, SVASC

UNIT II

UNIT II CONTROL STATEMENTS, LISTS, TUPLES


CONTROL STATEMENTS: Control Flow and Syntax - Indenting - if Statement -
statements and expressions- string operations- Boolean Expressions -while Loop - break
and continue - for Loop. LISTS: List-list slices - list methods - list loop–mutability–
aliasing - cloning lists - list parameters. TUPLES: Tuple assignment, tuple as return
value -Sets–Dictionaries.

Control Flow and Syntax


Control flow refers to the order in which statements in a program are executed.
Python provides several mechanisms to control the flow of execution, allowing you to make
1. Decisions,
2. Repeat blocks of code, and
3. Handle exceptions.

Syntax in Python

Python has a clear and brief syntax that highlights readability

Python is case-sensitive

Keywords are reserved words in Python that have special meanings and cannot be
used as variable names, function names, or any other identifiers.

They are essential for the language's syntax and structure

Indenting

Indentation in Python

Indentation in Python is not just for style.

It is a fundamental part of the Programming language.

Python uses indentation to define the structure of code.

In some programming languages use braces {} or keywords like end in other languages.

Failing to properly indent your code will result in a syntax error.

Consistent Indentation:
All lines within a block must have the same level of indentation.
Mixing spaces and tabs in the same file is not allowed.
Example with Consistent Indentation:
num = 10
if num % 2 == 0:
print(f"{num} is even")
else:
print(f"{num} is odd")
Output:
10 is even
V SURESH KUMAR, HEAD & AP, SVASC

Example without Consistent Indentation:


num = 10
if num % 2 == 0:
print(f"{num} is even")
else:
print(f"{num} is odd")
Output:
Indentation Error

Standard Indentation:

Python's convention is to use 4 spaces per indentation level.

Mandatory Indentation:

Python requires indentation to mark blocks of code, such as in if, for, while, def, and class.

Example:

BRANCHING (IF)
STATEMENTS AND EXPRESSIONS
1. If Statement
If statements are used for decision-making.
They allow you to execute a block of code conditionally, based on whether a given
condition is True or False.
If the condition is True, the block of code that is present inside the if statement is executed
"if statement" is written by using the if keyword.
Diagram
V SURESH KUMAR, HEAD & AP, SVASC

Example
a = 33
b = 200
if b > a:
print("b is greater than a")

Explanation:
In this example we use two variables, a and b,
If statement test whether b is greater than a.

Then print to screen that "b is greater than a".

2. if-else Statement
if…else statement is also used for decision-making based on specific conditions.
If the condition is true, True block will be executed otherwise false block will be executed
Diagram

Example:
x=3
if x > 5:
print("x is greater than 5")
else:
print("x is not greater than 5")

Explanation:
In this example we use one variables, x
If statement test whether x is greater than 5 or not.
V SURESH KUMAR, HEAD & AP, SVASC

Then print to screen that " x is not greater than 5".

3. if-elif-else Statement
if-elif-else statement is also used for decision-making based on specific conditions
The else statement executed when no matches found
Diagram

Example:
x=7
if x == 10:
print("x is greater than 10")
elif x == 7:
print("x is equal to 7")
else:
print("x is less than or equal to 10 but not equal to 7")

Explanation:
In this example we use one variables, x
If statement test whether x is equal to 7 or not.
The if condition false, then elif condition is true

Then print to screen that " x is equal to 7".

You might also like