Unit 2 Srinidhi H Asst Professor 1
Iterations 2
Python for Loop Statements for iterating_var in sequence: statements(s) 3
Python for While Statements while «expression»: «block» 4
The Collatz 3n + 1 sequence 5
The Collatz 3n + 1 sequence 6
LISTS 7
LISTS A list is an ordered collection of values. The values that make up a list are called its elements, or its items. Lists and strings and other collections that maintain the order of their items are called sequences. 8
To create a list The simplest is to enclose the elements in square brackets [ and ]: The elements of a list don t have to be the same type. 9
Memory model of a list 10
Accessing elements The syntax for accessing the elements of a list the index operator: [] Remember that the indices start at 0: 11
List length The function len returns the length of a list, which is equal to the number of its elements. If you are going to use an integer index to access the list, it is a good idea to use this value as the upper bound of a loop instead of a constant. 12
List Concatenation 13
List slices a[start:end] # items start through end-1 a[start:] # items start through the rest of the array a[:end] # items from the beginning through end-1 a[:] # a copy of the whole array a[start:end:step] # start through not past end, by step 14
List slices 15
List deletion 16
Objects and references There are two possible ways the Python interpreter could arrange its memory: 17
Objects and references- Try this?? 18
Aliasing If we assign one variable to another, both variables refer to the same object. Because the same list has two different names, a and b, we say that it is aliased. Changes made with one alias affect the other: 19
Lists are mutable Lists are mutable, which means we can change their elements. Using the index operator on the left side of an assignment, we can update one of the elements: >>> fruit = ["banana", "apple", "quince"] >>> fruit[0] = "pear" >>> fruit[2] = "orange" >>> fruit [ pear, apple, orange ] 20
Aliasing in Lists and Strings For mutable objects such as Lists aliasing might result in inconsistent values. For immutable objects such as Strings aliasing is helpful in copying value of one variable to another. 21
Cloning lists If we want to modify a list and also keep a copy of the original, we need to be able to make a copy of the list itself, not just the reference. This process is sometimes called cloning. The easiest way to clone a list is to use the slice operator: 22
Cloning lists Now we are free to make changes to b without worrying that we ll inadvertently be changing a: 23
List parameters Passing a list as an argument actually passes a reference to the list, not a copy or clone of the list. So parameter passing creates an alias: caller has one variable referencing the list. the called function has an alias. but there is only one underlying list object. 24
List parameters- Program Write a function to initialize a list as things=[1,2,3,4] and return the list as things=[2,4,6,8] 25
List methods 26
List methods- append 27
List methods- example 28
Pure functions and modifiers Functions which take lists as arguments and change them during execution are called modifiers and the changes they make are called side effects. A pure function does not produce side effects. It communicates with the calling program only through parameters, which it does not modify, and a return value. 29
Pure functions example Pure function Main function 30
Nested lists A nested list is a list that appears as an element in another list. In this list, the element with index 3 is a nested list: >>> nested = ["hello", 2.0, 5, [10, 20]] >>> print(nested[3]) [10, 20] To extract an element from the nested list, we can proceed as: 31 OR
Matrices Nested lists are often used to represent matrices. For example, the matrix might be represented as: >>> mx = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 32
Strings 33
Strings >>> ss = "Hello, World!" >>> tt = ss.upper() >>> tt HELLO, WORLD! 34
Working with the parts of a string The indexing operator (Python uses square brackets to enclose the index) selects a single character substring from a string: >>> fruit = "banana" >>> m = fruit[1] >>> print(m) 35
Traversal ix = 0 while ix < len(fruit): letter = fruit[ix] print(letter) ix += 1 prefixes = "JKLMNOPQ" suffix = "ack" for p in prefixes: print(p + suffix) The output of this program is: Jack Kack Lack Mack Nack Oack Pack Qack 36
Slices A substring of a string is obtained by taking a slice. >>> s = "Pirates of the Caribbean" >>> print(s[0:7]) Pirates >>> print(s[11:14]) the >>> print(s[15:24]) Caribbean 37
Program Check whether a given string is palindrome or not using Slicing. 38
Strings are immutable Strings are immutable, which means you can t change an existing string. 39
The in and not in operators The in operator tests for membership. When both of the arguments to in are strings, in checks whether the left argument is a substring of the right argument. 40
Program Write a program using functions that takes a string and removes all the vowels from it. 41
Program 42
The split method It splits a single multi-word string into a list of individual words, removing all the whitespace between them. 43