Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

GGS COLLEGE OF MODERN TECHNOLOGY

KHARAR

ASSIGNMENT – III
PROGRAMING IN PYTHON
BTCS510-18

Submitted to :- Munish Sir


Submitted by:- Eshan Padhiar
Roll No: 2103769
Course: B. Tech
Semester: 5th
Department: CSE
Ques 1. Define OOPS.
Answer : In Python, object-oriented Programming (OOPs) is a programming paradigm that
uses objects and classes in programming. It aims to implement real-world entities like
inheritance, polymorphisms, encapsulation, etc. in the programming. The main concept of
OOPs is to bind the data and the functions that work on that together as a single unit so that
no other part of the code can access this data.
OOPs Concepts in Python
 Class
 Objects
 Polymorphism
 Encapsulation
 Inheritance
 Data Abstraction

Ques 2. Define Object.


Answer : In Python, an "object" is a fundamental concept that represents a self-contained
unit of data and behavior. Objects are instances of classes, which serve as blueprints or
templates for creating objects. Each object has attributes (data) and methods (functions)
associated with it. Here are the key characteristics of objects in Python:
 Instances of Classes: Objects are created based on the definitions provided by classes.
A class defines the structure and behavior of objects. Objects are instances of a
particular class.
 Attributes (Data): Objects have attributes, which are variables that store data specific
to that object. These attributes can be accessed using dot notation.

Methods (Functions): Objects have methods, which are functions associated with the
object's behavior. Methods are called on objects to perform specific tasks.

 Identity and State: Each object has a unique identity, meaning no two objects are
exactly the same in memory. The state of an object refers to the values of its attributes
at any given moment.
 Object-Oriented Programming: Objects are fundamental to the object-oriented
programming (OOP) paradigm. In OOP, you model real-world entities and their
behavior using classes and create instances (objects) of those classes to represent
individual entities.
 Encapsulation: Objects are encapsulated, meaning they bundle data and the methods
that operate on that data into a single unit. This encapsulation provides data hiding and
allows objects to have well-defined interfaces for interaction.
 Abstraction: Abstraction allows objects to represent real-world entities while hiding
the complex details. Users of objects only need to know how to interact with them
through their methods and attributes without being concerned about the internal
implementation.
Objects play a central role in Python, and they are used extensively in creating and modeling
software. Python is an object-oriented language, and many of its standard libraries and third-
party packages are organized around classes and objects. By defining and using objects,
developers can model and manipulate data in a way that's more intuitive and organized.
Ques 3. What is Function?
Answer : In Python, a function is a block of reusable code that performs a specific task or a
set of related tasks. Functions are a fundamental concept in programming and play a key role
in organizing and structuring code. They allow you to encapsulate a sequence of statements
into a single unit, which can be called and executed multiple times with different inputs.
Here are some important characteristics of functions in Python:
 Function Definition: A function is defined using the def keyword, followed by the
function name, a pair of parentheses, and a colon. The function definition specifies its
name, parameters (if any), and the code to be executed when the function is called.

Function Call: To execute a function, you call it by using its name followed by parentheses.
You can provide arguments or inputs to the function within the parentheses.

Parameters: Functions can accept zero or more parameters (also called arguments). These
parameters are placeholders for the values that you pass to the function when you call it. The
function can use these values within its code.

Return Statement: A function can return a value using the return statement. The returned
value is the result of the function's computation, and it can be assigned to a variable or used
in expressions.
 Function Reusability: One of the primary benefits of functions is their reusability.
You can call a function as many times as needed with different inputs, which promotes
code modularity and reduces redundancy.
 Scoping: Functions in Python have their own local scope, meaning variables defined
within a function are not visible outside of it. This supports the principle of
encapsulation and helps prevent naming conflicts.
 Built-in Functions: Python provides a rich set of built-in functions like print(), len(),
input(), and many more. These functions are available for general use and provide
essential functionality.
 User-Defined Functions: In addition to built-in functions, you can create your own
custom functions to suit your specific needs and to perform tasks that are not covered
by built-in functions.
Functions are a fundamental building block in Python and are crucial for writing organized,
maintainable, and efficient code. They allow you to break down complex problems into
smaller, manageable units and promote code reusability and readability.

Ques 4. What is list.


Answer : In Python, a list is a versatile and fundamental data structure that is used to store
a collection of items. Lists are ordered, mutable (changeable), and can contain elements of
different data types, including numbers, strings, other lists, or any Python objects. Lists are
defined by enclosing a comma-separated sequence of values within square brackets []. Here
are some key characteristics and operations associated with lists in Python:
 Creating a List: You can create a list by enclosing a sequence of values in square
brackets.

Accessing Elements: Elements within a list are accessible by their index, starting from 0 for
the first element.

Modifying a List: Lists are mutable, which means you can change their contents. You can
assign new values to specific indices, append elements, remove elements, and more.

Ques 5. How is the python program executed?


Answer : The execution of a Python program involves several steps, from writing the code
to running it. Here are the main steps in the execution process of a Python program:
 Writing the Code: You start by writing your Python code in a text editor or an
integrated development environment (IDE). Python code is typically written in text
files with a .py extension.
 Editing and Debugging: During this phase, you can edit, test, and debug your code to
ensure it functions as expected. Many text editors and IDEs provide debugging tools
and features to help you identify and fix errors.
 Saving the File: Once you are satisfied with your code, you save the Python file with
