CLASSES AND
OBJECTS
Simple example program
# define a class
class Dog:
sound = "bark" # class attribute
class Dog:
sound = "bark"
dog1 = Dog() # Creating object from class
print([Link]) # Accessing the class
OUTPUT:bark
Using __init__() Function
class Dog:
species = "Canine" # Class attribute
def __init__(self, name, age):
[Link] = name # Instance attribute
[Link] = age # Instance attribute
# Creating an object of the Dog class
dog1 = Dog("Buddy", 3)
print([Link])
print([Link])
OUTPUT: Buddy
Canine
Functional Programming in
Python (UNIT-5)
Functional programming is a programming paradigm in which we try
to bind everything in a pure mathematical functions style.
It is a declarative type of programming style.
Its main focus is on " what to solve" in contrast to an imperative
style where the main focus is "how to solve"
JSON in Python
JSON is a syntax for storing and exchanging data.
JSON is text, written with JavaScript object notation.
Python has a built-in package called json, which can be used to work
with JSON data.
Convert from JSON to Python
([Link]() )
import json
# some JSON:
x = '{ "name":"John", "age":30, "city":"New York"}'
# parse x:
y = [Link](x)
# the result is a Python dictionary:
print(y["age"])
OUTPUT: 30
Convert from Python to
JSON([Link]())
import json
# a Python object (dict):
x={
"name": "John",
"age": 30,
"city": "New York"
}
# convert into JSON:
y = [Link](x)
# the result is a JSON string:
print(y)
OUTPUT: {"name": "John", "age": 30, "city": "New York"}
You can convert Python objects of the
following types, into JSON strings:
dict
list
tuple
string
int
float
True
False
None