Posts

Showing posts from April, 2022
 class and object class Person: def __init__ ( self , name , age): self .name = name self .age = age def myfunc ( self ): print ( "Hello my name is " + self .name) p1 = Person( "John" , 36 ) p1.myfunc()
 LARGEST NUMBER IN A LIST list = [ 22 , 44 , 56 , 77 , 89 , 43 , 740 ] list.sort() print ( "the smallest number in a list is:" , list[ 0 ])
 clear utkarsh = [ 1 , 2 , 3 , 4 ] print ( 'utkarsh before clearing:' , utkarsh ) utkarsh . clear () print ( 'utkarsh after clearing:' , utkarsh )
 CHECKING IF ELEMENTS EXIST IN LISTđź‘Ź test_list = [ 1 , 6 , 3 , 5 , 3 , 4 ]   print ( "Checking if 4 exists in list ( using loop ) : " )   for i in test_list :     if ( i == 4 ) :         print ( "Element Exists" )   print ( "Checking if 4 exists in list ( using in ) : " )   USING IN if ( 4 in test_list ):     print ( "Element Exists" )    
 SWAP POSITION IN LISTđź‘€ # Python3 program to swap elements # at given positions # Swap function def swapPositions(list, pos1, pos2): list[pos1], list[pos2] = list[pos2], list[pos1] return list # Driver function List = [23, 65, 19, 90] pos1, pos2 = 1, 3 print(swapPositions(List, pos1-1, pos2-1))
EVEN NUMBER IN THE LIST # Python program to print Even Numbers in a List # list of numbers list1 = [ 10 , 21 , 4 , 45 , 66 , 93 ] # iterating each number in list for num in list1 :     # checking condition     if num % 2 == 0 :         print ( num , end = " "