Posts

Showing posts from February, 2021

password own code

  ## ascii value (A)-65 to (Z)-90 a= 'A' print ( ord (a)) import random omit=[ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' , 'k' , 'l' , 'm' , 'n' , 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' , 'y' , 'z' ] letters=[ chr (i) for i in range ( 35 , 125 ) if chr (i) not in omit] random.choice(letters) print (random.choice(letters)) ### will choose random chr password= "" length= int ( input ( "enter the length" )) for i in range ( 0 , length): password=password+random.choice(letters) print (password) "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Scripts\python.exe" "C:/Users/darul Haram/PycharmProjects/pythonProject2/venv/Lib/password own code.py" 65 ] enter the length

Raising Error

  a= input ( "enter a number" ) if a.isdigit(): print ( "it is a number" ) else : raise ValueError "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Scripts\python.exe" "C:/Users/darul Haram/PycharmProjects/pythonProject2/venv/Lib/Raising Errors.py" enter a numberhj Traceback (most recent call last): File "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Lib\Raising Errors.py", line 5, in <module> raise ValueError ValueError Process finished with exit code 1

Exception Handling

  ## try, except , else and finally try : x= 10 / 0 except ZeroDivisionError : print ( "10/0 is impossible" ) else : print ( "no error" ) ## other example try : a= 6 a.lower() except AttributeError : print ( "int value doesnt have lower() method" ) else : print ( "no error" ) "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Scripts\python.exe" "C:/Users/darul Haram/PycharmProjects/pythonProject2/venv/Lib/Exception Handling.py" 10/0 is impossible int value doesnt have lower() method Process finished with exit code 0

Handling exception

  ## try , except, else, finally ## this is structure:- try : x= 10 / 0 except ZeroDivisionError : print ( "zero cant divide any number" ) else : print ( "no error" ) "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Scripts\python.exe" "C:/Users/darul Haram/PycharmProjects/pythonProject2/venv/Lib/Handling exceptions.py" zero cant divide any number Process finished with exit code 0

Overwriting(OOP)

  ##by ineheritance we can make available the object of other #in object of other class :-inheritance(think) class animal: def __init__ ( self , colour): self .colour=colour def introduce ( self ): print ( "i am an animal" ) def makesound ( self ): print ( "making sound" ) class dog(animal): def makesound ( self ): print ( "bow bow" ) def takecare ( self ): print ( "i am taking care of you" ) class cat(animal): def makesound ( self ): print ( "meow meow" ) wild_animal = animal( "red" ) puppy=dog( "white" ) kitten=cat( "white" ) puppy.makesound() ##object is use to call a method. kitten.makesound() "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Scripts\python.exe" "C:/Users/darul Haram/PycharmProjects/pythonProject2/venv/Lib/Overwriting(OOP).py" bow bow meow meow Process finished with exit code 0

Inheritance(OOP)

##by ineheritance we can make available the object of other #in object of other class :-inheritance(think) class animal: def __init__ ( self , colour): self .colour=colour def introduce ( self ): print ( "i am an animal" ) def makesound ( self ): print ( "making sound" ) class dog(animal): def takecare ( self ): print ( "i am taking care of you" ) wild_animal=animal( "brown" ) puppy=dog( "white" ) wild_animal.makesound() puppy.introduce()   ##obejct is use to call a method, here puppy is object and introduce is method "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Scripts\python.exe" "C:/Users/darul Haram/PycharmProjects/pythonProject2/venv/Lib/inheritance(OOP).py" making sound i am an animal Process finished with exit code 0  

Methods(OOP)

  class car: def __init__ ( self , mileage , colour): self .mileage=mileage self .colour=colour def getmileage ( self ): return self .mileage def getcolour ( self ): return self .colour def greet ( self ): print ( "hello i am a car" ) model1=car( 60 , "red" ) model2=car( 80 , "white" ) print (model1.getmileage()) print (model2.getmileage()) model2.greet() model1.greet() "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Scripts\python.exe" "C:/Users/darul Haram/PycharmProjects/pythonProject2/venv/Lib/methods(OOP).py" 60 80 hello i am a car hello i am a car Process finished with exit code 0

