Posts

Showing posts from November, 2020

Python3 Tuples DS

Python Data Structures : Tuple  Data Structures : Tuple  Tuples    ->A tuple is similar to list.    ->The difference between the two is that we can't change the elements of the tuple(immutable) once it is assigned whereas in the list, elements can be changed. Tuple Creation : Eg :              #empty tuple        t=( )                #tuple having integers        t=(1,2,3)        print(t)          #tuple with mixed data types        t=(1,'rama',2.5,'a')        print(t)         #nested tuple        t=(1,(1,2),[1,'abc',3])        print(t) output:: (1,2,3)              (1,'rama',2.5,'a')              (1,(1,2),[1,'abc',3])         #only parentheses is not enough            t=('raja')            type(t) output: : str         # need a comma at the end             t=('raju',)             type(t)  output:: tuple        #paranthesis is optional               t="raja",              print(type(t))              

Python3 Lists DS

Python : Data Structures Data Structure Lists   Data structure:        A data structure is a collection of elements(such as numbers or character-or even other data structures) that is structured in some way, for example, by numbering the elements. The most basic data structure in Python is the "sequence". ->List is one of the sequence data structure ->Lists are collection of items(strings, integers or even other lists) ->Lists are enclosed within [ ] ->Each item in the list has an assigned index value. ->Each item in a list is separated by a comma( , ) ->Lists are mutable, which means they can be changed. List creation : Eg:        emptyList=[ ]        lst=['one', 'two', 'three']            #list of strings        lst2=[1,3,2,4]                            #list of integers        lst3=[[1,2],[3,4],5,6]]              #list of lists        lst4=[1,'krishna',4,6.5]             #list of different datatypes        print(lst4) outp

Python3 break and continue Statements

Image
Python break and continue Statements break and continue statements              In Python , break and continue statements can alter the flow of normal loop. Loop iterate over a block of code until test expression is false, but sometimes we wish to terminate the current iteration or even the whole loop without checking test expression.   The break and continue statements are used in these cases. Python Break Statement     Syntax:                 break Flow Chart:                                         Working:                     Eg:        nums=[1,2,3,4]        for num in nums:              if num==4:                   break              print(num)        else:              print("in the else block")        print("outside of for loop")   Output:: 1                        2                        3                        outside of for loop     Python program to check given number is prime number or not (using break):        num=int(input("enter a number: &quo

Python3 for Loop

Image
Python For Loop for Loop:         The for loop is used to iterate over a sequence (list, tuple, string) or other iterable objects.      Iterating over a sequence  is called traversal. Syntax:           for element in sequence:                   body of for Here, elements is the variable that takes the value of the item inside the sequence on each iteration. Loop continues until we reach the last item in the sequence. Flow chart:                                        Eg:        lst=[10,20,30,40,50]        product=1        for ele in lst:               product*=ele        print("product is: {}".format(product)) output:: product is: 12000000 for more info :   https://youtu.be/yaqMSBr_NCU range() function :            We can generate a sequence of numbers using range() function. range(10) will generate numbers from 0 to 9(10 numbers). Eg:        for i in range(10):              print(i) output::     0                1                 2                3                4        

Python3 While loop

Image
Python While Loop   WHILE  Loop :               The while loop in python is used to iterate over a block of code as long as the test expression(condition) is true Syntax::            while test_expression:                 body of while    * The body of the loop is entered only if the test_expression is true.  * After one iteration, the test_expression is checked again.  * This process proceeds until the test_expression is false. Flow Chart : Eg:        lst=[10,20,30,40,50]                product=1        index=0        while index<len(lst):               product*=lst[index]               index+=1        print("Product is:{}".format(product))       output:    Product is 14400000 While loop with else:        This is same as for loop, we can use else block with while loop as well. * The else part is executed if the condition of while loop evaluates to false. The while loop can be terminated with the break statement. * In such case ,this is ignored. Hence, a while loop's

Python3 if..... else

Image
Python if .... else Statement  IF... ELSE STATEMENT :                  The if... elif.. else statements is used in Python for decision making.   if statement syntax:                    if test expression:                                    statement(s) Flow chart :      *  The program evaluates the test expression and execute the statement(s) only if the test expression is true. *  If the test expression is false, the statements is not executed. Eg:          num=25          if num=24:             print("The number is divisible by 2,3,6.")          print(" This will print always.") output:    This will print always. IF.... ELSE STATEMENT :     Syntax::                          if test expression:                                 body of if                          else:                                    body of else Flow chart :                                  Eg:           num=4           if num>0:                print("Positive number")           els