Class & Object, Set, Exception Handling and Examples

 3/5

## Python function

- Function is a small job program


## Types of function 

1) Built-in (library function)

2) User defined (created by the user)



## How to create function

To create a function 'def ' keyword is used.

def display():

    print("this is my function")

    

display()    

output:- 

this is my function



## Function argument

- Argument is used inside the function bracket

1) Actual argument (calling time)

2) Formal argument (definition time)


example:-

def display(name):                                                                (formal argument)

    print("this is my function "+ name)

    

display("mohan")                                                                  (actual argument)

output:-

this is my function mohan



## Default argument

def display(name = "Ramesh"):

    print("this is my function "+ name)

    

display()

output:-

this is my function Ramesh



## Return argument

def display(x):

    return 5*x

    

print(display(3)) 

print(display(4))

output:-

15

20

explanation:- here prgm is read from bottom to up 


## Class and Object

- Class is a collection of data members(variable) or member function(normal func.)

- Creating a class using 'class' keyword

example:-

class my:

    x = 5

    y = 6

    z = "Ramesh"

    

t1 = my()                                                                 ( 't1' is the object of 'my' class)  

print(t1.x)

print(t1.y)

print(t1.z)

output:-

5

6

Ramesh 


NOTE:- Variable inside class is data member in above ex. x, y, z are data members


## Member function

- Self is a pre-defined argument in Python.

example:-

class my:

    def display(self, name):

        self.name = name

        

t1 = my()

t1.display("Mohan")

print(t1.name)

output:- Mohan


Ex.    WAP to create biodata using class and object and function

class biodata:

    def display(self, name, course, city):

        self.name = name

        self.course = course

        self.city = city

        

t1 = biodata()

t1.display("Mohan","Python","Pune")

print(t1.name)

print(t1.course)

print(t1.city)


output:- 

Mohan

Python

Pune





4/5
## Python Mathematics
- There are some function used in python mathematics concept
1) min 
x = min(5,10,25)
print(x)
output:- 5

2) max
x = max(5,10,25)
print(x)
output:- 25

3) abs() absolute value
x = abs(-7.25)
print(x)
output:- 7.25

4) pow()   return the power
x = pow(4,3)
print(x)
output:- 64



## Include mathe library
using 'import' keyword 
ex.1:-
import math 
x = math.sqrt(64)
print(x)
output:- 8.0


ex.2:-
import math 
x = math.pi
print(x)
output:- 3.141592653589793




## Python String Format
All string written inside the "" 

x="This is my first string program"
print(x)
output:-  This is my first string program




## Format function
- Select the parts of string
Note:- {} read the string value
ex.
price = 49
txt ="The price is {} rupees "
print(txt.format(price))
output:- The price is 49 rupees




## Python Date Format
- Using date time library for displaying the all date format
NOTE:- now() return current date and time 

import datetime
x = datetime.datetime.now()
print(x)
output:-  2021-05-05 08:34:25.571520



## Display Year, Month, Day
import datetime
x = datetime.datetime.now()
print(x.year)
output:- 2021


import datetime
x = datetime.datetime.now()
print(x.month)
output:- 5


import datetime
x = datetime.datetime.now()
print(x.day)
output:- 4 
NOTE:-  It counts from saturday



## Python Iterator
- Iterator is similar to repeating the data.
ex.1:-
item = ("mango","grapes","apple","banana")
for x in item:
    print(x)
output:-
mango
grapes
apple
banana


ex.2:-
item = "mango"
for x in item:
    print(x)
output:-
m
a
n
g
o




## Python Set
- Set is a collection multiple data items
- It randomly arrange the data
ex.
theset = {"Mumbai","Pune","Nagpur","Nashik"}
print(theset)
output:-
{'Nashik', 'Pune', 'Nagpur', 'Mumbai'}




## Access the set data item
theset = {"Mumbai","Pune","Nagpur","Nashik"}
for x in theset:
    print(x)
output:- 
Nashik
Nagpur
Pune
Mumbai



## Remove the set element
theset = {"Mumbai","Pune","Nagpur","Nashik"}
theset.remove("Pune")
print(theset)
output:-  {'Nagpur', 'Mumbai', 'Nashik'}