complex numbers(OOP)

  ## complex number:- x+iy class complexnumber: def __init__ ( self , real , img): self .real=real self .img=img def __str__ ( self ): return "{}+{}i" .format( self .real , self .img) def __add__ ( self , other): x= self .real+other.real y= self .img+other.img return "{}+{}i" .format(x , y) num1=complexnumber( 4 , 5 ) num2=complexnumber( 10 , 27 ) print (num1+num2) "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Scripts\python.exe" "C:/Users/darul Haram/PycharmProjects/pythonProject2/venv/Lib/complex number(OOP).py" 14+32i Process finished with exit code 0

object oriented programming(initiation)

  ## creating an object of a class :-instantiation. ## in other words instantiating a class class students: def __init__ ( self , name , age): ##constructor definition self .sname=name ##self refers to current object(student) self .sage=age print ( "my name is {} and my age is {}" .format( self .sname , self .sage)) ##self will refer to the current object student1=students( "Aamir" , 25 ) student2=students( "Noor" , 28 ) ##object name =class name() student3=students( "Danish" , 22 ) student4=students( "qruzz" , 23 ) print (student1) print (student2) print (student3) print (student4) "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Scripts\python.exe" "C:/Users/darul Haram/PycharmProjects/pythonProject2/venv/Lib/OOP 1(initiation).py" my name is Aamir and my age is 25 my name is Noor and my age is 28 my name is Danish and my age is 22 my name is qruzz and my age is 23 <__...

OOP(object oriented programming)

  ## creating an object of a class :-instantiation. ## in other words instantiating a class class students: pass student1=students() student2=students() student3=students() student4=students() print ( type (student1)) print ( type (student2)) print ( type (student3)) print ( type (student4)) "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Scripts\python.exe" "C:/Users/darul Haram/PycharmProjects/pythonProject2/venv/Lib/OOP.py" <class '__main__.students'> <class '__main__.students'> <class '__main__.students'> <class '__main__.students'> Process finished with exit code 0 ## creating an object of a class :-instantiation. ## in other words instantiating a class class students: pass student1=students() student2=students() ####(remember) :-object name =class name () student3=students() student4=students() print (student1) print (student2) print (student3) print (student4) "C:\Users\darul Ha...

creating module

  ##building modules def add (*x): return sum (x) def sub (*x): result=x[ 0 ] for i in x[ 1 :]: result=result-i return result def multiply (*x): result= 1 for i in x: result=result*i return result def divide (*x): result= 1 for i in x: result=result/i return result import calc ## you'll have to create a module (calc) first of all to run this. print (calc.add( 10 , 20 , 30 , 40 , 50 )) print (calc.sub( 10 , 20 , 30 )) print (calc.multiply( 1 , 2 , 3 , 4 , 5 , 7 , 8 , 9 )) print (calc.divide( 1 , 2 , 5 , 5 , 5 , 5 )) "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Scripts\python.exe" "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Lib\created module.py" 150 -40 60480 0.0008 Process finished with exit code 0

Useful functions [mapping(with code and by inbuild function) , zip and filter.]

  ##maping:-[1,2,3,4,5] :-[1,4,9,16,25] ##firstly lets do it by coding, then by inbuild function. square=[] numbers=[ 1 , 2 , 3 , 4 , 5 ] for i in numbers: z=i*i square.append(z) print ( "square:-" + "with code" + str (square)) print ( "with function:-" + str ( list ( map ( lambda x:x*x , [ 1 , 2 , 3 , 4 , 5 ])))) ##concatenating two list list1=[ 1 , 2 , 3 , 4 , 5 , 6 ] list2=[ 7 , 8 , 9 , 10 , 11 ] print (list1+list2) ##combining two lists as a tuple print ( list ( zip (list1 , list2))) ##filter:-filter elements in a list print ( list ( filter ( lambda a:a% 2 == 0 , [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 ]))) "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Scripts\python.exe" "C:/Users/darul Haram/PycharmProjects/pythonProject2/venv/Lib/Useful Functions.py" square:-with code[1, 4, 9, 16, 25] with function:-[1, 4, 9, 16, 25] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] [(1, 7), (2, 8), (3, 9), (4, 10), (5, 11...

anonymous function application

##sorting ##list of tuple pairs list1=[( 38 , 2 ) , ( 3 , 10 ) , ( 5 , 1 ) , ( 30 , 8 )] print ( "sorted on the order of ist element" ) print ( sorted (list1)) ##on the order of first element print ( "sorted on the order of second element" ) print ( sorted (list1 , key = lambda x:x[ 1 ])) "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Scripts\python.exe" "C:/Users/darul Haram/PycharmProjects/pythonProject2/venv/Lib/application(anonymous,lambda).py" sorted on the order of ist element [(3, 10), (5, 1), (30, 8), (38, 2)] sorted on the order of second element [(5, 1), (38, 2), (30, 8), (3, 10)] Process finished with exit code 0

