0% found this document useful (0 votes)
7 views

Notes

student management report

Uploaded by

vidhikaliraman
Copyright
© © All Rights Reserved
0% found this document useful (0 votes)
7 views

Notes

student management report

Uploaded by

vidhikaliraman
Copyright
© © All Rights Reserved
You are on page 1/ 32

9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

In [1]:

1 print("Hello")

Hello

Comments:
'''Python is awesome language phython has simple syntax'''

Variables:
In [9]:

1 A = 10
2 print(A)

10

In [12]:

1 A = "Python"
2 print(A)

Python

In [13]:

1 A = 10.1
2 print(A)

10.1

In [14]:

1 A = True
2 print(A)

True

In [17]:

1 A, B, C = 10, 20, 30
2 print(A,B,C)
3 print(B)
4 print(C)

10 20 30
20
30

localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 1/32


9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

In [3]:

1 A = 1,2,3,4
2 a,b,c,d = A
3 print(a)
4 print(b)
5 print(c)
6 print(d)

1
2
3
4

In [23]:

1 # USER INPUT

In [4]:

1 A = input("Enter Your Name:")


2 print(" ")
3 print("My name is :",A)

Enter Your Name:Anchal

My name is : Anchal

In [30]:

1 # TASK:

Print the details of the student


1. Name
2. Batch id
3. Contact No
4. Email
5. Address
6. Technology
7. College Name

localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 2/32


9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

In [6]:

1 A = input("Enter Your Name:")


2 B = input("Enter Your Batch id:")
3 C = input("Enter Your Contact No:")
4 D = input("Enter Your Email:")
5 E = input("Enter Your Address:")
6 F = input("Enter Your Technology:")
7 G = input("Enter Your College Name:")
8 print(" ")
9 print("Name:",A)
10 print(" ")
11 print("Batch id:",B)
12 print(" ")
13 print("Contact No",C)
14 print(" ")
15 print("Email",D)
16 print(" ")
17 print("Address",E)
18 print(" ")
19 print("Technology",F)
20 print(" ")
21 print("College name",G, "😊")

Enter Your Name:Anchal


Enter Your Batch id:12345
Enter Your Contact No:1234567890
Enter Your Email:[email protected]
Enter Your Address:Mohali
Enter Your Technology:Python
Enter Your College Name:Thappar

Name: Anchal

Batch id: 12345

Contact No 1234567890

Email [email protected]

Address Mohali

Technology Python

College name Thappar 😊

In [2]:

1 # Emojis in python:

In [5]:

1 A = "😊😊😊😊"
2 print(A)

😊😊😊😊

Print the details of the employee


localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 3/32
9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

1. Employee Name
2. Employee Salary
3. Employee Designation
4. Employee Department

STRINGS:
In [3]:

1 A = "python is awesome"
2 B = A.title()
3 print(B)

Python Is Awesome

In [4]:

1 A = "PYTHON is awesome"
2 B = A.capitalize()
3 print(B)

Python is awesome

OPERATORS
1. Arithmetic
2. Comparison
3. Logical
4. Assignment
5. Membership

Airthmetic Operator:
In [3]:

1 # Addition:
2
3 A = 10
4 B = 10
5 C = A + B
6 print("Sum is :",C)

Sum is : 20

localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 4/32


9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

In [4]:

1 # Subtraction:
2
3 A = 10
4 B = 10
5 C = A - B
6 print(C)

In [5]:

1 # Multiplication:
2
3 A = 10
4 B = 10
5 C = A * B
6 print(C)

100

In [7]:

1 # Division:
2
3 A = 11
4 B = 5
5 C = A / B
6 print(C)

2.2

In [8]:

1 # Floor Division:
2
3 A = 11
4 B = 5
5 C = A // B
6 print(C)

In [13]:

1 # Exponentation:
2
3 A = 10
4 B = 3
5 print(A ** B)

1000

localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 5/32


9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

In [15]:

1 # Modulas
2
3 A = 11
4 B = 5
5 C = A % B
6 print(C)

In [17]:

1 A = int(input("Enter Your 1st Number:"))


