Iteration in Python PYTHON by Ravinder Nath Rajotiya - April 19, 2022April 22, 20220 Iteration- for and while loop: Iterations are used for repetitive execution of the same block of code over and over. Loop or iteration are popular block of code in all the programming language like basic, PASCAL, C, JAVA, PHP and all others. They can be numeric or collection based. Type of Iteration There are two types of iteration: a) Definite iteration, in which the number of repetitions is specified explicitly in advance. This is implemented using the for loop. b) Indefinite iteration, in which the code block executes until some condition is met. This type of iteration is implemented using the while loop How to check if an object is iterable? we can check whether an object is itrable r not by passing it to the
Tuple in Python PYTHON by Ravinder Nath Rajotiya - March 12, 2022March 20, 20220 Tuples Tuples in Python are similar to lists. A tuple is a sequence of comma separated numbers. A tuple can be decared as a sequence by enclosing or without enclosing them inside the () bracket and all elements are separated with commas. Elements in a tuple can be accessed via unpacking or indexing. Following are the two ways to declare the tuple in Python. a). Declaring and accessing tuple using indexing: >>>tup=(3,4,5,6,7) Or >>> tup=3,4,5,6,7 >>>tup1 (3,4,5,6,7) >>>tup[2] 5 >>>tup(3) 6 Since the tuple is immutable, A tuple does not have the append () or insert () method, nor can it be assigned to another element. >>>tup1=(3,4,5,6,7) >>>tup1[0]= 2 will give an error. TypeError: 'tuple' object does not support item assignment Because a tuple is unchangeable, the code is more secure. Therefore,
Lists in Python PYTHON by Ravinder Nath Rajotiya - April 19, 2019March 12, 20220 Lists List is the most common composite data type in Python. It is is an ordered set of elements. Elements in a list can be of similar or different types. Declaration: A list in python is enclosed inside [ ] bracket. Elements in the list are separated by comma. Example of a list is: list1 = [8,6,7,4,5,3] When a list is created in Python, the interpreter creates a data structure in memory that is similar to an array (but not an array) to store it. The numbering in the list starts with 0, then 1, and so on, if accessed from the end indexing starts from -1, -2 and so on. myList = [3,2,6,4,9,’a’, ‘b’,’c’,‘ram’,’s’] myList 3 2 6 4 ‘a’ ‘b’ ‘c’ ‘ram’ ‘s’ Index from begining 0 1 2 3 4 5 6 7 8 Indexing from end -9 -8 -7 -6 -5 -4 -3 -2 -1 Real-Time Usage of the List The list data