Anonymous function(lambda)

  ## anonymous function - lambda function ##only one return statement, any number of argument ##no name use keyword lambda ##variable = lambda argument(s):return expression a= lambda p , q:p+q print (a( 8 , 9 )) ## for j power of k b= lambda j , k:j**k print (b( 100 , 100 )) "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Scripts\python.exe" "C:/Users/darul Haram/PycharmProjects/pythonProject2/venv/Lib/Anonymous function (lambda fn).py" 17 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 Process finished with exit code 0

Functions5(creating variable for large inputs)

  ##arbitrary argument def add (*x): ## *:-class tuple, allows to take more than one variable return sum (x) print (add( 1 , 2 , 3 , 4 , 5 , 6 , 78 , 9 , 1 , 2 , 3 , 4 , 6 , 7 , 8 , 8 , 9 , )) "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Scripts\python.exe" "C:/Users/darul Haram/PycharmProjects/pythonProject2/venv/Lib/functions5(arbitrary argument).py" 156 Process finished with exit code 0

Functions4(default argument)

  ##solving quadratic equation import math def quadratic (b , c , a= 10 ): ## non default argumnet follows default argument. determinant=math.sqrt(b*b- 4 *a*c) a1=(-b-determinant)/( 2 *a*c) a2=(-b+determinant)/( 2 *a*c) return a1 , a2 a1 , a2=quadratic( b = 11 , c = 3 ) print ( "value a1:" + str (a1) , "value a2:" + str (a2)) "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Scripts\python.exe" "C:/Users/darul Haram/PycharmProjects/pythonProject2/venv/Lib/functions 2(default argument.py" value a1:-0.2 value a2:-0.16666666666666666 Process finished with exit code 0

Functions(solving quadratic eqn)

  import math def quadratic (a , b , c): determinant=math.sqrt(b** 2 - 4 *a*c) p1=(-b+determinant)/ 2 *a*c p2=(-b-determinant)/ 2 *a*c return p1 , p2 p1 , p2=quadratic( 2 , 32 , 128 ) print (p1 , p2) print ( "p1:-" + str (p1)) print ( "p2:-" + str (p2)) "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Scripts\python.exe" "C:/Users/darul Haram/PycharmProjects/pythonProject2/venv/Lib/Functions3(sqrt).py" -4096.0 -4096.0 p1:--4096.0 p2:--4096.0 Process finished with exit code 0

function(using variable inside function and updating)

  def update (x): x= 10 print ( "inside the function:" + str (x)) x= 5 print ( "outside the function:" + str (x)) update(x) print ( "after executing the function:" + str (x)) "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Scripts\python.exe" "C:/Users/darul Haram/PycharmProjects/pythonProject2/venv/Lib/Functions 2.py" outside the function:5 inside the function:10 after executing the function:5 Process finished with exit code 0

Functions 1

  ## 'write what wanna do' just after 'def'. ##then its expression ##then return that value. ##then either store that value in a variable and print it or just print. ##ex:- def power (j , k): z=j**k return z print (power( 100 , 10 )) ## for addition. def add (a , b): z=a+b return z print (add( 2 , 99 )) "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Scripts\python.exe" "C:/Users/darul Haram/PycharmProjects/pythonProject2/venv/Lib/Functions.py" 100000000000000000000 Process finished with exit code 0

set 3

 Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> A={1,2,3,4,5,6} >>> B={1,2,3,4,5,6,7,8,9} >>> A.issubset(B) True >>> A.issuperset(B) False >>> B.issuperset(A) True >>> B.issubset(A) False >>> A.clear() >>> A set() >>> B {1, 2, 3, 4, 5, 6, 7, 8, 9} >>> B.clear() >>> B set() >>> ##above was concept to clear all elements in a set >>> ##subset is small superset is large**

