App Unit 1 Notes
App Unit 1 Notes
Paradigm can also be termed as method to solve some problem or do some task. Programming
paradigm is an approach to solve problem using some programming language or also we can say
it is a method to solve a problem using tools and techniques that are available to us following
some approach. There are lots for programming language that are known but all of them need to
follow some strategy when they are implemented and this methodology/strategy is paradigms.
Apart from varieties of programming language there are lots of paradigms to fulfill each and
every demand.
• So we need multiple paradigms and we need to combine them in the same program
Program
A programming language is a set of instructions and syntax used to create software programs.
A programming language is a formal language that specifies a set of instructions for a computer
to perform specific tasks. It’s used to write software programs and applications, and to control
and manipulate computer systems. There are many different programming languages, each with
its own syntax, structure, and set of commands. Some of the most commonly used programming
languages include Java, Python, C++, JavaScript, and C#.
● A programming language must be simple, easy to learn and use, have good
readability, and be human recognizable.
● A portable programming language is always preferred.
● Programming language’s efficiency must be high so that it can be easily converted
into a machine code and its execution consumes little space in memory.
● A programming language should be well structured and documented so that it is
suitable for application development.
● Necessary tools for the development, debugging, testing, maintenance of a program
must be provided by a programming language.
● A programming language should provide a single environment known as Integrated
Development Environment(IDE).
● A programming language must be consistent in terms of syntax and semantics.
ELEMENTS OF PROGRAMMING LANGUAGES
Basic Terminologies in Programming Languages:
● Algorithm: A step-by-step procedure for solving a problem or performing a task.
● Variable: A named storage location in memory that holds a value or data.
● Data Type: A classification that specifies what type of data a variable can hold, such
as integer, string, or boolean.
● Function: A self-contained block of code that performs a specific task and can be
called from other parts of the program.
● Control Flow: The order in which statements are executed in a program, including
loops and conditional statements.
● Syntax: The set of rules that govern the structure and format of a programming
language.
● Comment: A piece of text in a program that is ignored by the compiler or interpreter,
used to add notes or explanations to the code.
● Debugging: The process of finding and fixing errors or bugs in a program.
● IDE: Integrated Development Environment, a software application that provides a
comprehensive development environment for coding, debugging, and testing.
● Operator: A symbol or keyword that represents an action or operation to be
performed on one or more values or variables, such as + (addition), – (subtraction), *
(multiplication), and / (division).
● Statement: A single line or instruction in a program that performs a specific action or
operation.
Low-level language is machine-dependent (0s and 1s) programming language. The processor
runs low- level programs directly without the need of a compiler or interpreter, so the programs
written in low-level language can be run very fast.
i. Machine Language
Machine language is a type of low-level programming language. It is also called as machine code
or object code. Machine language is easier to read because it is normally displayed in binary or
hexadecimal form (base 16) form.
i. Assembly Language
Assembly language (ASM) is also a type of low-level programming language that is designed for
specific processors. It represents the set of instructions in a symbolic and human-understandable
form. It uses an assembler to convert the assembly language to machine language.
Middle-level programming language lies between the low-level programming language and
high-level programming language. Machine code
Böhm–Jacopini theorem
The structured program theorem, also called the Böhm–Jacopini theorem,[1][2] is a result in
programming language theory. It states that a class of control-flow graphs (historically called
flowcharts in this context) can compute any computable function if it combines subprograms in
only three specific ways (control structures). These are
1. Executing one subprogram, and then another subprogram (sequence)
2. Executing one of two subprograms according to the value of a boolean expression
(selection)
3. Repeatedly executing a subprogram as long as a boolean expression is true
(iteration)
E.g SEQUENCE
def addtwo1():
x = int(raw_input("Enter an integer: "))
ans = x + 2
return ans
LOOP
for x in [3,5,9]:
print x
Outputs:
3
5
9
SELECTION
num = int(input("Enter a number: "))
if (num % 2) == 0:
print("NUM is Even”)
else:
print("NUM is Odd")
Multi-Paradigm Languages are languages that can use multiple Programming paradigm together.
Many programming languages support Muli Pradigum programming, for example, C++ is both
an imperative and an object-oriented language, Parlog is both a parallel and a logic programming
language.
The concept of multi-paradigm programming is very simple to understand, When a programming
language use more than one programming paradigm it know as multi-paradigm language, and the
concept of combining these paradigms are Multiparadigm programming.
Imperative: Programming with an explicit sequence of commands that update state.
Declarative: Programming by specifying the result you want, not how to get it.
Structured: Programming with clean, goto-free, nested control structures.
Procedural: Imperative programming with procedure calls.
Functional Programming
Object-Oriented Programming
Declarative Programming
advantage
1. Very simple to implement
2. It contains loops, variables etc.
Disadvantage:
1. Complex problem cannot be solved
2. Less efficient and less productive
3. Parallel programming is not possible
Structure programming processing
Program is made as a single structure.
Code will execute the instruction by instruction one after the other.
It doesn’t support the possibility of jumping from one instruction to some other with the
help of any statement like GOTO, etc.
The structured program consists of well structured and separated modules. But the entry
and exit in a Structured program is a single-time event. It means that the program uses
single-entry and single-exit elements.
Instructions in this approach will be executed in a serial and structured manner. The
languages that support Structured programming approach are: C, C++, Java, C# ..etc
The structured program mainly consists of three types of elements:
a. Selection Statements
b. Sequence Statements
c. Iteration Statements
Selection Statements
f else
Syntax:
if test expression:
statement(s)
If else
Syntax:
if test expression:
Body of if
else:
Body of else
If elif else
Syntax:
if test expression:
Body of if
elif test expression:
Body of elif
else:
Body of else
Conditional
• A conditional expression evaluates an expression based on a condition.
• Conditional expression is expressed using if and else combined with expression
Syntax:
expression if Boolean-expression else expression
Example:
Biggest of two numbers
num1 = 23
num2 = 15
big = num1 if num1 > num2 else num2
print ( “ the biggest number is “ , big )
Even or odd
print ( “ num is even “ if num % 2 == 0 else “ num is odd “)
Looping
For Loop
Syntax:
for iterating_var in sequence:
statements(s)
Example:
It is divided as Logic, Functional, Database. In computer science the declarative programming is a style
of building programs that expresses logic of computation without talking about its control flow. It often
considers programs as theories of some logic.It may simplify writing parallel programs. The focus is on
what needs to be done rather how it should be done basically emphasize on what code is actually doing.
It just declares the result we want rather how it has be produced. This is the only difference between
imperative (how to do) and declarative (what to do) programming paradigms. Getting into deeper we
would see logic, functional and database.
● Logic programming paradigms –
It can be termed as abstract model of computation. It would solve logical problems
like puzzles, series etc. In logic programming we have a knowledge base which we
know before and along with the question and knowledge base which is given to
machine, it produces result. In normal programming languages, such concept of
knowledge base is not available but while using the concept of artificial intelligence,
machine learning we have some models like Perception model which is using the
same mechanism. In logical programming the main emphasize is on knowledge base
and the problem. The execution of the program is very much like proof of
mathematical statement, e.g., Prolog
predicates
sumoftwonumber(integer, integer).
clauses
sumoftwonumber(0, 0).
sumoftwonumber(N, R) :-
N > 0,
N1 is N - 1,
sumoftwonumber(N1, R1),
R is R1 + N.
Functional programming paradigms –
The functional programming paradigms has its roots in mathematics and it is language
independent. The key principle of this paradigms is the execution of series of mathematical
functions. The central model for the abstraction is the function which are meant for some
specific computation and not the data structure. Data are loosely coupled to functions.The
function hide their implementation. Function can be replaced with their values without changing
the meaning of the program. Some of the languages like perl, javascript mostly uses this
paradigm.
Pure functions: These functions have two main properties. First, they always produce
the same output for same arguments irrespective of anything else.
Secondly, they have no side-effects i.e. they do not modify any arguments or
local/global variables or input/output streams.
sum(x, y) // sum is function taking x and y as arguments
return x + y // sum is returning sum of x and y without
changing them
Recursion: There are no “for” or “while” loop in functional languages. Iteration in
functional languages is implemented through recursion. Recursive functions repeatedly
call themselves, until it reaches the base case.
fib(n)
if (n <= 1)
return 1;
else
return fib(n - 1) + fib(n - 2);
Python Lambda Functions are anonymous function means that the function is
without a name
Syntax: lambda arguments : expression
def cube(y):
return y*y*y
lambda_cube = lambda y: y*y*y
cube(5)
Python supports various types of arguments that can be passed at the time of the
function call. In Python, we have the following 4 types of function arguments.
Default argument
Keyword arguments (named arguments)
Positional arguments
Arbitrary arguments (variable-length arguments *args and **kwargs)
Default Arguments
A default argument is a parameter that assumes a default value if a value is not
provided in the function call for that argument.
def myFun(x, y=50):
print("x: ", x)
print("y: ", y)
myFun(10)
Output:
x: 10
y: 50
Required Arguments: The number of arguments in the function call should
match exactly with the function definition.
def my_details( name, age ):
print("Name: ", name)
print("Age ", age)
return
my_details("george",56)
Output:
Name: george
Age 56
Keyword Arguments:
Python interpreter is able to use the keywords provided to match the values
with parameters even though if they are arranged in out of order.
Name: george
Age 56
Procedural Programming
Object-Oriented Programming
Object-oriented programming can be defined as a programming model which is based upon the
concept of objects. Objects contain data in the form of attributes and code in the form of
methods. In object-oriented programming, computer programs are designed using the concept of
objects that interact with the real world. Object-oriented programming languages are various but
the most popular ones are class-based, meaning that objects are instances of classes, which also
determine their types. Languages used in Object-Oriented Programming: Java, C++, C#,
Python, PHP, JavaScript, Ruby, Perl,
Adding new data and functions is not easy. Adding new data and function is easy.
Procedural programming does not have any Object-oriented programming provides data hiding
proper way of hiding data so it is less secure. so it is more secure.
Procedural programming uses the concept of Object-oriented programming uses the concept of
procedure abstraction. data abstraction.