0% found this document useful (0 votes)
7 views28 pages

Master Python Branching, Slicing, and Loops Simplified For All Learners

Uploaded by

uzairznkhattak
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 views28 pages

Master Python Branching, Slicing, and Loops Simplified For All Learners

Uploaded by

uzairznkhattak
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/ 28

Computer Programming- CEN1008

Assignment, Branching, Slicing, and Loops

Lecture 4 & 5

1
RECAP Lecture 3

q OBJECTS
q EXPRESIONS
q OPERATORS ON ints and floats
q BINDING VARIABLES AND VALUES
q CHANGING BINDINGS
q STRINGS
OBJECTS
EXPRESIONS
OPERATORS ON ints and floats
BINDING VARIABLES AND VALUES
CHANGING BINDINGS
STRINGS

hi = "hello there"
name = "batch23 students"
action="Wake up"
greet = hi + name
print(greet)

silly = hi + " " + name + (" " + action)*3


INPUT/OUTPUT: print
INPUT/OUTPUT: input(“ “)
RESERVED WORDS OR KEY WORDS
and, as, assert, break,
class, continue, def, del, elif,
else, except, exec, finally, for,
from, global, if, import, in, is,
lambda, not, or, pass, print, raise,
return, try, with, while, and yield

They have built-in functionality !

import keyword
print (keyword.kwlist)
PYTHON ALLOWS MULTIPLE ASSIGNMENT

• x, y = 2, 3
• binds x to 2 and y to 3
• All of the expressions on the right-hand side
of the assignment are evaluated before any
bindings are changed
• Can we swap the bindings of 2 variables ????
Python allows multiple assignment

Can we swap the bindings of 2 variables ????

For example, the code


x, y = 2, 3
x, y = y, x
print 'x =', x
print 'y =', y

What would be the output?


BRANCHING PROGRAM

• The kinds of computations we have been looking at thus far are


called straight-line programs
• They execute one statement after another in the order in which
they appear and stop when they run out of statements.

• Branching programs are more interesting. The simplest branching


statement is a conditional.

• A conditional statement has three parts:


1) a test, i.e., an expression that evaluates to either True or False;
2) a block of code that is executed if the test evaluates to True;
and
3) an optional block of code that is executed if the test evaluates
to False.
BRANCHING PROGRAM
BRANCHING PROGRAM
NESTED CONDITIONS

When either the true block or the false block of a conditional


contains another conditional, the conditional statements are said
to be nested
Example Nested if condition (bugs) Example nested if condition
x=3 x=3
y=2 y=2
if x > y:
print ("you win") if x > y:
if x==1: print("you win")
, print ("100 rps") if x==3:
else: print ("100 rps")
print ("10 rps") else:
else: print ("10 rps")
print("you lose") else:
print("you lose")

IndentationError: unindent does not match any outer indentation level


IndentationError: unexpected indent
INDENTATION

Example indentation problem


x=2
if x%2 == 0:
print ("Even")
else:
print ("Odd")
print ("Done with conditional")

Q. Correct indentation errors?


Q. What will be the output?
INDENTATION

Example indentation problem


x=2
if x%2 == 0:
print ("Even")
else:
print ("Odd")
print ("Done with conditional")

Output:

Even
Done with conditional
NESTED CONDITION AND ELIF
#Example nested if condition and elif and comments
#----- Initializing variables-------
x=3
y=2
#----------First condition----------------
if x < y:
print("you win")
if x==3:
print ("100 rps")
else:
print ("10 rps")
#-----------Nested condition ends here ---
#-----------Second Condition-------------
elif x == y:
print("second condition")
#---------------second condition ends here---
#-----------third Condition-------------
elif x > y:
print("third condition")
#---------------second condition ends here---
else:
print("you lose")
# -----------First condition ends here------
print("multiple conditions")
COMPARISON OPERATORS
LOGICAL OPERATORS ON bools
LENGTH AND INDEXING

• The length of a string can be found using the len function. For example, the
• value of len('abc') is 3.

• Indexing can be used to extract individual characters from a string.


• In Python, all indexing is zero-based. For example, typing 'abc'[0] into the
interpreter will cause it to display the string 'a’.
• Typing 'abc'[3] will produce the error message
Index Error: string index out of range.
Since Python uses 0 to indicate the first element of a string, the last
element of a string of length 3 is accessed using the index 2.
• Negative numbers are used to index from the end of a string.
• For example, the value of 'abc'[-1] is 'c'.
SLICING

Slicing is used to extract substrings of arbitrary length. If s is a


string, the expression s[start:end] denotes the substring of s that
starts at index start and ends at index end.

For example, “abc” [1:2] is “bc'.


= vs ==
ITERATION (LOOPING)
• A generic iteration (also called
looping) mechanism is depicted in
Figure.
• Like a conditional statement it
begins with a test.
• If the test evaluates to True, the
program executes the loop body
once, and then goes back to
reevaluate the test.
• This process is repeated until the
test evaluates to False, after which
control passes to the code
following the iteration statement.

You might also like