a .py extension. This file will contain the source code of your program.
 Python Interpreter: To execute a Python program, you need a Python interpreter.
The Python interpreter is a software application that reads and interprets your Python
code and executes it.
 Running the Program: You can run your Python program in several ways:

Command Line: You can open a command prompt or terminal, navigate to the directory
containing your Python file, and use the python command followed by the filename to run
the program. For example:


o Interactive Shell: You can enter Python code directly into the interactive
Python shell (also known as the REPL - Read-Eval-Print Loop) by typing
python or python3 in the command line. This is useful for experimenting with
small code snippets or testing individual lines of code.
o IDEs: Integrated development environments (IDEs) provide a user-friendly
interface for writing, running, and debugging Python code. IDEs often have
built-in run and debug buttons.
 Parsing and Execution: When you run your Python program, the Python interpreter
parses your code line by line, executing each line as it encounters it. The interpreter
translates your Python code into machine code that the computer's CPU can
understand.
 Output and Interaction: During execution, your Python program can produce output,
which is typically displayed in the command line or terminal if you use print
statements. Additionally, you can interact with the program by providing input
through the command line or other input methods.
 Termination: Once your program has executed all its statements or encountered an
exit condition (e.g., an exception or reaching the end of the code), it terminates, and
the Python interpreter releases the allocated resources.
 Error Handling: If errors are encountered during the execution of your program, the
Python interpreter generates error messages, which can be used for debugging and
identifying issues in your code.
 Result: The result of the program's execution depends on what the program was
designed to do. It could be generating output, performing calculations, interacting with
the user, or any other specific task.
These are the general steps involved in executing a Python program. The process may vary
slightly depending on the development environment and tools you use. Python's simplicity
and readability make it accessible for both beginners and experienced developers to write,
test, and run code effectively.
Ques 6. How expression is used in python
Answer : In Python, expressions are fundamental components of the language and are used
to represent and evaluate different operations or computations. An expression is a
combination of values, variables, operators, and function calls that are evaluated to produce a
single value. Expressions can be simple or complex, and they can be used in various contexts
within the code. Here are some common ways expressions are used in Python:
 Arithmetic Operations: Expressions are frequently used for performing arithmetic
operations such as addition, subtraction, multiplication, division, and more.

Boolean Operations: Expressions can also be used in boolean contexts for comparisons,
logical operations, and conditional statements.

Function Calls: Function calls are expressions that can return a value, and they are used to
perform specific tasks or operations within the code.
List and Dictionary Comprehensions: List and dictionary comprehensions allow you to
create new lists or dictionaries using expressions that operate on existing iterables.
String Operations: Expressions can be used to manipulate strings, concatenate them, or
extract substrings using slicing or other string methods.
Conditional Expressions (Ternary Operator): Python supports a ternary operator for
conditional expressions, allowing you to write compact conditional statements in a single
line.
Bitwise Operations: Bitwise operations can be used to manipulate binary values at the bit
level, and they are often used in various low-level operations.
Ques 7. What is Datatypes? explain its types.
Answer: In programming, data types define the kind of data that a variable or object can
hold. They specify the size, format, and operations that can be performed on the data.
Python, like many programming languages, supports various data types. Here are some of
the fundamental data types in Python:
 Numeric Data Types:
o int: The int data type represents integers, which are whole numbers, both
positive and negative. Example: 5, -42, 0.
o float: The float data type represents floating-point numbers, which include
decimal fractions. Example: 3.14, -0.5, 2.0.
o complex: The complex data type represents complex numbers with a real and
imaginary part. Example: 2+3j, 1-2j.
 Sequence Types:
o str (String): The str data type represents text or character sequences. Example:
"Hello, World!", 'Python'.
o list: A list is an ordered and mutable collection of items. Example: [1, 2, 3,
"apple"].
o tuple: A tuple is an ordered and immutable collection of items. Example: (1, 2,
3, "banana").
 Mapping Type:
o dict (Dictionary): The dict data type represents key-value pairs, allowing you
to store and retrieve values based on their associated keys. Example: {"name":
"Alice", "age": 30}.
 Set Types:
o set: A set is an unordered collection of unique items. Example: {1, 2, 3}.
o frozenset: A frozenset is an immutable set. Once created, its contents cannot be
changed. Example: frozenset({1, 2, 3}).
 Boolean Type:
o bool: The bool data type represents Boolean values, which can be either True
or False. Example: True, False.
 Binary Types:
o bytes: The bytes data type represents sequences of bytes and is immutable.
Example: b'Hello'.
o bytearray: The bytearray data type represents mutable sequences of bytes.
Example: bytearray(b'World').
 None Type:
o None: The None type represents a null or absence of a value. It is often used to
indicate the absence of a return value or as an initial value for variables.
 Type Objects:
o type: The type data type is used to represent the data type of an object. It can be
used to check the type of an object.
Python's dynamic typing allows variables to change their data type dynamically. For
example, a variable initially holding an integer value can later store a string or any other data
type. However, you should be mindful of the data types used in your code to ensure proper
data manipulation and avoid unexpected errors.
You can use functions like type(), isinstance(), and casting functions like int(), str(), and
float() to work with and manipulate data types in Python. Understanding and correctly using
data types is essential for writing reliable and efficient Python programs.

You might also like