Application Development using Python Unit - 1


Unit 1

1. What is Python and What is Objects?     4M 

Python: 

Python is a general-purpose, dynamically typed, high-level, compiled and interpreted, garbage collected, and purely object-oriented programming language that supports procedural, object-oriented, and functional programming. Python is a widely used programming language that offers several unique features and advantages compared to languages like Java and C++.

Python uses:

  1. Web development (server side)
  2. Software development
  3. Mathematics
  4. System scripting

Python Objects: 

Class

A class is a user-defined data type. It consists of data members (variables) and member functions, which can be accessed and used by creating an object of that class.

It represents the set of properties or methods that are associated with all objects of one type. A class is like a blueprint for an object.

ex:

Fields

  • Name: String
  • Size: int
  • Rotate
  • Moved

Objects

A class can be used to create multiple objects.

  • Object 1 — Name: "Object 1", Size: 2
  • Object 2 — Name: "Object 2", Size: 3
  • Object 3 — Name: "Object 3", Size: 1

Note: Each object has its own values for fields and can call the methods found in the class.

2. Brefly Explain about Objects and Characteristics of Objects?  10M

Objects :

In Python, an object is a fundamental concept representing a specific instance of a class. It encapsulates data (attributes) and behavior (methods) into a single entity. Essentially, everything in Python is an object, including integers, strings, lists, and functions.

  • An object is a basic unit of an object-oriented programming language. An entity that has state and behavior is called an object in Python or other object-oriented programming languages.
  • Here, the state represents properties, and behavior represents actions or functionality. Hence, objects in Python consist of states or a list of data members (called attributes) and behaviors (called functions). An object is an instance of a defined class. Each instance of an object holds its own real data or value.
  • In Python, an object can refer to anything that can be assigned to a name, such as integers, strings, floats, classes, methods, and modules. It is a collection of data and methods that utilize that data.

Characteristics of Object :

1. State

The specific data held in an object's member variables is called the state of the object. It is also called properties or attributes of the object.

It is typically represented by variables. During the execution of a program, the state of an object may change.

2. Behavior

The behavior represents functionality or actions that an object can perform. It is usually represented by functions (or methods) within a class. Methods are used to change the state of an object.

3. Identity

The identity represents the unique name of an object. It differentiates one object from another. The unique name of an object is used to identify the object.

Syntax

class ClassName:
    self.attribute1 = attribute1
    self.attribute2 = attribute2

    # Method
    def method_name(self):

Creating an object of the class:
object_name = ClassName(attribute1_value, attribute2_value)

The __init__ Method

We use the __init__ method to initialize the state of an object or to create an instance of a class. The __init__ method requires only the self argument, which refers to the newly generated class instance.

Example: Define a class

class Car:

    def __init__(self, brand, model):
        self.brand = brand
        self.model = model

Here, __init__ is the initialization method.

Create an Object (Instance) of the Car Class

my_car = Car("Toyota", "Corolla")

Access Attributes

print(my_car.brand)
print(my_car.model)

Output

Toyota
Corolla

3. Write a Standard Data Types?

“Standard Types” are typically the most basic, core types like integers,

floating-point numbers, booleans, and characters. “Other Built-in Types”

might include more specialized types that are still part of the language’s

foundation, such as strings, lists, dictionaries, or sets.

Standard Types

These are the most common and fundamental data types that are typically built into

a programming language's core. Examples include:

1. Integers

This data type is represented with the help of int class. It consists of positive

or negative whole numbers (without fractions or decimals). In Python, there's no

limit to how long integer values are often.

Example:

a = 2
print(a, "is of type", type(a))

2. Float

The float class represents this type. It is a real number with a floating-point

representation. It is specified by a decimal point. Optionally, the character

or E followed by a positive or negative integer could even be appended to specify

scientific notation.

Example:

b = 1.5
print(b, "is of type", type(b))

Output:

1.5 is of type <class 'float'>

3. Boolean Data Type