sets 2

 Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> ##union :-it gives common elements to both >>> a={1,2,3,4,5,6} >>> b={4,5,6,7,8,9} >>> a|b {1, 2, 3, 4, 5, 6, 7, 8, 9} >>> a.union(b) {1, 2, 3, 4, 5, 6, 7, 8, 9} >>> ##above comment is for intersection, but that was union(elements of both sets without repetition) >>> A&B##A intersection B, A n B Traceback (most recent call last):   File "<pyshell#6>", line 1, in <module>     A&B##A intersection B, A n B NameError: name 'A' is not defined >>> ##A&B, A intersection B,    A n B >>> A={10,20,30} >>> B={20,30,40,50} >>> A&B {20, 30} >>> A n B SyntaxError: invalid syntax >>> A-B {10} >>> B-A {40, 50} >>> ...

Sets

 Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> ##Almost similar to sets in maths >>> a={1,2,3,4,5} >>> type(a) <class 'set'> >>> ## remember this below >>> a={} >>> type(a) <class 'dict'> >>> ##set >>> a=() >>> type(a) <class 'tuple'> >>> ##above fo tuple >>> ##below for set >>> a=set() >>> type(a) <class 'set'> >>> ##sets are immutable >>> ##sets doesnt have any index , no order >>> a={10,90,100,3,5,40,70} >>> set(a) {3, 100, 5, 70, 90, 40, 10} >>> set(a) {3, 100, 5, 70, 90, 40, 10} >>> set(a) {3, 100, 5, 70, 90, 40, 10} >>> set[0] set[0] >>> a[0] Traceback (most recent call last):...

methods in dictionary

 Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> islam={'to believe':'in all that ordained','prayer':'5 compulsory','fast':'once a year','charity':'2.5percent','pilgrimage':'once in life'} >>> islam.details() Traceback (most recent call last):   File "<pyshell#1>", line 1, in <module>     islam.details() AttributeError: 'dict' object has no attribute 'details' >>> islam.keys() dict_keys(['to believe', 'prayer', 'fast', 'charity', 'pilgrimage']) >>> islam.vales() Traceback (most recent call last):   File "<pyshell#4>", line 1, in <module>     islam.vales() AttributeError: 'dict' object has no attribute ...

dictionary :- split and count length

  ##sentence:-input, key:-word, value:-length of word sentence= input ( "enter the sentence" ) z=sentence.split( " " ) print (z) ##split :-provides us words in list a={z: len (z) for z in z} print (a) "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Scripts\python.exe" "C:/Users/darul Haram/PycharmProjects/pythonProject2/venv/Lib/dictionary comprehension 2.py" enter the sentenceall human is being is created to worship one god ['all', 'human', 'is', 'being', 'is', 'created', 'to', 'worship', 'one', 'god', ''] {'all': 3, 'human': 5, 'is': 2, 'being': 5, 'created': 7, 'to': 2, 'worship': 7, 'one': 3, 'god': 3, '': 0} Process finished with exit code 0

dictionary comprehension

##Dictionary concept:-numbers(expression(key:value),for loop condition) squares={a:a*a for a in range ( 1 , 1000 )} print (squares) "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Scripts\python.exe" "C:/Users/darul Haram/PycharmProjects/pythonProject2/venv/Lib/Dictionary comprehension.py" {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225, 16: 256, 17: 289, 18: 324, 19: 361, 20: 400, 21: 441, 22: 484, 23: 529, 24: 576, 25: 625, 26: 676, 27: 729, 28: 784, 29: 841, 30: 900, 31: 961, 32: 1024, 33: 1089, 34: 1156, 35: 1225, 36: 1296, 37: 1369, 38: 1444, 39: 1521, 40: 1600, 41: 1681, 42: 1764, 43: 1849, 44: 1936, 45: 2025, 46: 2116, 47: 2209, 48: 2304, 49: 2401, 50: 2500, 51: 2601, 52: 2704, 53: 2809, 54: 2916, 55: 3025, 56: 3136, 57: 3249, 58: 3364, 59: 3481, 60: 3600, 61: 3721, 62: 3844, 63: 3969, 64: 4096, 65: 4225, 66: 4356, 67: 4489, 68: 4624, 69: 4761, 70: 4900, 71: 5041, 72: 5184, 73: 5329, ...

Dictionary

