|
| 1 | +# Variables |
| 2 | +a=10 # assign 10 to variable a |
| 3 | +b=10 # assign 10 to variable b |
| 4 | + |
| 5 | +# id(): |
| 6 | +# id() function is a built-in function that returns the unique identifier of an object. |
| 7 | +# The identifier is an integer, which represents the memory address of the object. |
| 8 | +# The id() function is commonly used to check if two variables or objects refer to the same memory location. |
| 9 | +print(id(a))#a and b have same value the memory location will be same displayed. |
| 10 | +print(id(b)) |
| 11 | + |
| 12 | +# class: |
| 13 | +# Python is an object oriented programming language. |
| 14 | +# Almost everything in Python is an object, with its properties and methods. |
| 15 | +# A Class is like an object constructor, or a "blueprint" for creating objects. |
| 16 | +# A class is a user-defined blueprint or prototype from which objects are created. |
| 17 | +# Classes provide a means of bundling data and functionality together. |
| 18 | +# Creating a new class creates a new type of object, allowing new instances of that type to be made. |
| 19 | + |
| 20 | +class employee: |
| 21 | + # class is a keyword to create a class. class name is employee |
| 22 | + #if you create a empty class it shows an error so ypu need to put pass statement on it |
| 23 | + pass |
| 24 | + |
| 25 | +emp1=employee()# each unique employee that we create using our employee class will be an instance of the class |
| 26 | +emp2=employee() |
| 27 | +print(emp1)# <__main__.employee object at 0x7fc5ca05bfd0> emp1 object memory address |
| 28 | +print(emp2)# <__main__.employee object at 0x7fc5ca05beb0> emp2 object memory address |
| 29 | + |
| 30 | + |
| 31 | +# object: |
| 32 | +# An object is called an instance of a class.here emp1 and emp2 have unique employee classes |
| 33 | + |
| 34 | + |
| 35 | +# pass: |
| 36 | +# The pass statement is used as a placeholder for future code. |
| 37 | +# When the pass statement is executed, nothing happens, but you avoid getting an error |
| 38 | +# when empty code is not allowed. |
| 39 | +# Empty code is not allowed in loops, function definitions, class definitions, or in if statements |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +class my_class: |
| 44 | + # global variable |
| 45 | + strength='empty' |
| 46 | + classroom='None' |
| 47 | + |
| 48 | + def __init__(self,class_name="None",staff="Unallotted",fullname="None"): |
| 49 | + # self : Pointer to Current Object. staff=None is default arguments |
| 50 | + self.staff_name=staff # Instance variable creating automatically |
| 51 | + self.fullname=fullname |
| 52 | + self.classroom=class_name |
| 53 | + print(self.staff_name,self.fullname,self.classroom) |
| 54 | + |
| 55 | + |
| 56 | + def myclass(self,student): # without self keyword positional argument error occurs |
| 57 | + global strength # access the global variable change inside a function |
| 58 | + strength=student# it will work inside the |
| 59 | + return "Total strength of the class is " +strength # return statement store the value in the the function on it |
| 60 | + |
| 61 | + def myfunct(self,repname): |
| 62 | + #local variable |
| 63 | + rep=repname |
| 64 | + print(self.classroom," incharge staff ",self.staff_name," full name ",self.fullname) |
| 65 | + print(rep+" is representative for " +self.classroom) |
| 66 | + print(self.strength+" students inside the class room ") |
| 67 | + #self.strength show class variable strength if current obj not available it will take global variable |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +obj=my_class("I MCA","HPT","Harrish PT") |
| 73 | +# object creation for class (my_class is Class name).obj is instance of the class or object |
| 74 | +# you can't pass global variable inside class |
| 75 | + |
| 76 | +# print(obj) |
| 77 | +# print memory address of the object (obj) |
| 78 | + |
| 79 | +print(obj.strength) |
| 80 | +# Print obj.name is Global Variable (my_class.name) |
| 81 | + |
| 82 | +obj.staff_name="KK" |
| 83 | +obj.fullname="Kishore" |
| 84 | +# Manually created instance variable |
| 85 | + |
| 86 | +print("Manually created staff name : "+obj.staff_name) |
| 87 | +print("Manually created staff fullname : "+obj.fullname) |
| 88 | +# printing Manually created instance variable |
| 89 | + |
| 90 | +print(obj.myclass('16')) |
| 91 | +# It will print inside the myfunc stored value //Roll16 |
| 92 | + |
| 93 | +obj.myclass('15') |
| 94 | +# It will not print anything if you call the function without print statement it will print Nothing |
| 95 | + |
| 96 | +obj.myfunct('Harrish') |
| 97 | +# Here It will print the value because in myfunct contain print statement inside on it so no need to again use print function |
| 98 | + |
| 99 | +obj2=my_class("I MCA",'Dk','Dineshkumar') # create a object and pass a value to the constructor or __init__ method |
| 100 | +obj2.myfunct('Santhosh') |
| 101 | + |
| 102 | +# Instance variable: |
| 103 | +# Instance variable contains data that is unique to each instance. |
| 104 | +# Instance variable can created manually and automatically. |
| 105 | +# Manually set the variables every time its a lots of code and it also prone to mistake. |
| 106 | + |
| 107 | + |
| 108 | +# self |
| 109 | +# the term “self” refers to the instance of the class that is currently being used. |
| 110 | +# It is customary to use “self” as the first parameter in instance methods of a class. |
| 111 | +# Whenever you call a method of an object created from a class, |
| 112 | +# the object is automatically passed as the first argument using the “self” parameter. |
| 113 | +# The self is always pointing to the Current Object. |
| 114 | +# When you create an instance of a class, |
| 115 | +# you’re essentially creating an object with its own set of attributes and methods. |
| 116 | + |
| 117 | + |
| 118 | +# constructor |
| 119 | +# a class constructor is a special method named __init__ that gets called when you create an instance (object) of a class. |
| 120 | +# This method is used to initialize the attributes of the object. |
| 121 | +# Keep in mind that the self parameter in the constructor refers to the instance being created and allows |
| 122 | +# you to access and set its attributes. |
| 123 | + |
0 commit comments