## Join the set
set1 = {"a","b","c"}
set2 = {1,2,3}
set3 = set1.union(set2)
print(set3)
output:-  {1, 'c', 2, 3, 'b', 'a'}







5/5

## Examples:-

1) WAP to print data type of the given values
x = 1
y = 2.8
z = 1j
print(type(x))
print(type(y))
print(type(z))

output:-  
<class 'int'>
<class 'float'>
<class 'complex'>




2) WAP to input age from user and check age>18 then print  "You can vote" otherwise print " u can not vote"
age = int(input("Enter your Age "))
if age>18:
    print("You are eligible to Vote ")
    
else:
    print("You are not eligible to Vote. You are below 18")

output:- 
Enter your Age 12
You are not eligible to Vote. You are below 18




3) WAP to display 3rd position character 
a = "Hello, World!"

a = "Hello, World!"
x = a[3]
print(x)

output:-  l





4) WAP to display multiplication of this variable 
x = 5
y = 3

x = 5
y = 3
print("Multiplication of x and y is ",x*y)

output:-
Multiplication of x and y is  15




5) WAP to create list for 4 item and display all item using for loop

item = ["Cars", "Fruits", "Veggies", "Toys", "Cities"]
for i in item:
    print(i)

output:- 
Cars
Fruits
Veggies
Toys
Cities





6) WAP to create list and display the length of given list

item = ["Cars", "Fruits", "Veggies", "Toys", "Cities"]
x = len(item)
print(x)

output:-  5




7) WAP to input 3 number user choice and display greater number

a = int(input("Enter the first no."))
b = int(input("Enter the second no.")) 
c = int(input("Enter third no."))
if a > b and a > c:
    print("First no. is greater")
elif b > a and b > c:
    print("Second no. is greater")
else:
    print("Third no. is greater")
 
output:-
Enter the first no.9
Enter the second no.67
Enter third no.99
Third no. is greater




8) WAP to display sum of 1 to 5  number using while loop

i = 0         
while i<=5:
    print(i)
    i=i+1

output:-
0
1
2
3
4
5










6/5

##  Exception Handling

- Some type of error return by the user called exception.
1) Try          (program develop)
2) Except    (error msg is printed)
3) Finally

1) Try is basically used to variable related error


## Multiple exception

- NameError :- represent variable name error
- NameError is a exception keyword
- Only variable related error

example:-
try:
    print(x)
    
except NameError:
    print("variable x is not defined ")
    
except:
    print("Something got wrong ")
    
output:- variable x is not defined




## Exception using else statement

try:
    print("Hello")
    
except:
    print("Something got wrong ")
    
else:
    print("Invalid Variable ")
    
Hello
Invalid Variable 



## Finally
- If try block is raise error or not in this situation finally block is executed

try:
    print(x)
    
except:
    print("Something got wrong")
    
finally:
    print("Try block is finished ")
    
    
output:- 
Something got wrong
Try block is finished 





7/5

## Examples:-

1) WAP to display factorial number ?

n = int(input("Enter any no. "))
fact = 1
if n < 0:
   print("Factorial does not exist for negative numbers")
elif n == 0:
   print("The factorial of 0 is 1")
else:
   for i in range(1, n + 1):
       fact = fact*i
   print("The factorial of",n,"is",fact)
   
 output:-
Enter any no.5
The factorial of 5 is 120




2) WAP to display fibonacci series ?



3) WAP to display n number of table and display odd number in given table ?

ODD NO. :-
n = int(input("Enter any no."))                                          n=int(input("enter any no."))
i = n                                                                                    while n<100:
while i<100:                                           OR                                print(n)
    print(i)                                                                                      n=n+2
    i=i+2
    
output:-
Enter any no.77
77
79
81
83
85
87
89
91
93
95
97
99





TO PRINT TABLE OF ANY NO. :-
n = int(input("Enter any no."))
i = n     
while i<100:
    print(i)
    i=i+n
    
output:-
Enter any no.12
24
36
48
60
72
84
96


4) WAP to display sum of 1 to n number ?

n = int(input("Enter any no."))
i = 1     
while i<=n:
    print(i)
    i=i+1

 output:-
Enter any no.17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17



Comments

Popular posts from this blog

PyCharm Setup

JavaScript

HTML and CSS