## lets create a dictionary biodata={ "name" : "Aamir" , "age" : "25" , "ms" : "unmarried" } print (biodata) ##to add details biodata[ "blood" ]= "o+" print (biodata) ## to get details print (biodata[ "name" ]) ##1:-others from of dictionary country={ "india" : "delhi" , "usa" : "wdc" } print (country) ##in case of numbers:-remeber this about string concept. numbers={ 1 : 2 , 3 : 4 , 5 : 6 } print (numbers) ##2:-other form of dictionary:-##dict(list of tuple pairs) currency=[( "rupee" , "india" ) , ( "dollar" , "usa" )] print (currency) ##in case of numbers here squares={ 1 : 1 , 2 : 4 , 3 : 9 } print (squares) ##all praise to be Allah "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Scripts\python.exe" "C:/Users/darul Haram/PycharmProjects/pythonProject2/venv/Lib/Dictionary.py" {'name...

all about tuples

 Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> ###All about tuples >>> tuples=(1,2,3,4,5) >>> tuples (1, 2, 3, 4, 5) >>> tuples=1,2,3,4,5 >>> tuples (1, 2, 3, 4, 5) >>> ##tuples are immutable :-its value cant be changed but list contained in tuple can be changed >>> #slicing is possible >>> tuple=1,2,3,4,5,6,7,8 >>> tuple[::] (1, 2, 3, 4, 5, 6, 7, 8) >>> tuple=2,4,6,8 >>> tuple[0]=5 Traceback (most recent call last):   File "<pyshell#10>", line 1, in <module>     tuple[0]=5 TypeError: 'tuple' object does not support item assignment >>> tuple=([1],2,3,4) >>> tuple[0]=5 Traceback (most recent call last):   File "<pyshell#12>", line 1, in <module>     tuple[0]...