2 B = int(input("Enter Your 2nd Number:"))
3 print(" ")
4 print(A + B)

Enter Your 1st Number:5


Enter Your 2nd Number:5

10

COMPARISON OPERATOR
1. Greater than
2. Less tham
3. Equal too
4. Not equal too
5. Greater than or equal too
6. Less than or euqal too

In [18]:

1 # Greater Than:
2
3 A = 20
4 B = 10
5 print(A > B)

True

In [19]:

1 # Less than
2
3 A = 10
4 B = 20
5 print(A < B)

True

localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 6/32


9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

In [20]:

1 # Equals too:
2
3 A = 10
4 B = 10
5 print(A == B)

True

In [21]:

1 # Not Equals too:


2
3 A = 10
4 B = 20
5 print(A != B)

True

In [25]:

1 # Greater than or equals to


2
3 A = 50
4 B = 10
5 print(A >= B)

True

In [27]:

1 # Less than or equals to


2
3 A = 0
4 B = 10
5 print(A <= B)

True

In [28]:

1 # ASSIGNMENT OPERATORS:

In [29]:

1 A = 10
2 print(A)

10

localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 7/32


9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

In [30]:

1 A = 10
2 A += 5
3 print(A)

15

In [31]:

1 A = 10
2 A -= 5
3 print(A)

In [32]:

1 A = 10
2 A /= 5
3 print(A)

2.0

In [34]:

1 A = 10
2 A //= 5
3 print(A)

In [35]:

1 A = 10
2 A *= 5
3 print(A)

50

In [36]:

1 A = 10
2 A **= 5
3 print(A)

100000

Logical operators:
1. AND
2. OR
3. NOT

localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 8/32


9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

In [40]:

1 A = 10
2 print(A > 5 and A == 10)

True

In [41]:

1 # OR:
2
3 A = 20
4 print(A == 10 or A == 20)

True

In [43]:

1 # Not
2
3 A = 10
4 print(not(A < 5 and A == 10))

True

Membership:
1. IN
2. NOT IN

In [46]:

1 A = 1,2,3,4,5
2 print(3 in A)

True

In [48]:

1 A = 1,2,3,4,5
2 print(13 not in A)

True

In [ ]:

1 A = input("Enter Your String:")


2 print(" ")
3 print("A" in A)

localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 9/32


9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

In [1]:

1 # LIST:

In [3]:

1 number = [1,2,3,4,5,6,7,8,9,10]
2 print(number)
3 print(type(number))

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
<class 'list'>

In [4]:

1 number = [1,2,3,4,5,6,7,8,9,10]
2 print(number[5])

In [5]:

1 number = [1,2,3,4,5,6,7,8,9,10]
2 print(number[0:])

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

In [6]:

1 number = [1,2,3,4,5,6,7,8,9,10]
2 print(number[:11])

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

In [7]:

1 number = [1,2,3,4,5,6,7,8,9,10]
2 print(number[::2])

[1, 3, 5, 7, 9]

In [8]:

1 number = [1,2,3,4,5,6,7,8,9,10]
2 print(number[-1:-11:-2])

[10, 8, 6, 4, 2]

In [9]:

1 # LIST FUNCTIONS:

localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 10/32


9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

In [10]:

1 number = [1,2,3,4,5,6,7,8,9,10]
2 number.append(11)
3 print(number)

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

In [11]:

1 A = [1,2,3,4]
2 B = [5,6,7,8]
3 A.extend(B)
4 print(A)

[1, 2, 3, 4, 5, 6, 7, 8]

In [13]:

1 # Insert:
2 number = [1,2,3,4,5,6,7,8,9,10]
3 number.insert(0,0)
4 print(number)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

In [14]:

1 # Remove:

In [15]:

1 number = [1,2,3,4,5,6,7,8,9,10]
2 number.remove(10)
3 print(number)

[1, 2, 3, 4, 5, 6, 7, 8, 9]

In [16]:

1 # Pop:
2
3 number = [1,2,3,4,5,6,7,8,9,10]
4 number.pop(0)
5 print(number)

[2, 3, 4, 5, 6, 7, 8, 9, 10]

In [17]:

1 # Pop:
2
3 number = [1,2,3,4,5,6,7,8,9,10]
4 number.pop()
5 print(number)

[1, 2, 3, 4, 5, 6, 7, 8, 9]

localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 11/32


9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

In [18]:

1 # clear:
2
3 number = [1,2,3,4,5,6,7,8,9,10]
4 number.clear()
5 print(number)

[]

In [20]:

1 # del
2
3 number = [1,2,3,4,5,6,7,8,9,10]
4 del number
5 # print(number)

In [21]:

1 # Copy:
2
3 number = [1,2,3,4,5,6,7,8,9,10]
4 A = number.copy()
5 print(number)
6 print(A)

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

In [22]:

1 A = [1,2,3,4]
2 B = [5,6,7,8]
3 C = A + B
4 print(C)

[1, 2, 3, 4, 5, 6, 7, 8]

In [1]:

1 # TUPLES:

In [3]:

1 fruits = ("Apple", "Mango", "Banana", "Orange", "Pear")


2 print(fruits)
3 print(type(fruits))

('Apple', 'Mango', 'Banana', 'Orange', 'Pear')


<class 'tuple'>

localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 12/32


9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

In [4]:

1 fruits = ("Apple", "Mango", "Banana", "Orange", "Pear")


2 fruits.append("Strawberry")
3 print(fruits)

--------------------------------------------------------------------------
-
AttributeError Traceback (most recent call las
t)
~\AppData\Local\Temp\ipykernel_27536\3198605891.py in <module>
1 fruits = ("Apple", "Mango", "Banana", "Orange", "Pear")
----> 2 fruits.append("Strawberry")
3 print(fruits)

AttributeError: 'tuple' object has no attribute 'append'

In [7]:

1 fruits = ("Apple", "Mango", "Banana", "Orange", "Pear")


2 A = list(fruits)
3 A.append("Strawberry")
4 fruits = tuple(A)
5 print(fruits)

('Apple', 'Mango', 'Banana', 'Orange', 'Pear', 'Strawberry')

In [8]:

1 # Practice Questions:

Dictionary:
In [20]:

1 student = {
2 "Name" : "Isha",
3 "Rollno" : 101,
4 "Tech" : "python"
5 }
6
7 print(student)
8 print(type(student))

{'Name': 'Isha', 'Rollno': 101, 'Tech': 'python'}


<class 'dict'>

In [ ]:

1 # Get:

localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 13/32


9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

In [3]:

1 student = {
2 "Name" : "Isha",
3 "Rollno" : 101,
4 "Tech" : "python"
5 }
6
7 print(student["Name"])

Isha

In [5]:

1 student = {
2 "Name" : "Isha",
3 "Rollno" : 101,
4 "Tech" : "python"
5 }
6 A = student.get("Rollno")
7 print(A)

101

In [6]:

1 # Keys():

In [7]:

1 student = {
2 "Name" : "Isha",
3 "Rollno" : 101,
4 "Tech" : "python"
5 }
6 A = student.keys()
7 print(A)

dict_keys(['Name', 'Rollno', 'Tech'])

In [8]:

1 student = {
2 "Name" : "Isha",
3 "Rollno" : 101,
4 "Tech" : "python"
5 }
6 A = student.values()
7 print(A)

dict_values(['Isha', 101, 'python'])

In [9]:

1 # Update:

localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 14/32


9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

In [10]:

1 student = {
2 "Name" : "Isha",
3 "Rollno" : 101,
4 "Tech" : "python"
5 }
6 student["Name"] = "Anchal"
7 print(student)

{'Name': 'Anchal', 'Rollno': 101, 'Tech': 'python'}

In [11]:

1 student = {
2 "Name" : "Isha",
3 "Rollno" : 101,
4 "Tech" : "python"
5 }
6 student["Email"] = "[email protected]"
7 print(student)

