basics of python - control_structures_and_debugging.pptx

shopinderjeet 18 views 10 slides May 01, 2024
Slide 1
Slide 1 of 10
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10

About This Presentation

control structures in python


Slide Content

Control Structures

Selection Control a) if Statement :- allows branching or decision making depending upon a condition. This statement is used for executing a set of statements, only if a certain condition is true. It does nothing if the condition is false. Syntax if condition: statement(s) Note: Python uses indentation to delimit block of code, it is mandatory, program won’t work without it.

b) if-else Statement :- The if-else statement includes a condition, if it is true the statements within if block will be executed and if the condition is false, statements within else block will be executed. Syntax- if condition: statement(s) else: statement(s)

c) if- elif -else Statement :- The keyword ‘ elif ’ is short for ‘else if’ and allows to check multiple expressions. Its an substitute of switch statement found in other languages Syntax:- if condition1: statement(s) elif condition2: statement(s) else: statement(s)

Nested Conditions Nested conditions are used where we want to check for a secondary condition . If the first condition executes are true, we can have an if-else statement inside of another if-else statement. Syntax:- if condition1: if condition2: statement(s) else: statement(s) else: statement(s)

Multiple Nested if We have multiple if statements nested throughout our code. Any number of these statements can be nested inside one another. Syntax: if condition1: if condition2: statement(s) elif condition3: statement (s) else: statement(s) elif condition4: statement(s) else: statement(s)

Python Debugging With branching and looping statements, there may be chances of programming mistakes. In order to cope with these program errors, debugging is useful to trace the dynamic execution of the program. This helps in to Examine variables Set forward line by line Show code as it executes Inspection of stack frames Code evaluation Post mortem debugging

To enable debugging, the following statement imports the Python debugger module, pdb. import pdb Now find a spot where we like tracing to begin and insert the following code: pdb.set_trace() When program encounter the line pdb.set_trace(), it will start tracing. That is, it will (1) stop (2) display the current statement. (3) wait for your input

pdb commands p:Print the value of variable c: Continue execution s: Execute the current line and stop at the first possible occasion l:List source code for the current file w: displays current location in stack trace q: quit

Thanks