creating user inputs as a list

  '''creating user inputs as a list''' new_list=[] n= int ( input ( "enter the size of the list" )) print ( "enter the elements one by one" ) for a in range ( 0 , n): z= int ( input ()) new_list.append(z) print (new_list "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Scripts\python.exe" "C:/Users/darul Haram/PycharmProjects/pythonProject2/venv/Lib/user input as a list.py" enter the size of the list4 enter the elements one by one 2 4 6 8 [2, 4, 6, 8] Process finished with exit code 0

Adding two list using list comprehension

  ##adding two lists list1=[ 1 , 2 , 3 , 4 , 5 , 10 , 19 , 8 ] list2=[ 4 , 5 , 6 , 7 , 8 ] list3=[x for x in list1 if x in list2] print (list3) "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Scripts\python.exe" "C:/Users/darul Haram/PycharmProjects/pythonProject2/venv/Lib/Adding two list using list comprehension.py" [4, 5, 8] Process finished with exit code 0

Join method in string

 Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> ##join method in string >>> string=("Allah","is","almerciful") >>> "".join(string) 'Allahisalmerciful' >>> " ".join(string) 'Allah is almerciful' >>> 

list comprehension

  ## i wanna generate a list of numbers ##list(#expression#for loop# condition) squares=[i*i for i in range ( 1 , 11 )] print (squares) ##if i**i then i=n upto n times multiply=[i**i for i in range ( 1 , 11 )] print (multiply) "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Scripts\python.exe" "C:/Users/darul Haram/PycharmProjects/pythonProject2/venv/Lib/list comprehension.py" [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] [1, 4, 27, 256, 3125, 46656, 823543, 16777216, 387420489, 10000000000] Process finished with exit code 0

palindrome checker

##find whether a string is palindrome or not ##like:-malayalam,rotor original_string= input ( "enter a string" ) reversed_string=original_string[::- 1 ] if original_string==reversed_string: print ( "the_given_string_is_palindrome" ) else : print ( "not a string" ) "C:\Users\darul Haram\PycharmProjects\pythonProject2\venv\Scripts\python.exe" "C:/Users/darul Haram/PycharmProjects/pythonProject2/venv/Lib/palindrome checker.py" enter a stringmadam the_given_string_is_palindrome Process finished with exit code 0

count,index,copy in a list

 Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> ##list methods >>> ##count >>> list=[1,234,345,10,10,10,20,020,20,50,50,050] SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers >>> list=[10,10,10,20,20,20,234,40,24,40,40,40,5050,5050,5050] >>> list.count(10) 3 >>> count(40) Traceback (most recent call last):   File "<pyshell#5>", line 1, in <module>     count(40) NameError: name 'count' is not defined >>> list.count(40) 4 >>> ##index >>>  >>>  >>> list.index(4) Traceback (most recent call last):   File "<pyshell#10>", line 1, in <module>     list.index(4) ValueError: 4 is not in list >>> list.index(40) 7 >...

slicing in string

Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> ##nested list >>> list1[1,2,3,4,5,[6,7,8],20,29] Traceback (most recent call last):   File "<pyshell#1>", line 1, in <module>     list1[1,2,3,4,5,[6,7,8],20,29] NameError: name 'list1' is not defined >>> list1=[1,2,3,4,5,[10,19,20,30],39,48,49] >>> list[8] list[8] >>> list1[8] 49 >>> list1[5] [10, 19, 20, 30] >>> list1[5][3] 30 >>> ##slicing >>> fruits=["apple","orange","banana","mango","pineapple"] >>> fruits[::] ['apple', 'orange', 'banana', 'mango', 'pineapple'] >>> fruits[::-1] ['pineapple', 'mango', 'banana', 'orange', 'ap...

insert,reverse,sort,pop and remove elements from list

 Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> ##list methods and functions e.g-sum(),max(), min(),any(), len(),all() >>> numbers=[2,4,6,8,10] >>> numbers.insert(12) Traceback (most recent call last):   File "<pyshell#2>", line 1, in <module>     numbers.insert(12) TypeError: insert expected 2 arguments, got 1 >>> numbers.insert[12] Traceback (most recent call last):   File "<pyshell#3>", line 1, in <module>     numbers.insert[12] TypeError: 'builtin_function_or_method' object is not subscriptable >>> numbers.insert(-1,12) >>> numbers [2, 4, 6, 8, 12, 10] >>> numbers.insert(5,14) >>> numbers [2, 4, 6, 8, 12, 14, 10] >>> numbers.(reverse=False   SyntaxError: invalid syntax >>> numbe...

Adding elements to a list:-append and extend

 Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> ##Adding elements to a list >>> squares=[1,4,9,16] >>> squares.append(35) >>> squares [1, 4, 9, 16, 35] >>> squares.extend([36,49,64]) >>> squares [1, 4, 9, 16, 35, 36, 49, 64] >>> squares.append([36,49,64]) >>> squares [1, 4, 9, 16, 35, 36, 49, 64, [36, 49, 64]] >>> 

changing the values of list

 Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> ##change the values of a list >>> islam=["believe","prayer","fast","charity","pilgrimage"] >>> islam[-2]=prayer Traceback (most recent call last):   File "<pyshell#2>", line 1, in <module>     islam[-2]=prayer NameError: name 'prayer' is not defined >>> islam[-2]="prayer" >>> islam ['believe', 'prayer', 'fast', 'prayer', 'pilgrimage'] >>> islam[::]="to believe" >>> islam ['t', 'o', ' ', 'b', 'e', 'l', 'i', 'e', 'v', 'e'] >>> islam[0:1:]="bei SyntaxError: EOL while scanning string liter...

creating a list

 Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> ##creating a list >>> list1[] SyntaxError: invalid syntax >>> list1=[] >>> list1 [] >>> list=[] >>> list [] >>> list=[1,2,3,4,5,6,] >>> list [1, 2, 3, 4, 5, 6] >>> list2=[1,2,3,4,"apple", "orange"] >>> list2 [1, 2, 3, 4, 'apple', 'orange'] >>> 

reversing a string

 Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> name="Aamir" >>> string[::,-1] Traceback (most recent call last):   File "<pyshell#1>", line 1, in <module>     string[::,-1] NameError: name 'string' is not defined >>> string[::-1] Traceback (most recent call last):   File "<pyshell#2>", line 1, in <module>     string[::-1] NameError: name 'string' is not defined >>> name[::] 'Aamir' >>> name[::-1] 'rimaA' >>> ##if step value is negative then it start from last upto begining.

String methds

 Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> z="Aamir" >>> type z SyntaxError: invalid syntax >>> type(z) <class 'str'> >>> z.upper() 'AAMIR' >>> z.lower() 'aamir' >>> len(z) 5 >>> z.replace('m','z') 'Aazir' >>>  >>>  >>> names="Aamir,Asad,noor,neyaz" >>> names 'Aamir,Asad,noor,neyaz' >>> names.split(",") ['Aamir', 'Asad', 'noor', 'neyaz'] >>> names[0] 'A' >>> names=names.split(",") >>> names[0] 'Aamir' >>> names=names.split(",") Traceback (most recent call last):   File "<pyshell#16>", line 1, in <modul...