{'Name': 'Isha', 'Rollno': 101, 'Tech': 'python', 'Email': '[email protected]


om'}

In [13]:

1 student = {
2 "Name" : "Isha",
3 "Rollno" : 101,
4 "Tech" : "python"
5 }
6 student.update({"Name" : "Anchal"})
7 print(student)

{'Name': 'Anchal', 'Rollno': 101, 'Tech': 'python'}

In [14]:

1 student = {
2 "Name" : "Isha",
3 "Rollno" : 101,
4 "Tech" : "python"
5 }
6 student.update({"Address" : "Mohali"})
7 print(student)

{'Name': 'Isha', 'Rollno': 101, 'Tech': 'python', 'Address': 'Mohali'}

localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 15/32


9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

In [15]:

1 student = {
2 "Name" : "Isha",
3 "Rollno" : 101,
4 "Tech" : "python"
5 }
6 student.pop("Name")
7 print(student)

{'Rollno': 101, 'Tech': 'python'}

In [17]:

1 student = {
2 "Name" : "Isha",
3 "Rollno" : 101,
4 "Tech" : "python"
5 }
6 student.popitem()
7 print(student)

{'Name': 'Isha', 'Rollno': 101}

In [18]:

1 # clear:

In [19]:

1 student = {
2 "Name" : "Isha",
3 "Rollno" : 101,
4 "Tech" : "python"
5 }
6 student.clear()
7 print(student)

{}

In [26]:

1 A = [1,2,4,5,76,97,9,43,3,34]
2 A.sort(reverse = True)
3 print(A)

[97, 76, 43, 34, 9, 5, 4, 3, 2, 1]

In [32]:

1 a = "python"
2 print(a[-1:-7:-1])

nohtyp

SETS:
localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 16/32
9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

In [2]:

1 A = {1,2,3,5,65,76,3,45,5,3}
2 print(A)

{65, 2, 3, 1, 5, 76, 45}

In [3]:

1 # Add:

In [5]:

1 A = {1,3,35,46,57,97,3,4,"isha"}
2 A.add("Anchal")
3 print(A)

{1, 97, 3, 4, 35, 46, 'isha', 57, 'Anchal'}

In [9]:

1 A = {1,3,35,46,57,97,3,4,"isha"}
2 A.pop()
3 print(A)

{97, 3, 4, 35, 46, 'isha', 57}

In [13]:

1 A = {1,2,3,43,54,45}
2 B = {23,34,35,45}
3 C = A.union(B)
4 print(C)

{1, 2, 3, 34, 35, 43, 45, 54, 23}

In [14]:

1 A = {1,3,35,46,57,97,3,4,"isha"}
2 A.clear()A = {1,2,3,43,54,45}
3 B = {23,34,35,45}
4 C = A.union(B)
5 print(C)
6 print(A)

set()

In [17]:

1 A = {1,2,3,43,54,45}
2 B = {23,34,35,45}
3 C = A.intersection(B)
4 print(C)

{45}

localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 17/32


9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

Conditional Statements:
1. if
2. if else
3. elif
4. Nested if

In [20]:

1 A = 10
2 if A < 5:
3 print("Sucess")

In [21]:

1 # if else:

In [23]:

1 A = 10
2 if A < 5:
3 print("Hello")
4 else:
5 print("By")

By

In [24]:

1 # WAP to check weather a number is even or odd..

In [26]:

1 A = int(input("Enter Your Number:"))


2 if (A % 2) == 0:
3 print("The number is even.")
4 else:
5 print(" ")
6 print("The number is odd.")

Enter Your Number:5

The number is odd.

In [27]:

1 # elif:

localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 18/32


9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

In [30]:

1 A = 10
2 if A < 6:
3 print("hello")
4 elif A < 10:
5 print("hy")
6 elif A < 6:
7 print('By')
8 else:
9 print("Good Bye")

Good Bye

CREATE A NUMBER GUESSING GAME:

FOR LOOP:
In [3]:

1 A = [1,2,3,4,5,6]
2 for i in A:
3 print(i)

1
2
3
4
5
6

In [4]:

1 A = (1,2,3,4,6,7)
2 for j in A:
3 print(j)

1
2
3
4
6
7

In [5]:

1 # Range()

localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 19/32


9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

In [8]:

1 for i in range(10):
2 print(i)

0
1
2
3
4
5
6
7
8
9

In [13]:

1 for i in range(2,20,2):
2 print(i)

2
4
6
8
10
12
14
16
18

In [14]:

1 # NESTED FOR LOOP:

localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 20/32


9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

In [15]:

1 for i in range(5): # Outer Loop


2 for j in range(5): # Inner Loop
3 print(i,j)

0 0
0 1
0 2
0 3
0 4
1 0
1 1
1 2
1 3
1 4
2 0
2 1
2 2
2 3
2 4
3 0
3 1
3 2
3 3
3 4
4 0
4 1
4 2
4 3
4 4

In [16]:

1 *
2 **
3 ***
4 ****
5 *****

File "C:\Users\mitta\AppData\Local\Temp\ipykernel_4220\4239558033.py", l
ine 1
*
^
SyntaxError: invalid syntax

localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 21/32


9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

In [20]:

1 for i in range(5):
2 for j in range(5):
3 if i >= j:
4 print("*", end = '')
5 else:
6 print(" ", end = '')
7 print(" ")

*
**
***
****
*****

FUNCTIONS:
Function is a block of code which only runs when it is called

In [4]:

1 def employee(): # Function Definition


2 print("My name is Isha.") # Block of code
3 employee() # Function Calling
4 employee()

My name is Isha.
My name is Isha.

PARAMETERS:
In [10]:

1 def employee(name,project):
2 print("My name is",name)
3 print("I am working on",project,"project.")
4 employee("Anchal", "NLP")
5 print(" ")
6 employee("Priyanka", "BCPH")

My name is Anchal
I am working on NLP project.

My name is Priyanka
I am working on BCPH project.

In [11]:

1 # Arbitary Arguments: (*)

localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 22/32


9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

In [15]:

1 def student(*name):
2 print("Tooper of the class is",name[3])
3 print("Topper of the class is",name[2])
4 student("Isha", "Anmol", "Kunal", "Sushil")
5 # print(" ")
6 # student("Isha", "Anmol", "Kunal", "Sushil")

Tooper of the class is Sushil


Topper of the class is Kunal

In [16]:

1 # KEYWORD ARGUMENTS:

In [17]:

1 def employee(name,project,email):
2 print("My name is",name)
3 print("I am working on",project,"project.")
4 print("My mail is",email)
5 employee(name = "Anchal", project = "NLP", email = "[email protected]")

My name is Anchal
I am working on NLP project.
My mail is [email protected]

In [18]:

1 # DEFAULT PARAMETER VALUE:

In [22]:

1 def country(name = "India"):


2 print("I am from",name)
3 country("Canada")
4 country()

I am from Canada
I am from India

In [23]:

1 # Pass Statement:

In [25]:

1 def student():
2 pass
3 student()

localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 23/32


9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

In [26]:

1 # RETURN STATEMENT:

In [29]:

1 def add(a):
2 return 5 * a
3 print(add(5))
4 print(add(6))

25
30

WAP to calculate the sum of 2 numbers:

WAP to check weather a number is even or odd if


number is even print you are eligible else print not
eligible

WAP to find largest number from the sequence:


In [1]:

1 # OOPS:

In [2]:

1 # CLASS:

In [5]:

1 class Employee:
2 def __init__(self,Name,Project):
3 self.Name = Name
4 self.Project = Project
5 def result(self):
6 print(self.Name)
7 print(self.Project)
8 emp = Employee("Anchal", "BCPH")
9 emp.result()

Anchal
BCPH

localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 24/32


9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

In [7]:

1 class Student:
2
3 def details(self,Name, Address):
4 print("My name is",Name)
5 print("I lives in",Address)
6 st = Student()
7 st.details("Anchal", "Mohali")

My name is Anchal
I lives in Mohali

INHERITANCE:
1. Single
2. Multiple
3. Multilevel
4. Hierarchical
5. Hybrid

In [1]:

1 class parent:
2 def pinfo(self, name):
3 print("My name is",name)
4 class child(parent):
5 def cinfo(self, hobby):
6 print("My hobby is",hobby)
7 ch = child()
8 ch.pinfo("ABC")
9 ch.cinfo("playing guitar")

My name is ABC
My hobby is playing guitar

In [2]:

1 # MULTIPLE:

localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 25/32


9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

In [3]:

1 class father:
2 def finfo(self,name):
3 print("My name is",name)
4 class mother:
5 def minfo(self, cooking):
6 print("I love to cook",cooking)
7 class child(father,mother):
8 def cinfo(self, study):
9 print("I have done",study)
10 ch = child()
11 ch.finfo("Anchal")
12 ch.minfo("Indian Food")
13 ch.cinfo("M.Tech")

My name is Anchal
I love to cook Indian Food
I have done M.Tech

In [4]:

1 # MULTILEVEL:

In [5]:

1 class grandfather:
2 def ginfo(self,name):
3 print("My name is",name)
4 class father(grandfather):
5 def finfo(self, cooking):
6 print("I love to cook",cooking)
7 class child(father):
8 def cinfo(self, study):
9 print("I have done",study)
10 ch = child()
11 ch.ginfo("Anchal")
12 ch.finfo("Indian Food")
13 ch.cinfo("M.Tech")

My name is Anchal
I love to cook Indian Food
I have done M.Tech

In [6]:

1 # Hierarchical:

localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 26/32


9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

In [8]:

1 class parent:
2 def pinfo(self, Name, Hair_color):
3 print("My name is", Name)
4 print("My hair color is",Hair_color)
5 class Child1(parent):
6 def ch1(self, hobby):
7 print("My hobby is",hobby)
8 class Child2(parent):
9 def ch2(self, study):
10 print("I have done",study)
11 c1 = Child1()
12 c1.pinfo("ABC", "Black")
13 c1.ch1("reading books")
14 print(" ")
15 c2 = Child2()
16 c2.pinfo("XYZ", "Black")
17 c2.ch2("MCA")

My name is ABC
My hair color is Black
My hobby is reading books

My name is XYZ
My hair color is Black
I have done MCA

In [9]:

1 # HYBRID:

In [10]:

1 class vehicle:
2 def vinfo(self):
3 print("THIS IS VEHICLE CLASS")
4 class car(vehicle):
5 def cinfo(self):
6 print("THIS IS CAR CLASS")
7 class truck(vehicle):
8 def tinfo(self):
9 print("THIS IS TRUCK CLASS")
10 class sportscar(car,vehicle):
11 def sinfo(self):
12 print("THIS IS SPORTS CAR CLASS")
13 sc = sportscar()
14 sc.vinfo()
15 sc.cinfo()
16 sc.sinfo()

THIS IS VEHICLE CLASS


THIS IS CAR CLASS
THIS IS SPORTS CAR CLASS

ENCAPSULATION:

localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 27/32


9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

Encapsulation is the process of bundling of data members and methods into a single unit.

In [1]:

1 class Employee:
2 def __init__(self, name, project):
3 self.name = name
4 self.project = project
5 def show(self):
6 print(self.name)
7 print(self.project)
8 em = Employee("ABC", "XYZ")
9 em.show()

ABC
XYZ

ACCESS MODIFIERS:
1. Public
2. Private
3. Protected

In [2]:

1 # Public:

In [6]:

1 class employee:
2 def __init__(self, name, address):
3 self.name = name
4 self.address = address
5 def show(self):
6 print("Name:",self.name)
7 print("Address:",self.address)
8 emp = employee("Anchal", "Mohali")
9 emp.show()
10 print(" ")
11 print("Name:", emp.name)
12 print("Address:", emp.address)

Name: Anchal
Address: Mohali

Name: Anchal
Address: Mohali

In [7]:

1 # PRIVATE:

localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 28/32


9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

In [5]:

1 class employee:
2 def __init__(self, __name, address):
3 self.__name = __name
4 self.address = address
5 def show(self):
6 print("Name:",self.__name)
7 print("Address:",self.address)
8 emp = employee("Anchal", "Mohali")
9 emp.show()
10 # NAME MAGLING
11 print("Private member is accessed outside the class:")
12 print("Name:", emp._employee__name)

Name: Anchal
Address: Mohali
Private member is accessed outside the class:
Name: Anchal

In [6]:

1 # PROTECTED MEMBERS:

In [9]:

1 class Company:
2 def __init__(self):
3 self._project = "BCPH"
4 class Employee(Company):
5 def __init__(self, name):
6 self.name = name
7 Company.__init__(self)
8 def show(self):
9 print("Name:",self.name)
10 print("Project:", self._project)
11 emp = Employee("Anchal")
12 emp.show()
13
14 print(" ")
15 print("Project:", emp._project)

Name: Anchal
Project: BCPH

Project: BCPH

POLYMORPHISM
In [10]:

1 A = 1,2,3,4,5
2 print(len(A))

localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 29/32


9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

In [11]:

1 A = [1,2,3,3,4,5]
2 print(len(A))

In [15]:

1 class Vehicle:
2 def __init__(self, name, color):
3 self.name = name
4 self.color = color
5 def show(self):
6 print("Vehicle name is",self.name)
7 print("It's color is",self.color)
8
9 def max_speed(self):
10 print("Maximum speed of a vehicle is 220")
11 class Car(Vehicle):
12 def max_speed(self):
13 print("Maximum speed of a car is 420")
14 c = Car("Scorpio", "Black")
15 c.show()
16 c.max_speed()
17 print(" ")
18 v = Vehicle("Bike", "Black")
19 v.show()
20 v.max_speed()

Vehicle name is Scorpio


It's color is Black
Maximum speed of a car is 420

Vehicle name is Bike


It's color is Black
Maximum speed of a vehicle is 220

TKINTER:

localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 30/32


9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

In [*]:

1 from tkinter import *


2 # Window:
3 root = Tk()
4 root.geometry('600x500')
5 root.title("My first window")
6 root.config(bg = '#F08080')
7 # root.resizable(width = False, height = True)
8 # Function:
9
10 def submit():
11 user = StringVar()
12 passwd = StringVar()
13
14 user = e1.get()
15 passwd = e2.get()
16
17 if user == 'root' and passwd == 'solitaire':
18 print("Welcome",user,"to our services...")
19 else:
20 print("Sorry",user,"you have invalid credentials.")
21
22 print("Your password is",passwd)
23
24 # LABELS:
25 # PACK FUNCTION
26 l1 = Label(root, text = 'LOGIN PAGE', font = ('algerian', 20, 'bold'),
27 width = 10, height = 1, bg = 'black', fg = 'white', bd = 6,
28 relief = 'sunken')
29 l1.pack()
30 # PLACE FUNCTION
31 l2 = Label(root, text = 'USERNAME', font = ('algerian', 20, 'bold'),
32 width = 10, height = 1, bg = 'yellow', fg = 'black', bd = 6,
33 relief = 'sunken')
34 l2.place(x = 50, y = 80)
35
36 l3 = Label(root, text = 'PASSWORD', font = ('algerian', 20, 'bold'),
37 width = 10, height = 1, bg = 'yellow', fg = 'black', bd = 6,
38 relief = 'sunken')
39 l3.place(x = 50, y = 160)
40 # ENTRY:
41 e1 = Entry(root, width = 20, bg = 'light grey', font = ('algerian',10))
42 e1.place(x = 250, y = 90)
43
44 e2 = Entry(root, width = 20, bg = 'light grey', font = ('algerian',10), show = "*")
45 e2.place(x = 250, y = 175)
46 # Button:
47 b1 = Button(root,text = 'SUBMIT', font = ('algerian', 20, 'bold'),
48 width = 8, height = 1, bg = 'green', fg = 'black', bd = 6,
49 relief = 'sunken', activebackground = 'red', command = submit)
50 b1.place(x = 150, y = 250)
51 root.mainloop()

Sorry amnchal you have invalid credentials.


Your password is 1234567890
Welcome root to our services...
Your password is solitaire

localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 31/32


9/15/23, 2:23 PM Python Batch 1 (10 to 12) - Jupyter Notebook

In [ ]:

1 # SIGN UP PAGE:
2 1. First_Name
3 2. Last_Name
4 3. Username
5 4. Password
6 5. Confirm_Passwordwww4eee
7 6. Email
8 7. Contact_No

localhost:8888/notebooks/Python Batch 1 (10 to 12).ipynb 32/32

You might also like