Unit - 4 Python Conditional and Iterative Statements
Unit - 4 Python Conditional and Iterative Statements
• o/p : 1
• 2
• 3
nested while loop
• Python programming language allows to use
one loop inside another loop.
• Syntax
• while expression:
while expression:
statement(s)
statement(s)
• i=1
• while (i<=5):
• j=1
• while(j<=i):
• print(j,end="")
• j=j+1
• print()
• i=i+1
• o/p : 1
• 12
• 123
• 1234
• 12345
In while loop break and continue
statements
• You might face a situation in which you need to exit a
loop completely when an some condition is becomes
true or there may also be a situation when you want
to skip a part of the loop and start next execution.
• Python provides break and continue statements to
handle such situations and to have good control on
your loop.
Use of break statement
• The break statement in Python terminates the
current loop and resumes execution at the
next statement after the loop.
• The most common use for break is when some
external condition is triggered requiring a
sudden exit from a loop. The break statement
can be used in both while and for loops.
• var = 10
• while var > 0:
• print ( 'Current variable value :', var)
• var = var -1
• if var == 5:
• break
• print "Good bye!“
•
•
o/p Current variable value : 10
Current variable value : 9
• Current variable value : 8
• Current variable value : 7
• Current variable value : 6
• Good bye!
Continue in while
• print(list1.index(5,2,6))
Accessing list members
• You access the list items by referring to the index number:
• Example:
• list2 = ['cat', 'bat', 'mat', 'cat‘, 'get‘, 'pet']
• Print(list2[3]) ## o/p cat
• Negative Indexing:
• Negative indexing means beginning from the end, -1 refers to the
last item, -2 refers to the second last item etc.
• o/p:
• ['Mathematics', 'chemistry', 1997, 2000, 20544,’1087’]
Clear..list
• Clear():Removes all the elements from the list
• lst = ['Hello','Python',‘c++']
• print(lst)
• lst.clear()
• print(lst)
o/p:
• ['Hello','Python',‘c++']
• []
Copy…List
• copy(): Returns a shallow copy of the list.
• lst = ['Hello', 'Python', ‘c++']
• lst1 = lst
• lst1.append('Java')
• print(lst) ## ['Hello', 'Python',
‘c++‘,’java’]
• print(lst1) ## ['Hello', 'Python', ‘c++‘,’java’]
•
• #With copy
• lst = ['Hello', 'Python', ‘c++']
• lst1 = lst.copy()
• lst1.append("Java")
• print(lst) ## ['Hello', 'Python', ‘c++‘]
• print(lst1) ## ['Hello', 'Python', ‘c++‘,’java’]
Count…list
• count():
• Returns the number of elements with the
specified value.
• lst = ['Hello', 'Python', ‘java','Python']
• print(lst.count("Python")) ## 2
• print(lst.count(“java")) ## 1
• print(lst.count(" ")) ## 0
Pop..List
• Pop():Removes the element at the specified position
• Syntax: list.pop() :it removes last element
• list.pop(3): it removes third index element (4th)
• lst = ['CPlusPlus', 'Hello', 'Python', 'Java', ‘java', 'Python']
• print(lst)
• #Without index lst.pop()
• print(lst)
• #With Index
• lst.pop(3)
• print(lst)
Remove…List
• Remove(): It removes the element from the
list
• Syntax: list.remove(obj)
• Eg:lst = ['CPlusPlus', 'Hello', 'Python', 'Java']
• lst.remove(“hello”)
• print(lst) ## o/p = ['CPlusPlus', 'Python', 'Java']
Reverse …list
• Reverse(): It will reverse the whole list
• Syntax: list.reverse()
• Eg:lst = ['CPlusPlus', 'Hello', 'Python', 'Java']
• lst.reverse()
• print(lst) ## [‘java’,Python’,’Hello’,'CPlusPlus‘]
Sort…List
• Remove Items
• Note: You cannot remove items in a tuple.
• Tuples are unchangeable, so you cannot remove items from it, but
you can use the same workaround as we used for changing and
adding tuple items:
• Example
• Convert the tuple into a list, remove “Tom", and convert it back into
a tuple:
• Thistuple= (“Tom", “Dick", “Harry")
y = list(thistuple)
y.remove(“Tom")
thistuple = tuple(y)
• Delete tuple
• thistuple = (“Tom", “Dick", “Harry") del thistuple
print(thistuple)
• Tuple Methods
• Python has two built-in methods that you can use on
tuples.
• count() Returns the number of times a specified value
occurs in a tuple
• thistuple = ("Tom","Dick","Harry","Tom")
• print(thistuple.count('Tom'))
• index() Searches the tuple for a specified value and returns
the position of where it was found
thistuple = (“Tom", “Dick", “Harry")
• print(thistuple.index(‘Dick’) ## case sensitive
• o/p=1
Set
Sets: declaring set, access set data, set methods (add, clear, copy,
discard, pop, remove, union, update).
• Access Set:
• You cannot access items in a set by referring to an index or a key.
• But you can loop through the set items using a for loop, or ask if a
specified value is present in a set, by using the in keyword.
• Example
• Loop through the set, and print the values:
• thisset = {"apple", "banana", "cherry"}
for x in thisset:
print(x)
• Add Items
• Once a set is created, you cannot change its items, but you can add new items.
• To add one item to a set use the add() method.
• Example
• Add an item to a set, using the add() method:
• thisset = {"apple", "banana", "cherry"}
thisset.add("orange")
print(thisset)
• Add Sets
• To add items from another set into the current set, use the update() method.
• Example
• Add elements from tropical and thisset into newset:
• thisset = {"apple", "banana", "cherry"}
tropical = {"pineapple", "mango", "papaya"}
thisset.update(tropical)
print(thisset)
• Remove Item
• To remove an item in a set, use the remove(), or
the discard() method.
• Example
• Remove "banana" by using the remove() method:
• thisset = {"apple", "banana", "cherry"}
thisset.remove("banana")
or
• thisset.discard("banana")
print(thisset)
• Union
• Two sets can be merged using union() function
or | operator.
• people = {"Jay", "Idrish", "Archil"}
• vampires = {"Karan", "Arjun"}
• dracula = {"Deepanshu", "Raju"}
• population = people.union(vampires)
• o/p {'Idrish', 'Jay', 'Karan', 'Archil', 'Arjun'}
• population = people|dracula
• o/p {'Idrish', 'Jay', 'Archil', 'Raju', 'Deepanshu'}
• Clearing sets
• Clear() method empties the whole set.
•
• set1 = {1,2,3,4,5,6}
•
• print("Initial set")
• print(set1)
•
• set1.clear()
•
• print("\nSet after using clear() function")
• print(set1)
• Output:Initial set {1, 2, 3, 4, 5, 6}
• Set after using clear() function set()
• Pop(): This method removes a random element from
the set and returns the removed element.
• people = {"Jay", "Idrish", "Archil"}
• People.pop()
• Print(people)
• Update():The update() method updates the current
set, by adding items from another set (or any other
iterable).
• x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.update(y)
print(x)
• o/p {'apple', 'banana', 'cherry', 'microsoft', 'google'}