This document provides an introduction to type conversion, arrays, lists, and control flow in Python. It discusses:
1) Type conversion between integers, strings, and lists using functions like str(), int(), and list().
2) The differences between arrays and lists in Python, including how arrays store elements contiguously in memory while lists use pointers.
3) Control flow statements like conditionals (if, if-else, if-elif-else) and loops (for, while) and how they determine the order of statement execution.
4) The range function and how it can generate sequences of numbers for use in for loops.
This document provides an introduction to type conversion, arrays, lists, and control flow in Python. It discusses:
1) Type conversion between integers, strings, and lists using functions like str(), int(), and list().
2) The differences between arrays and lists in Python, including how arrays store elements contiguously in memory while lists use pointers.
3) Control flow statements like conditionals (if, if-else, if-elif-else) and loops (for, while) and how they determine the order of statement execution.
4) The range function and how it can generate sequences of numbers for use in for loops.
This document provides an introduction to type conversion, arrays, lists, and control flow in Python. It discusses:
1) Type conversion between integers, strings, and lists using functions like str(), int(), and list().
2) The differences between arrays and lists in Python, including how arrays store elements contiguously in memory while lists use pointers.
3) Control flow statements like conditionals (if, if-else, if-elif-else) and loops (for, while) and how they determine the order of statement execution.
4) The range function and how it can generate sequences of numbers for use in for loops.
(CSEN 3135) Module 1: Introduction to Python Lecture 4: 26/10/2021
Dr. Debranjan Sarkar
Type conversion • str(69) = “69” # conversion from integer to string • int(“854”) = 854 # conversion from string to integer • int(“3A”) => Error # string is not a decimal number • range(0,n) generates the sequence 0, 1, 2, …, n-1 • Sequence produced by the range() function is not of type list • So we use list function to convert this sequence into type list • list(range(0,5)) = [0,1,2,3,4] • list function is also used to convert a string into list • list(‘abcdef’) will produce the list [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’] Arrays 57C A[0] 57E A[1] 580 A[2] • A sequence of values can be stored as an array or as a list 582 A[3] • Features of an array: 584 A[4] • Contiguous area of memory for storing the consecutive elements of an array • Elements are of uniform type (int/ float etc.) so that each element occupies same number of bytes (or words) in memory • Typically size of the sequence (number of elements) is fixed in advance • Array indexing is fast and accessing ith sequence for all i, takes constant time • Offset is computed from the start of memory block • Word address of the 1st element of the array = s, each element requires n words (say) • Then the word address of the ith element of the array is (s + (i-1)*n) • In the above example, s = 57C, n = 2 => word address of the 4th element = 57C + (4-1)*2 = 582 • Insertion of an element between seq[n] and seq[n+1] is expensive • Deletion of an element in the sequence is also expensive 57C A[0] 57E A[4] Lists 580 A[2] 582 A[1] • Features of a list: 584 A[3]
• Consecutive elements need not be stored in contiguous area of memory
• It is like data structure linked-list where in each element there is a pointer to the next element and for the last element, the pointer to the next element is Null • Elements of a list may be non-uniform type (int/ string etc.) and have flexible size • Size of the sequence (number of elements) is not to be fixed in advance, as the last element of a list contains a Null pointer to indicate that this is the last element • To access the ith sequence, i number of links have to be traversed and so the time is proportional to i • Insertion and deletion of an element is easy and less expensive Array vs List • Exchange Operations (exchange the values of seq[i] and seq[j]) • Takes constant time in array` • Takes linear time in list • Delete seq[i] or insert value v after seq[i] • Constant time in list as we are already at seq[i] and we only have to shift some links • Linear time in array as the elements before or after the seq[i] are to be shifted • So the algorithms for a sequence of values represented as an array may not work well for data structure list Control Flow : Conditionals • Statements of a Python program are executed normally from top to bottom • Control flow determines the order in which the statements are to be executed • A common task is to choose between two or more alternative possibilities depending upon the outcome of a test • Each alternative is called a conditional • Conditional execution (if) if condition: # condition returns True or False, ‘:’ indicates end of condition Statement1 Statement2 # Uniform indentation demarcates the body of ‘if’ Statement3 # No curly brackets (braces) required as in C • Statements 1 and 2 are executed when the condition is True • Statement 3 is executed unconditionally (i.e. irrespective of whether condition is True or False) Control Flow: Conditionals • Conditional execution (if-else) if condition: Statement1 …….. else: Statement2 …….. Statement3 ………… • Statement 1 … are executed when the condition is True • Statement 2 … are executed when the condition is False • Shortcut for conditions • If a number is 0, it is as good as False • Empty sequence (Null string “” or empty list []) is treated as False • Everything else is True • Example: if m != 0: is equivalent to if m: Control Flow: Conditionals • Multiway branching (if-elif-else) if condition1: Statement1 ……………… elif condition2: Statement2 ……………… elif condition3: Statement3 ……………… else: Statement4 ……………… Control Flow (Loops or Iteration) • Repeat some statements for a fixed number of times • for i in [1,3,5,7,9]: sq = i * i # indentation for the body of the ‘for’ statement double = i * 2 print(‘i = ‘, i, ‘square = ‘, sq, ‘double = ‘, double) • for i in [0,1,2,3,4,5,6,7,8,9]: for i in range(10): …………… …………… • range(m,n) produces the sequence m,(m+1),(m+2), …, (n-1) • range(n) produces the sequence 0, 1, 2, …, (n-1) • range(m,n,k) produces the sequence m,(m+k),(m+2k), …, (m + l.k) such that m + lk < n <= m + (l+1)k • When k is negative, there will be a countdown • range(m,n,-1) produces the sequence m,(m-1),(m-2), …, (n + 1) [m > n] More on range function • Example: Find the sum of first 10 natural numbers • n = 10 • sum = 0 • for i in range(1,n+1): • sum = sum + i • print(“sum = “, sum) • ================================== • # Compare range and list • Is range(0,5) == [0,1,2,3,4] ? • Answer: In Python 2 ➔ YES In Python 3 ➔ NO • list(range(0,5)) == [0,1,2,3,4] in Python 3 # Type conversion Control Flow (while) • Number of iterations of a loop is not known in advance • Repeat some statements until a condition becomes False • i=0 • while i <= 9: • sq = i * i • double = i * 2 • print(‘i = ‘, i, ‘square = ‘, sq, ‘double = ‘, double) • i=i+1 # indentation for the body of the ‘while’ statement • Condition should be made False inside the body of the statement, otherwise the loop will become an infinite loop Thank you