Boolean data type in Python includes two values: True and False.

These values are used to represent the truth values of expressions.

Boolean values are often used in conditional statements and to control

the flow of a program.

To create a Boolean variable in Python, simply assign a Boolean value

to a variable name:

x = True
y = False

You can also use comparison operators to create Boolean values:

a = 5
b = 3
c = (a > b)   # Output: True
d = (a < b)   # Output: False

4. Complex

This data type stores complex type numbers. Moreover, such numbers have two parts:

real and imaginary.

Examples:

2+4j
6-8j

4. Write a Built-in Types Data types?

Data types represent the value type that helps understand what operations you can
perform on a particular piece of data. To define the values of various data types 
of Python and check their data types we use the type() function. The following 
are the standard or built-in data types in Python:
  • Numeric
  • Sequence Type
  • Boolean
  • Set
  • Dictionary
                                                    


Number Data Type

We store numeric values in this data type. Furthermore, it has three different types as follows:

a) Int

int stores all the integer numbers. Moreover, these numbers can be positive or negative.

For example: -1, 99, 10, 25, etc.

b) Float

float data type stores the real or floating-point numbers. This means that it stores all the numbers having decimal values. Moreover, they can be negative or positive.

For example: 4.0, -2.89, 3.45, etc.

c) Complex

This data type stores complex type numbers. Moreover, such numbers have two parts, real and imaginary.

For example: 2+4j, 6-8j

Example:

a = 150

print("a value", a, b=20.846)

print("b value", b)

Output:

a value 150

b value 20.846

5. Write a Sequence Data Types?

1. String Data Type

  • A group of one or more characters enclosed in a single, double, or triple
quote is called a string in Python.
  • A character in Python is just a string with a length of one; there is
     no character data type.
Example:
str = "Hello World!"
print(str)

Output:

Hello World!

2. List Data Type

Python list is an arranged grouping of the same or dissimilar elements that

are enclosed by brackets [] and separated by commas. Use the index number

to retrieve the list entries. To access a particular item in a list, use the

index operator [].

Example:

lst = ['abcd', 786, 2.23]

print(lst)
print(lst[0])

Output:

['abcd', 786, 2.23]
abcd

3. Tuple Data Type

  • A tuple in Python is an ordered list of elements, just like a list.
  • Tuples are not changeable, which is the only difference.
  • Once created, tuples cannot be changed.
  • In Python, items of a tuple are stored using parentheses ().
  • In Python, we utilize the index number to retrieve tuple items,
     just like with lists.

Example:

tuple = ('abcd', 786, 2.23)

print(tuple)
print(tuple[0])

Output:

('abcd', 786, 2.23)
abcd

6. Write a Dictionary Data Type?

Dictionary:

  • Python's dictionary is an unordered collection of data values that
     is used to store data values in a map-like fashion.
  • Unlike other data types, which only include a single value per element,
     dictionaries contain a key-value pair.
  • The dictionary contains key-value pairs to make it more effective.
    In a dictionary, a colon (:) separates each key-value pair, while a comma
    separates each key. A dictionary in Python can be made by enclosing a list of
    elements in curly {} braces and separating them with commas.

Example:

dict = {}

dict['one'] = "This is one"

dict[2] = "This is two"

print(dict['one'])

print(dict[2])

Output:

This is one

This is two

Boolean Data Type:

One of the built-in data types in Python is the boolean type, which can represent

either True or False. Any expression can be evaluated for value using the Python

bool() method, which returns True or False depending on the expression.

Set Data Type:

A set is an unordered collection of distinct components. This means that set

elements cannot be repeated, and the order in which they are kept is irrelevant.

Sets are changeable, which means that after they are constructed, their elements

can be added or withdrawn. A set's elements, on the other hand, cannot be modified

in place.Curly brackets are used to define sets.

Example:

my_set = {1, 2, 3, 4, 5}

print(my_set)

Output:

{1, 2, 3, 4, 5}



No comments:

Post a Comment