0% found this document useful (0 votes)
4 views9 pages

Python Classes, JSON, and Functional Programming

The document provides an overview of classes and objects in Python, including examples of defining a class and creating objects with attributes. It also covers functional programming principles, emphasizing the declarative style of programming, and explains how to work with JSON data in Python, including converting between JSON and Python objects. Key functions like json.loads() and json.dumps() are demonstrated for parsing and generating JSON strings.

Uploaded by

neeraja
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Python Classes, JSON, and Functional Programming

The document provides an overview of classes and objects in Python, including examples of defining a class and creating objects with attributes. It also covers functional programming principles, emphasizing the declarative style of programming, and explains how to work with JSON data in Python, including converting between JSON and Python objects. Key functions like json.loads() and json.dumps() are demonstrated for parsing and generating JSON strings.

Uploaded by

neeraja
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

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

You might also like