├── Python_Tuple.py ├── Python_While_Loop.py ├── Python_Class_and_Objects.py ├── Python_If_Else.py ├── Python_For_Loop.py ├── Python_Break_and_Contiue.py ├── Python_Class_Attributes.py ├── Python_Polymorphism.py ├── Python_List_Comprehension.py ├── Python_String.py ├── Python_Sets.py ├── Python_Dictionary.py ├── Python_Exception_Handling.py ├── Python_Inheritance.py ├── Python_Lambda_Functions ├── Python_Static_Method.py ├── Python_Input_Output.py ├── Python_Operators.py ├── Python_List.py └── Python_Functions.py /Python_Tuple.py: -------------------------------------------------------------------------------- 1 | # Work with tuple - Immutable object 2 | 3 | # empty tuple 4 | t1 = () 5 | 6 | # failed assignment and append 7 | # t1.append(5) 8 | # t1[0] = 3 9 | 10 | t2 = (20,30,40,50) 11 | print(len(t2)) 12 | 13 | # indexing also works for tuple 14 | # access 3rd element from tuple 15 | 16 | print(t2[2]) 17 | 18 | # Indexing works 19 | # Slicing works 20 | # Iteration also works 21 | -------------------------------------------------------------------------------- /Python_While_Loop.py: -------------------------------------------------------------------------------- 1 | # Syntax 2 | # while exp: 3 | # statements 4 | 5 | # write a program to print the table of 9 6 | num = 9 7 | counter = 1 8 | 9 | while counter<=10: # Either True or False 10 | ans = num * counter 11 | print(num,' * ',counter,' = ',ans) 12 | counter = counter + 1 13 | 14 | # What will happen if counter not incremented?? 15 | while counter<=10: # Either True or False 16 | ans = num * counter 17 | print(num,' * ',counter,' = ',ans) 18 | #counter = counter + 1 19 | 20 | # 9 * 1 = 9 -> 21 | 22 | # What will happen? 23 | while True: 24 | print("iNeuron") 25 | -------------------------------------------------------------------------------- /Python_Class_and_Objects.py: -------------------------------------------------------------------------------- 1 | # How to create a class in Python 2 | class Employee: 3 | # Constructor of class 4 | # it is mainly used for assignment of instance variables 5 | def __init__(self, name, salary ): 6 | # instance variable or instance attributes 7 | self.emp_name = name 8 | self.emp_salary = salary 9 | # method of a class 10 | def displayEmployeeInfo(self): 11 | print("Employee name : ",self.emp_name, " , Employee Salary : ",self.emp_salary) 12 | 13 | emp1 = Employee('Shashank', 1000) 14 | emp2 = Employee('Rahul', 2000) 15 | 16 | emp1.displayEmployeeInfo() 17 | emp2.displayEmployeeInfo() 18 | 19 | print(emp1.emp_name) 20 | print(emp2.emp_name) 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /Python_If_Else.py: -------------------------------------------------------------------------------- 1 | # How to use If-Else in Python 2 | x = 10 3 | y = 5 4 | 5 | if x==y: 6 | print("Yes, X is Equals to Y !!") 7 | else: 8 | print("No, X is not Equals to Y !!") 9 | 10 | # Is it mandatory to use else block with if? 11 | 12 | a = 50 13 | 14 | if a==50: 15 | print("Yes, A is Equals to 50 !!") 16 | print("Bye !!") 17 | 18 | a = 40 19 | 20 | if a==50: 21 | print("Yes, A is Equals to 50 !!") 22 | 23 | print("Bye !!") 24 | 25 | # Nested if-else condition 26 | marks = 54 27 | 28 | if marks>=90: 29 | print("Grade A+") 30 | elif marks>=80 and marks<90: 31 | print("Grade A") 32 | elif marks>=70 and marks<80: 33 | print("Grade B+") 34 | elif marks>=60 and marks<70: 35 | print("Grade B") 36 | else: 37 | print("Grade C") 38 | -------------------------------------------------------------------------------- /Python_For_Loop.py: -------------------------------------------------------------------------------- 1 | 2 | # Write a code to print numbers from 1 to 10 3 | for num in range(1,11): #rang(1,11) -> [1,2,3,4,5,6,7,8,9,10] 4 | print(num) 5 | 6 | # Write a code to print numbers from 1 to 10 using 2 steps 7 | # OR 8 | # Example to print odd numbers starting from value 1 9 | for num in range(1,11,2): 10 | print(num) 11 | 12 | # Write a program to print numbers from 10 to 1 13 | for num in range(10,0,-1): 14 | print(num) 15 | 16 | # Write a program to calculate the sum of given list elements using for loop 17 | # output = 25 18 | int_list = [4,8,-2,10,5] 19 | list_sum = 0 20 | for num in int_list: 21 | list_sum = list_sum + num 22 | print("Total sum of elements = ",list_sum) 23 | 24 | # 0 + 4 = 4 25 | # 4 + 8 = 12 26 | # 12 + (-2) = 10 27 | # 10 + 10 = 20 28 | # 20 + 5 = 25 29 | -------------------------------------------------------------------------------- /Python_Break_and_Contiue.py: -------------------------------------------------------------------------------- 1 | 2 | # How to use break statement 3 | int_list = [1,5,7,8,19,13,17,3] 4 | 5 | # Find the even value in the given list 6 | for num in int_list: 7 | print("Current element of the list = ",num) 8 | if num%2 == 0: 9 | print("Even number in the list = ",num) 10 | break 11 | 12 | # What is break is removed? 13 | for num in int_list: 14 | print("Current element of the list = ",num) 15 | if num%2 == 0: 16 | print("Even number in the list = ",num) 17 | 18 | # How to use continue keyword? 19 | # Print the numbers from for loop and start them from value 1 20 | # but print values on terminal if number is greater than 10 21 | 22 | for num in range(1,21): # range(1,21) -> [1,2,3,4,5,6....20] 23 | if num<10: 24 | continue 25 | print(num) 26 | -------------------------------------------------------------------------------- /Python_Class_Attributes.py: -------------------------------------------------------------------------------- 1 | class Employee: 2 | 3 | # Class attribute 4 | empCount = 0 5 | 6 | # Constructor of class 7 | # it is mainly used for assignment of instance variables 8 | def __init__(self, name, salary ): 9 | # instance variable or instance attributes 10 | self.emp_name = name 11 | self.emp_salary = salary 12 | Employee.empCount += 1 13 | 14 | # method of a class 15 | def displayEmployeeInfo(self): 16 | print("Employee name : ",self.emp_name, " , Employee Salary : ",self.emp_salary) 17 | 18 | # method of a class 19 | def displayEmployeeCount(self): 20 | print("Employee Count : ",Employee.empCount) 21 | 22 | emp1 = Employee('Shashank', 1000) 23 | emp1.displayEmployeeInfo() 24 | emp1.displayEmployeeCount() 25 | 26 | emp2 = Employee('Rahul', 2000) 27 | emp2.displayEmployeeInfo() 28 | emp2.displayEmployeeCount() 29 | 30 | 31 | emp1.displayEmployeeCount() 32 | emp2.displayEmployeeCount() 33 | -------------------------------------------------------------------------------- /Python_Polymorphism.py: -------------------------------------------------------------------------------- 1 | # Function Overriding in Python 2 | 3 | from math import pi 4 | 5 | class Shape: 6 | def __init__(self, name): 7 | self.name = name 8 | 9 | def area(self): 10 | pass 11 | 12 | def fact(self): 13 | pass 14 | 15 | def whichShape(self): 16 | print(self.name) 17 | 18 | class Square(Shape): 19 | def __init__(self, name, length): 20 | super().__init__(name) 21 | self.side_length = length 22 | 23 | def area(self): 24 | print(self.side_length**2) 25 | 26 | def fact(self): 27 | print("Square has each angle equalt to 90 degrees") 28 | 29 | class Circle(Shape): 30 | def __init__(self, name, radius): 31 | super().__init__(name) 32 | self.circle_radius = radius 33 | 34 | def area(self): 35 | print(pi * (self.circle_radius**2)) 36 | 37 | sq = Square("Square", 4) 38 | cr = Circle("Circle", 5) 39 | sq.area() 40 | cr.area() 41 | sq.fact() 42 | cr.fact() 43 | 44 | sq.whichShape() 45 | cr.whichShape() 46 | -------------------------------------------------------------------------------- /Python_List_Comprehension.py: -------------------------------------------------------------------------------- 1 | # Write a program to generate list of 10 numbers 2 | result = [] 3 | for i in range(1,11): 4 | result.append(i) 5 | 6 | print(result) 7 | 8 | # How to do it with the help of list comprehension? 9 | result = [ x for x in range(1,11) ] 10 | print(result) 11 | 12 | # Get a list of all even numbers between 1 to 50 13 | result = [ x for x in range(1,51) if x % 2 == 0 ] 14 | print(result) 15 | 16 | 17 | # Get a list of all even numbers from given list 18 | list_a = [1,2,4,3,6,7,9] 19 | result = [ x for x in list_a if x % 2 == 0 ] 20 | print(result) 21 | 22 | 23 | # convert all string into upper case in given list 24 | list_a = ['hi', 'hello' , 'bye' , 'nice'] 25 | 26 | result = [ x.upper() for x in list_a ] 27 | print(result) 28 | 29 | # Put all negative numbers after positive numbers from given list 30 | list_a = [9,-1,2,-5,1,10,-6] 31 | 32 | # result = [9,2,1,10,-1,-5,-6] 33 | # result1= [x for x in list_a if x>0 ] 34 | # result2= [x for x in list_a if x<0 ] 35 | # print (result1 + result2) 36 | 37 | result = [ x for x in list_a if x>0 ] + [ x for x in list_a if x<0 ] 38 | print (result) 39 | -------------------------------------------------------------------------------- /Python_String.py: -------------------------------------------------------------------------------- 1 | # Create a string in Python 2 | str1 = "iNeuron" 3 | print(str1) 4 | 5 | str2 = "Shashank's" 6 | print(str2) 7 | 8 | str3 = 'He is a "Good" boy' 9 | print(str3) 10 | 11 | # use length function 12 | print("length of the string : ",len(str3)) 13 | 14 | # how to write multiline string 15 | str4 = '''Shashank is 16 | not going 17 | to attend 18 | the seminar 19 | ''' 20 | print(str4) 21 | 22 | # string concatenation 23 | str5 = "Shashank" 24 | str6 = "Mishra" 25 | print(str5 + str6) 26 | 27 | # string comparsion? 28 | str7 = "Shashank" 29 | str8 = "Shashank" 30 | print(str7 == str8) 31 | 32 | # how to print the each character from the string 33 | for char in str8: 34 | print(char) 35 | 36 | print(str7[0]) 37 | print(str7[1]) 38 | print(str7[2]) 39 | print(str7[3]) 40 | print(str7[4]) 41 | print(str7[5]) 42 | print(str7[6]) 43 | print(str7[7]) 44 | 45 | str9 = "Shashank" 46 | 47 | # Update the 4th character in the string by M 48 | # str9[3] = 'M' 49 | # print(str9) 50 | 51 | # convert string into lower case 52 | print(str9.lower()) 53 | 54 | # convert string into upper case 55 | print(str9.upper()) 56 | 57 | # Other functionalities will be same as list like Slicing etc 58 | -------------------------------------------------------------------------------- /Python_Sets.py: -------------------------------------------------------------------------------- 1 | # Sets in python 2 | set1 = set() 3 | print(type(set1)) 4 | 5 | set3 = {1,2,3,4,5,2,1,4} 6 | print(set3) 7 | print(type(set3)) 8 | 9 | # dictionary 10 | # set4 = {} 11 | # print(type(set4)) 12 | 13 | list1 = [1,2,3,4,1,2,3,4,5,6,7,8,1] 14 | set2 = set(list1) 15 | print(set2) 16 | 17 | # how we can iterate elements in the set 18 | for num in set2: 19 | print(num) 20 | 21 | # Convert output of set into list 22 | list1 = [1,2,3,4,1,2,3,4,5,6,7,8,1] 23 | set5 = list(set(list1)) 24 | print(set5) 25 | print(set5[-1]) 26 | 27 | 28 | # how to insert elements in the set 29 | set6 = set() 30 | set6.add(1) 31 | set6.add(1) 32 | set6.add(2) 33 | set6.add(5) 34 | set6.add(2) 35 | set6.add(1) 36 | set6.add(2) 37 | print(set6) 38 | 39 | # use of update method 40 | tmp = [1,2,3,4,5,1,2,3,4,5,1,2,3] 41 | set6.update(tmp) 42 | print(set6) 43 | 44 | # calculate the length of the set 45 | print(len(set6)) 46 | 47 | # Set operations 48 | set_a = {1,2,3,4,5,6} 49 | set_b = {3,6,8,9,10} 50 | 51 | # union operation 52 | print(set_a | set_b) 53 | 54 | # intersection operation 55 | print(set_a & set_b) 56 | 57 | # A - B ? 58 | # B - A ? 59 | # Difference in Sets 60 | print(set_a - set_b) 61 | print(set_b - set_a) 62 | 63 | 64 | # Comparison in sets 65 | set_x = {1,2,3,4,5} 66 | set_y = {1,2,3,5,4,5,1} 67 | print ( set_x == set_y ) 68 | 69 | set_x = {1,2,3,4,5,6} # {1,2,3,4,5,6} 70 | set_y = {1,2,3,5,4,5,1} # {1,2,3,4,5} 71 | print ( set_x == set_y ) 72 | 73 | # set_m = {5,6,7,1,2,4} 74 | # print(set_m) 75 | -------------------------------------------------------------------------------- /Python_Dictionary.py: -------------------------------------------------------------------------------- 1 | # Dictionary in Python 2 | dict1 = {} 3 | print(type(dict1)) 4 | 5 | # How to insert values in Dictionary 6 | dict2 = {} 7 | dict2['name'] = 'Shashank' 8 | dict2['age'] = 22 9 | dict2['skills'] = ['Python', 'Java'] 10 | dict2['states_visited'] = ('UP', 'Goa') 11 | dict2[45] = 'Random Key' 12 | dict2['other_details'] = {'color' : 'Black', 'nationality' : 'Indian'} 13 | print(dict2) 14 | 15 | # Check the length of dictionary 16 | print(len(dict2)) 17 | 18 | # how to access value of a particular key 19 | print(dict2['name']) 20 | print(dict2['skills'][0]) 21 | print(dict2['other_details']['nationality']) 22 | 23 | 24 | # how to update value on a particular key 25 | dict2['age'] = 30 26 | print(dict2) 27 | 28 | # How to get the keys from a dictionary 29 | total_keys = list(dict2.keys()) 30 | print("Total Keys in dictionary : ",total_keys) 31 | 32 | # How to get the values from a dictionary 33 | total_values = list(dict2.values()) 34 | print("Total Values in dictionary : ",total_values) 35 | 36 | # How to iterate on dictionary? 37 | for k,v in dict2.items(): 38 | print("Keys is = ",k,' and Value is = ',v) 39 | 40 | 41 | # Compare dictionary ?? 42 | 43 | dict3 = {'a' : 1, 'b' : 2, 'c' : 3} 44 | dict4 = {'b' : 2, 'c' : 3, 'a' : 1} 45 | print(dict3 == dict4) 46 | 47 | dict3 = {'a' : 1, 'b' : 2, 'c' : 3} 48 | dict4 = {'b' : 2, 'c' : 3, 'a' : 5} 49 | print(dict3 == dict4) 50 | 51 | 52 | # how to delete specific key value pair from dictionary 53 | print(dict2) 54 | 55 | del dict2['age'] 56 | del dict2[45] 57 | 58 | print(dict2) 59 | 60 | # how to check if key exists in dictionary or not?? 61 | keys_in_dict = list(dict2.keys()) 62 | if 'skills' in keys_in_dict: 63 | print(True) 64 | else: 65 | print(False) 66 | 67 | 68 | 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /Python_Exception_Handling.py: -------------------------------------------------------------------------------- 1 | # How to handle exceptions in Python 2 | 3 | # a = 10 4 | # print("Hello !!") 5 | # print( a/0 ) 6 | # print("Bye !!!") 7 | 8 | # list_a = [2,6,8,1,30] 9 | # print( list_a[7] ) 10 | 11 | # a = 5 12 | # try: 13 | # result = a/0 14 | # print(result) 15 | # except: 16 | # print("Some Error Has Occured !!!") 17 | 18 | # print("Byee !!!") 19 | 20 | num = 5 21 | list_a = [1,2,3,4,7,90,20] 22 | dict_a = {'shashank' : 20, 'rahul' : 30} 23 | try: 24 | print("Divide number by 0 ") 25 | #result = num/0 26 | result = num/5 27 | print(result) 28 | print("Step 1 Done ") 29 | 30 | print("Access 11'th element from List ") 31 | # print(list_a[11]) 32 | print(list_a[5]) 33 | print("Step 2 Done ") 34 | 35 | print("Access value of amit from dictionary ") 36 | #print(dict_a['amit']) 37 | print(dict_a['shashank']) 38 | print("Step 3 Done ") 39 | except ZeroDivisionError: 40 | print("This Error Was Ocuured Because Division by 0 Happened !!") 41 | except IndexError: 42 | print("Error occured because out of bound index is getting accessed !!") 43 | except KeyError: 44 | print("Search Key Doesn't Exists !!") 45 | except Exception as err: 46 | print("Error Occured and Message : ", err) 47 | 48 | # Use of Else Block 49 | a = 5 50 | try: 51 | #result = a / 0 52 | result = a / 2 53 | except ZeroDivisionError: 54 | print("Error Occured because of Division by 0 !!") 55 | else: 56 | print("Calculation completed !!") 57 | print(result) 58 | 59 | # Use Of Finally Keyword 60 | a = 5 61 | try: 62 | result = a / 0 63 | # result = a / 2 64 | except ZeroDivisionError: 65 | print("Error Occured because of Division by 0 !!") 66 | else: 67 | print("Calculation completed !!") 68 | print(result) 69 | finally: 70 | print("Doesn't matter try-except but I will print myself !!") 71 | -------------------------------------------------------------------------------- /Python_Inheritance.py: -------------------------------------------------------------------------------- 1 | # # Inheritance in Python 2 | 3 | # # Base Class aka Parent Class 4 | class Person(): 5 | def __init__(self, name): 6 | self.name = name 7 | 8 | def displayName(self): 9 | print(self.name) 10 | 11 | # By default we can say that particular perosn is unemployed 12 | def isEmployed(self): 13 | print(self.name," is Un-Employed !!") 14 | 15 | # Derived Class aka Child Class 16 | class Employee(Person): 17 | 18 | def isEmployed(self): 19 | print(self.name," is Employed !!") 20 | 21 | 22 | emp = Person('Shashank') 23 | emp.displayName() 24 | emp.isEmployed() 25 | 26 | emp1 = Employee('Rahul') 27 | emp1.displayName() 28 | emp1.isEmployed() 29 | 30 | # How to initialize constructor of Base Class 31 | # Base Class aka Parent Class 32 | class Person(): 33 | def __init__(self, name): 34 | self.name = name 35 | 36 | def displayName(self): 37 | print(self.name) 38 | 39 | # By default we can say that particular perosn is unemployed 40 | def isEmployed(self): 41 | print(self.name," is Un-Employed !!") 42 | 43 | # Derived Class aka Child Class 44 | class Employee(Person): 45 | 46 | def __init__(self,emp_name, id_num, salary, designation): 47 | self.idnumber = id_num 48 | self.emp_salary = salary 49 | self.emp_designation = designation 50 | 51 | # Calling constructor of Base Class 52 | # Person.__init__(self,emp_name) 53 | super().__init__(emp_name) 54 | 55 | def isEmployed(self): 56 | print(self.name," is Employed !!") 57 | 58 | def employeeDetails(self): 59 | print("Employee Id is ",self.idnumber, 60 | " Employee Salary is ",self.emp_salary, 61 | " Employee Designation is ",self.emp_designation) 62 | 63 | emp1 = Employee('Shashank',5432,1000,'Data Engineer - 3') 64 | emp1.displayName() 65 | print(emp1.name) 66 | emp1.employeeDetails() 67 | -------------------------------------------------------------------------------- /Python_Lambda_Functions: -------------------------------------------------------------------------------- 1 | # How to create Lambda Functions 2 | 3 | # First normal function to add integer 5 in given number 4 | def add_five(num): 5 | result = num + 5 6 | return result 7 | 8 | x = 7 9 | print(add_five(x)) 10 | 11 | # Same program using lambda function 12 | lambda_add_five = lambda x : x + 5 13 | 14 | y = 10 15 | print(lambda_add_five(y)) 16 | 17 | # Write a lambda function to add two input numbers 18 | lambda_add_two_nums = lambda x , y : x + y 19 | 20 | a = 30 21 | b = 20 22 | print(lambda_add_two_nums(a,b)) 23 | 24 | # Wite a lambda function to concatenate two input strings 25 | lamda_q=lambda x,y: x + y 26 | c="darshan" 27 | d="shirke" 28 | print(lamda_q(c,d)) 29 | 30 | 31 | # Write a lambda function to calculate maximum of two numbers 32 | def max_two_nums(x,y): 33 | if x>y: 34 | return x 35 | else: 36 | return y 37 | 38 | a = 5 39 | b = 4 40 | print(max_two_nums(a,b)) 41 | 42 | lambda_max_two_nums = lambda x,y : x if x>y else y 43 | 44 | num1 = 20 45 | num2 = 10 46 | print(lambda_max_two_nums(num1,num2)) 47 | 48 | # How to work with map() , filter(), reduce() 49 | 50 | # implement map function 51 | list1 = [1,2,3,4,5,6,7,8,9] 52 | 53 | # Result 54 | # result = [1,4,9,16,25,36,49,64,81] 55 | square_num = lambda x : x*x 56 | square_list = list(map(square_num, list1)) 57 | print(square_list) 58 | 59 | # Add sequential respective elements in two given lists 60 | list_a = [1,2,3,4,5] 61 | list_b = [5,4,3,2,1] 62 | 63 | #result = [6, 6, 6, 6, 6] 64 | sum_two_elements = lambda x , y : x + y 65 | result = list(map(sum_two_elements , list_a, list_b )) 66 | print(result) 67 | 68 | # How to use reduce ?? 69 | import functools 70 | 71 | list_x = [1,2,3,4,5] 72 | add_two_nums = lambda x , y : x + y 73 | 74 | result = functools.reduce(add_two_nums , list_x) 75 | print(result) 76 | 77 | multiply_two_nums = lambda x , y : x * y 78 | result = functools.reduce(multiply_two_nums , list_x) 79 | print(result) 80 | 81 | 82 | # How to use filter () 83 | 84 | # Create list of only odd elements 85 | seq = [1,2,5,6,9,7,10] 86 | 87 | filter_odd = lambda x : x % 2 != 0 88 | 89 | result = list(filter(filter_odd , seq)) 90 | print(result) 91 | 92 | filter_even = lambda x : x % 2 == 0 93 | 94 | result = list(filter(filter_even , seq)) 95 | print(result) 96 | -------------------------------------------------------------------------------- /Python_Static_Method.py: -------------------------------------------------------------------------------- 1 | class Employee: 2 | 3 | # Class attribute 4 | empCount = 0 5 | 6 | # Constructor of class 7 | # it is mainly used for assignment of instance variables 8 | def __init__(self, name, salary ): 9 | # instance variable or instance attributes 10 | self.emp_name = name 11 | self.emp_salary = salary 12 | Employee.empCount += 1 13 | 14 | # method of a class 15 | def displayEmployeeInfo(self): 16 | print("Employee name : ",self.emp_name, " , Employee Salary : ",self.emp_salary) 17 | 18 | # method of a class 19 | def displayEmployeeCount(self): 20 | print("Employee Count : ",Employee.empCount) 21 | 22 | emp1 = Employee('Shashank', 1000) 23 | emp1.displayEmployeeInfo() 24 | emp1.displayEmployeeCount() 25 | 26 | emp2 = Employee('Rahul', 2000) 27 | emp2.displayEmployeeInfo() 28 | emp2.displayEmployeeCount() 29 | 30 | 31 | emp1.displayEmployeeCount() 32 | emp2.displayEmployeeCount() 33 | 34 | # print instance variables of a object 35 | print(emp1.emp_name) 36 | print(emp1.emp_salary) 37 | print(emp2.emp_name) 38 | print(emp2.emp_salary) 39 | 40 | # lets access Class variable from instance itself 41 | print(emp1.empCount) 42 | print(emp2.empCount) 43 | 44 | emp1.empCount = 10 45 | emp2.empCount = 20 46 | 47 | print(emp1.empCount) 48 | print(emp2.empCount) 49 | print(Employee.empCount) 50 | 51 | 52 | # What is Static Method in python?? 53 | class Car: 54 | def __init__(self, name, color): 55 | self.car_name = name 56 | self.car_color = color 57 | 58 | def displayCarDetails(self): 59 | print("Car name is ",self.car_name," and Car color is ",self.car_color) 60 | 61 | @staticmethod 62 | def initialMessage(): 63 | print("Nice Car !!!!!") 64 | 65 | Car.initialMessage() 66 | car1 = Car('XUV 700', 'Red') 67 | car1.displayCarDetails() 68 | 69 | # This will not work 70 | # Car.displayCarDetails() 71 | 72 | # iNeuron Company 73 | class Employee: 74 | @staticmethod 75 | def isEmployeeOf(): 76 | print("Employee Class for iNeuron !!") 77 | 78 | Employee.isEmployeeOf() 79 | 80 | 81 | class Calculation: 82 | @staticmethod 83 | def addTwoNums( num1, num2 ): 84 | print("Sum of two numbers = ", num1 + num2) 85 | 86 | Calculation.addTwoNums(10,5) 87 | -------------------------------------------------------------------------------- /Python_Input_Output.py: -------------------------------------------------------------------------------- 1 | # Declare & Assign Variables in python 2 | 3 | int_var = 10 4 | float_var = 18.25 5 | string_var = 'ineuron batch2' # or another way "ineuron batch2" 6 | bool_var = True # if we want to assign false then it should be like False 7 | 8 | 9 | # Function in python for Output 10 | # Function does what ? They might or minght not accept some parameter 11 | print("Hello World !!!!") 12 | 13 | # Print variable values in python 14 | print("Value of int_var = ",int_var," - Result Done !!") 15 | print("Value of float_var = ",float_var," - Result Done !!") 16 | print("Value of string_var = ",string_var," - Result Done !!") 17 | print("Value of bool_var = ",bool_var," - Result Done !!") 18 | 19 | # How to check the data type of any variable in Python 20 | # We can use type() funtion 21 | print("Type of int_var ?", type(int_var)) 22 | print("Type of float_var ?", type(float_var)) 23 | print("Type of string_var ?", type(string_var)) 24 | print("Type of bool_var ?", type(bool_var)) 25 | 26 | # How to do the type casting in python ?? 27 | # int() , float(), str(), bool() 28 | int_var = int_var + 10 # int_var = 10 + 10 and in next step int_var = 20 29 | casted_int_var = float(int_var) 30 | casted_float_var = int(float_var) 31 | 32 | print("Int to Float Typecasting for int_var = ",casted_int_var) 33 | print("Float to Int Typecasting for float_var = ",casted_float_var) 34 | 35 | numeric_str = "123" 36 | #numeric_str = numeric_str + 50 # is it valid ?? numeric_str = "123" + 50 37 | #print("Value of numeric_str = ",numeric_str) 38 | 39 | numeric_str = int(numeric_str) + 50 # numeric_str = int("123") + 50 -> 123+50 = 173 40 | print("Value of numeric_str = ",numeric_str) 41 | 42 | # How to take the inputs in Python ? 43 | # we can use input() function 44 | 45 | # name = input() 46 | # age = input() 47 | # print("User name = ",name) 48 | # print("User age = ",age) 49 | 50 | # Another way to take user input with custome message 51 | name = input("Enter value for name = ") 52 | age = input("Enter value for age = ") 53 | print("User name = ",name) 54 | print("User age = ",age) 55 | 56 | 57 | # Convert type of data during the input 58 | name = input("Enter value for name = ") 59 | age = int(input("Enter value for age = ")) 60 | print("User name = ",name) 61 | print("User age = ",age) 62 | 63 | 64 | print("Type of name = ", type(name)) 65 | print("Type of age = ", type(age)) 66 | -------------------------------------------------------------------------------- /Python_Operators.py: -------------------------------------------------------------------------------- 1 | # Numerical operators in Python 2 | 3 | # + for addition 4 | # - for substractions 5 | # * for multiplication 6 | # / for float division 7 | # // for integer division 8 | # ** for power calculation 9 | # % for Modulus 10 | 11 | x = 5 12 | y = 3 13 | 14 | print("Addition of x + y = ", x+y) 15 | print("Substraction of x - y = ", x-y) 16 | print("Multiplication of x * y = ", x*y) 17 | print("Float Division of x / y = ", x/y) 18 | print("Integer Divison of x // y = ", x//y) 19 | print("Modulus of x % y = ", x%y) 20 | print("Power of y on x i.e; x ** y = ", x**y) 21 | 22 | # concat operation for string 23 | full_name = str_data + " " + "mishra" 24 | print("Full name = ", full_name) 25 | 26 | # if we can use - as well ? IT will not work 27 | minus_str = "shashank" - "mishra" 28 | print("Minus str = ", minus_str) 29 | 30 | multiply_str = "shashank"*"mishra" 31 | print("Multiply str = ", multiply_str) 32 | 33 | power_str = "shashank"**"mishra" 34 | print("Power str = ", power_str) 35 | 36 | power_str = "shashank" ** 3 37 | print("Power str = ", power_str) 38 | 39 | multiply_numeric_str = "shashank"*5 40 | print("Multiply numeric str = ", multiply_numeric_str) 41 | 42 | # Assignment operators 43 | # = , x = 5 44 | # += , x += 5 -> x = x + 5 45 | # -= , x -= 5 -> x = x - 5 46 | # *= , x *= 5 -> x = x * 5 47 | # /= , x /= 5 -> x = x / 5 48 | # //= , x //= 5 -> x = x // 5 49 | # %= , x %= 5 -> x = x % 5 50 | 51 | 52 | # Comparison Operators ( we compare operand values) 53 | # == , Equals to condition , x == y 54 | # != , Not Equals to condition , x != y 55 | # > , Greater than condition , x > y 56 | # < , Less than condition , x < y 57 | # >= , Greater than and Equals to condition , x >= y 58 | # <= , Less than and Equals to condition , x <= y 59 | 60 | a = 10 61 | b = 5 62 | print("Result of a == b , ", a == b) 63 | print("Result of a != b , ", a != b) 64 | print("Result of a > b , ", a > b) 65 | print("Result of a < b , ", a < b) 66 | print("Result of a >= b , ", a >= b) 67 | print("Result of a <= b , ", a <= b) 68 | 69 | 70 | # logical operators in Python ( Logical check will happen for expression result) 71 | # and -> Returns true if both statements are true 72 | # or -> Returns true if one of statements are true 73 | # not -> Reverse the result, returns false if the result is true 74 | 75 | m = 10 76 | n = 8 77 | print("m>10 and n<10 Result " , m>10 and n<10) # False and True -> False 78 | print("m>20 or n<10 Result " , m>10 or n<10) # False or True -> 79 | print("not(m>20 and n<10) Result " , not(m>10 and n<10)) 80 | # not(False and True) -> not(False) -> True 81 | -------------------------------------------------------------------------------- /Python_List.py: -------------------------------------------------------------------------------- 1 | # Declare empty list 2 | L1 = [] 3 | #print(type(L1)) 4 | print("Initial length of List : ",len(L1)) 5 | 6 | # Insert values in list 7 | L1.append(5) 8 | L1.append(7) 9 | L1.append(8) 10 | print(L1) 11 | 12 | # Create a list of 1000 numbers start from 1 to 1000 13 | # int_list = [] 14 | # for num in range(1,1001): 15 | # int_list.append(num) 16 | 17 | # How to calculate the length of a list? 18 | len_of_list = len(L1) 19 | print("Lenght of a list : ",len_of_list) 20 | 21 | 22 | # how to check is list are equal to each other? 23 | list1 = [1,"Shashank",20,"Hi"] 24 | list2 = [1,"Shashank",20,"Hi"] 25 | print("Lists are equal ?? ", list1 == list2) 26 | 27 | list1 = [1,"Shashank",20,"Hi"] 28 | list2 = [1,"Shashank","Hi",20] 29 | print("Lists are equal ?? ", list1 == list2) 30 | 31 | # List concat 32 | list4 = [1,2,3,4,5] 33 | list5 = [80,90,100,110] 34 | result = list4 + list5 35 | print(result) 36 | 37 | # how to access list values 38 | list6 = [10,15,20,25,30,35] 39 | 40 | # Print all the elements of given list - Option 1 41 | for num in list6: 42 | print(num) 43 | 44 | # Print 3rd elements of given list - Option 2 45 | # syntax = list_name[index] 46 | print(list6[0]) 47 | print(list6[1]) 48 | print(list6[2]) 49 | print(list6[3]) 50 | print(list6[4]) 51 | print(list6[5]) 52 | 53 | # What will happen ?? 54 | # Answer will be Index out of range 55 | # print(list6[100]) 56 | 57 | list7 = [1,"Shashank",1000] 58 | print(list7) 59 | 60 | # how to update the value of list index item? 61 | # update Shashank to Rahul 62 | list7[1] = "Rahul" 63 | print(list7) 64 | 65 | 66 | # how to print list elements using length? 67 | for index in range(0, len(list7)): # range(0, 3) -> [0, 1, 2] 68 | print(list7[index]) 69 | 70 | list8 = [ 1 , 2 , 50, "Shashank", [6,6,6] , "Rahul"] 71 | print(len(list8)) # What will be the output ??? 72 | 73 | 74 | # Difference between append() and extend() 75 | list9 = [20,30,40] 76 | list10 = ["hi", "hello", "bye"] 77 | list9.append(list10) 78 | print("Output of Append : ",list9) 79 | print("Length after Append : ",len(list9)) 80 | 81 | list11 = [20,30,40] 82 | list12 = ["hi", "hello", "bye"] 83 | list11.extend(list12) 84 | print("Output of Extend : ",list11) 85 | print("Length after Extend : ",len(list11)) 86 | 87 | # List slicing 88 | list13 = [20,30,40,50,60,80,90] 89 | # Syntax -> list_name[start : end] 90 | # end index is exclusive 91 | print(list13[ 0 : ]) 92 | print(list13[ 3 : ]) 93 | print(list13[ : ]) 94 | print(list13[ : 4 ]) 95 | print(list13[ 2 : 6 ]) 96 | print(list13[ 0 : : 2]) # 3rd value is for step 97 | 98 | # How to print the last value of the list? 99 | print(list13[ len(list13) - 1 ]) 100 | print(list13[-1]) # negative index -1 means last element of the list 101 | 102 | # Print second last element from the list?? 103 | print(list13[-2]) 104 | 105 | # print inout list in reverse direction 106 | print(list13[-1 : : -1]) 107 | 108 | 109 | 110 | 111 | 112 | 113 | -------------------------------------------------------------------------------- /Python_Functions.py: -------------------------------------------------------------------------------- 1 | # Functions in python 2 | # What about len() 3 | 4 | # Examples of inbuilt functions 5 | # list1 = [1,2,3,4,5,6] 6 | # print("Maximum number from list : ", max(list1)) 7 | # print("Minimum number from list : ", min(list1)) 8 | # print("Sum number from list : ", sum(list1)) 9 | 10 | # How do functions works? 11 | # They may or may not accepts input parameter 12 | # They may or may not return any output 13 | 14 | 15 | # # example of a function which doesn't accept any parameter 16 | # # and doesn't return anything 17 | # def welcome_message(): 18 | # print("Welcome to iNeuron Batch-2 !!!") 19 | 20 | # welcome_message() 21 | 22 | # # example of a function which doesn't accept any parameter 23 | # # and does return something 24 | # def bot_message(): 25 | # print("Message from Bot using Print!!") 26 | # return "Message from Bot !!" 27 | 28 | # print( bot_message() ) 29 | # result = bot_message() 30 | # print("Output from bot_message() ", result) 31 | 32 | 33 | # example of a function which accepts some parameter 34 | # and return the values 35 | def avg_of_two_nums( a , b): 36 | count = 2 37 | print("First Value : ", a) 38 | print("Last Value : ", b) 39 | avg_result = (a+b)/2 40 | return avg_result 41 | 42 | num1 = 10 43 | num2 = 15 44 | # output = avg_of_two_nums( num1, num2 ) 45 | # print("Result of avg_of_two_nums functions : ", output) 46 | 47 | # output1 = avg_of_two_nums( num1 ) 48 | # print("Result of avg_of_two_nums functions : ", output1) 49 | 50 | 51 | # Write a function to calculate the factorial of a num ? 52 | def factorial(n): 53 | 54 | if n == 0 or n == 1: 55 | return 1 56 | 57 | result = 1 58 | for num in range(1, n+1): 59 | result = result * num 60 | 61 | return result 62 | 63 | x = 5 64 | ans = factorial(x) 65 | print("Factorial of number ",x, " = ", ans) 66 | 67 | x = 0 68 | ans = factorial(x) 69 | print("Factorial of number ",x, " = ", ans) 70 | 71 | 72 | # how to return multiple values from function 73 | 74 | a , b , c = 2 , 3, 5 75 | print("Value of a is : ",a) 76 | print("Value of b is : ",b) 77 | print("Value of c is : ",c) 78 | 79 | def square_and_cube(n): 80 | sqr = n*n 81 | cube = n*n*n 82 | return sqr,cube 83 | 84 | num = 3 85 | sqr_ans , cube_ans = square_and_cube(num) 86 | print("Square of ",num," is : ",sqr_ans) 87 | print("Cube of ",num," is : ",cube_ans) 88 | 89 | 90 | # How to create optional arguments in python fucntions 91 | def multiply(a , b=3): 92 | result = a * b 93 | return result 94 | 95 | num1 = 5 96 | num2 = 10 97 | print( multiply(num1 , num2) ) 98 | print( multiply(num1) ) 99 | print( multiply(num2) ) 100 | 101 | # What if we reverse this part 102 | # Not possible 103 | # def multiply(a=3 , b): 104 | # result = a * b 105 | # return result 106 | 107 | def multiply(a , b=3, c=10): 108 | result = a * b * c 109 | return result 110 | 111 | # What if we have more than 2 112 | num1 = 5 113 | num2 = 10 114 | num3 = 2 115 | print( multiply(num1 , num2, num3) ) 116 | print( multiply(num1, num2) ) 117 | print( multiply(num2, num3) ) 118 | print( multiply(num3) ) 119 | # print( multiply(num2) ) 120 | 121 | 122 | # Non-Key Valued arguments 123 | def example_nonkeyed_args( *argv ): 124 | for param in argv: 125 | print(param) 126 | 127 | example_nonkeyed_args( 'Hello' , 'Welcome', 'To', 'iNeuron' ) 128 | 129 | 130 | # Key Value type of arguments in Python 131 | def example_of_kwargs( **kwargs ): 132 | 133 | print("Value of host : ", kwargs['host']) 134 | print("Value of port : ", kwargs['port']) 135 | print("Value of password : ", kwargs['pswd']) 136 | 137 | for k,v in kwargs.items(): 138 | print("Key is ",k,' and Value is ',v) 139 | 140 | 141 | example_of_kwargs( host='170.80.80.80' , port=9021, pswd='XXFGH' ) 142 | 143 | example_of_kwargs( port=9021, pswd='XXFGH' , host='170.80.80.80') 144 | --------------------------------------------------------------------------------