2. UNIT 2 : SYLLABUS
• Conditions, Boolean logic, logical operators; (Refer UNIT 1: Operators)
• ranges
• Control statements: if-else, loops (for, while); short-circuit (lazy) evaluation
• Strings and text files; manipulating files and directories, os and sys
modules; text files: reading/writing text and numbers from/to a file;
creating and reading a formatted file (csv or tab-separated).
• String manipulations: subscript operator, indexing, slicing a string;
• strings and number system: converting strings to numbers and vice versa.
Binary, octal, hexadecimal numbers
3. range function
• The range() function returns a sequence of
numbers, starting from 0 by default, and
increments by 1 (by default), and stops
before a specified number
• The range() function can be represented in
three different ways, or you can think of
them as three range parameters:
1. range(stop)
2. range(start, stop)
3. range(start, stop, step)
• start: integer starting from which the
sequence of integers is to be returned
• stop: integer before which the
sequence of integers is to be returned.
The range of integers end at stop – 1.
• step: integer value which determines
the increment between each integer in
the sequence
5. Strings and text files
Control statements
strings and number
system
String manipulations
6. Conditional (if – else)
Conditional
execution
Alternative
execution
Chained conditionals
Nested
conditionals
• Conditions
change the flow
of program
execution
• if statement is
used in conditions
Syntax:
if condition:
statement 1
…
• Two possibilities
(else is used)
Syntax:
if condition:
statement 1
…
else:
statement 2
…
• More than two
possibilities.
• elif is used.
Syntax:
if condition:
statement 1
elif condition:
statement 2
elif condition:
statement 3
else:
statement 4
• Another
Condition in one
condition
Syntax:
if condition1:
if condition2:
statement 1
else:
statement 2
else:
statement 3
Control
statements
7. Conditional (if – else)
Control
statements
Conditional
execution
• Conditions
change the flow
of program
execution
• if statement is
used in conditions
Syntax:
if condition:
statement 1
…
8. Conditional (if – else)
Control
statements
Conditional
execution
• Conditions
change the flow
of program
execution
• if statement is
used in conditions
Syntax:
if condition:
statement 1
…
9. Control
statements
Conditional
execution
Alternative
execution
Chained conditionals
Nested
conditionals
• Conditions
change the flow
of program
execution
• if statement is
used in conditions
Syntax:
if condition:
statement 1
…
• Two possibilities
(else is used)
Syntax:
if condition:
statement 1
…
else:
statement 2
…
• More than two
possibilities.
• elif is used.
Syntax:
if condition:
statement 1
elif condition:
statement 2
elif condition:
statement 3
else:
statement 4
• Another
Condition in one
condition
Syntax:
if condition1:
if condition2:
statement 1
else:
statement 2
else:
statement 3
Conditional (if – else)
10. Conditional (if – else)
Control
statements
Alternative
execution
• Two possibilities
(else is used)
Syntax:
if condition:
statement 1
…
else:
statement 2
…
11. Conditional (if – else)
Control
statements
Alternative
execution
• Two possibilities
(else is used)
Syntax:
if condition:
statement 1
…
else:
statement 2
…
12. Conditional (if – else)
Control
statements
Conditional
execution
Alternative
execution
Chained conditionals
Nested
conditionals
• Conditions
change the flow
of program
execution
• if statement is
used in conditions
Syntax:
if condition:
statement 1
…
• Two possibilities
(else is used)
Syntax:
if condition:
statement 1
…
else:
statement 2
…
• More than two
possibilities.
• elif is used.
Syntax:
if condition:
statement 1
elif condition:
statement 2
elif condition:
statement 3
else:
statement 4
• Another
Condition in one
condition
Syntax:
if condition1:
if condition2:
statement 1
else:
statement 2
else:
statement 3
13. Conditional (if – else)
Control
statements
Chained conditionals
• More than two
possibilities.
• elif is used.
Syntax:
if condition:
statement 1
elif condition:
statement 2
elif condition:
statement 3
else:
statement 4
14. Conditional (if – else)
Control
statements
Chained conditionals
• More than two
possibilities.
• elif is used.
Syntax:
if condition:
statement 1
elif condition:
statement 2
elif condition:
statement 3
else:
statement 4
15. Conditional (if – else)
Control
statements
Conditional
execution
Alternative
execution
Chained conditionals
Nested
conditionals
• Conditions
change the flow
of program
execution
• if statement is
used in conditions
Syntax:
if condition:
statement 1
…
• Two possibilities
(else is used)
Syntax:
if condition:
statement 1
…
else:
statement 2
…
• More than two
possibilities.
• elif is used.
Syntax:
if condition:
statement 1
elif condition:
statement 2
elif condition:
statement 3
else:
statement 4
• Another
Condition in one
condition
Syntax:
if condition1:
if condition2:
statement 1
else:
statement 2
else:
statement 3
16. Conditional (if – else)
Control
statements
Nested
conditionals
• Another
Condition in one
condition
Syntax:
if condition1:
if condition2:
statement 1
else:
statement 2
else:
statement 3
17. Conditional (if – else)
Control
statements
Nested
conditionals
• Another
Condition in one
condition
Syntax:
if condition1:
if condition2:
statement 1
else:
statement 2
else:
statement 3
18. Conditional (if – else)
Control
statements
Conditional
execution
Alternative
execution
Chained conditionals
Nested
conditionals
• Conditions
change the flow
of program
execution
• if statement is
used in conditions
Syntax:
if condition:
statement 1
…
• Two possibilities
(else is used)
Syntax:
if condition:
statement 1
…
else:
statement 2
…
• More than two
possibilities.
• elif is used.
Syntax:
if condition:
statement 1
elif condition:
statement 2
elif condition:
statement 3
else:
statement 4
• Another
Condition in one
condition
Syntax:
if condition1:
if condition2:
statement 1
else:
statement 2
else:
statement 3
19. Strings and text files
Control statements
strings and number
system
String manipulations
20. Loops (for , while)
Control
statements
• A loop is a programming
structure that repeats a sequence
of instructions until a specific
condition is met.
• Each repetition of the action is
known as a pass or an iteration.
• Two main loop statements are
available.
• for
• while
21. Loops (for , while)
Control
statements
• for: Executes a sequence of
statements multiple times and
reduces the code that manages the
loop variable.
22. Loops (for , while)
Control
statements
Loop Header end with colon (:)
body must be indented
24. Loops (for , while)
Control
statements
Using else Statement with for
Loop:
• If the else statement is used with
a for loop, the else statement is
executed when the loop has
exhausted iterating the list.
25. Loops (for , while)
Control
statements
• A while loop statement in Python
programming language
repeatedly executes a target
statement if a given condition is
true.
28. Loops (for , while)
Control
statements
Using else Statement with while
Loop:
• If the else statement is used with
a while loop, the else statement is
executed when the condition
becomes false.
29. Loops (for , while)
Control
statements
Infinite Loop:
• A loop becomes infinite loop if a
condition never becomes FALSE.
• When using while loops because
of the possibility that this
condition never resolves to a
FALSE value.
• This results in a loop that never
ends. Such a loop is called an
infinite loop.
30. Loops (for , while)
Control
statements
Loop Control Statements:
• Loop control statements change
execution from its normal
sequence.
• Python supports the following
control statements.
o break
o continue
o pass
break: Terminates the loop statement
and transfers execution to the
statement immediately following the
loop.
continue: Causes the loop to skip the
remainder of its body and
immediately retest its condition prior
to reiterating.
pass: when a statement is required
syntactically but you do not want
any command or code to execute.
31. Loops (for , while)
Control
statements
break: Terminates the loop statement
and transfers execution to the
statement immediately following the
loop.
continue: Causes the loop to skip the
remainder of its body and
immediately retest its condition prior
to reiterating.
pass: when a statement is required
syntactically but you do not want
any command or code to execute.
32. Loops (for , while)
Control
statements
break: Terminates the loop statement
and transfers execution to the
statement immediately following the
loop.
continue: Causes the loop to skip the
remainder of its body and
immediately retest its condition prior
to reiterating.
pass: when a statement is required
syntactically but you do not want
any command or code to execute.
33. Loops (for , while)
Control
statements
break: Terminates the loop statement
and transfers execution to the
statement immediately following the
loop.
continue: Causes the loop to skip the
remainder of its body and
immediately retest its condition prior
to reiterating.
pass: when a statement is required
syntactically but you do not want
any command or code to execute.
37. Loops (for , while)
Control
statements
Nested Loop:
• Python programming language
allows to use one loop inside
another loop.
38. Loops (for , while)
Control
statements
Nested Loop:
• Python programming language
allows to use one loop inside
another loop.
39. Strings and text files
Control statements
strings and number
system
String manipulations
40. Short-Circuit Evaluation
Control
statements
• The Python virtual machine
sometimes knows the value of a
Boolean expression before it has
evaluated all its operands.
• For instance, in the expression A
and B, if A is false, then so is the
expression, and there is no need to
evaluate B
• Likewise, in the expression A or B, if A
is true, then so is the expression, and
again there is no need to evaluate B.
• This approach, in which evaluation
stops as soon as possible, is called
short-circuit evaluation.
41. UNIT 2 : SYLLABUS
• Conditions, Boolean logic, logical operators; (Refer UNIT 1: Operators)
• ranges
• Control statements: if-else, loops (for, while); short-circuit (lazy) evaluation
• Strings and text files; manipulating files and directories, os and sys
modules; text files: reading/writing text and numbers from/to a file;
creating and reading a formatted file (csv or tab-separated).
• String manipulations: subscript operator, indexing, slicing a string;
• strings and number system: converting strings to numbers and vice versa.
Binary, octal, hexadecimal numbers