Programming Techniques - Python
Programming Techniques - Python
Programming Techniques - Python
• Syntax is the set of rules that define what the various combinations of symbols
mean. This tells the computer how to read the code. Syntax refers to a concept in
writing code dealing with a very specific set of words and a very specific order to
those words when we give the computer instructions.
• Machine Language
• Assembly Language
• High level Language
• The fundamental language of the computer’s processor, also called Low Level Language.
• All programs are converted into machine language before they can be executed.
• Consists of combination of 0’s and 1’s that represent high and low electrical voltage.
• Fastest to execute because it is already in the language that the computer can understand
• It is difficult to identify mistakes made Time-consuming and tedious to write Machine
dependent
• Programing becomes more difficult as the complexity of the program increases
Compiler Interpreter
A compiler takes the entire program in one go. An interpreter takes a single line of code at a time.
The compiler generates an intermediate machine code The interpreter never produces any intermediate
(object code). machine code.
The compiler is best suited for the production An interpreter is best suited for a software
environment. development environment.
The compiler is used by programming languages as C, An interpreter is used by such programming languages
C++, C#, Scala, Java, etc. such as Python, PHP, Perl, Ruby, etc.
Memory requirement -> More (since object code is Memory requirement -> Less
generated)
• Pros
• Excellent for general-purpose programming
• Enhances the reusability of the code.
• Simplicity of use
• Easy accessibility
• Tracking the program flow is easy as the same flow linearly.
• Cons
• Data is vulnerable.
• Not much practical for solving real-world problems.
• Programs created using this programming paradigm are complex.
For example, a person is an object which has certain properties such as height,
gender, age, etc. It also has certain methods such as move, talk, and so on.
• An object is a single instance of a class. All data members and member functions
of the class can be accessed with the help of objects.
• Since many houses can be made from the same description, we can create many
objects from a class.
• Abstraction − It refers to, providing only essential information to the outside world and hiding their background details.
For example, a web server hides how it processes data it receives, the end user just hits the endpoints and gets the data back.
• Encapsulation − Encapsulation is a process of binding data members (variables, properties) and member functions
(methods) into a single unit. It is also a way of restricting access to certain properties or component. The best example for
encapsulation is a class.
• Inheritance − The ability to create a new class from an existing class is called Inheritance. Using inheritance, we can
create a Child class from a Parent class such that it inherits the properties and methods of the parent class and can have its
own additional properties and methods. For example, if we have a class Vehicle that has properties like Color, Price, etc.,
we can create 2 classes like Bike and Car from it that have those 2 properties and additional properties that are specialized
for them like a car has number Of Windows while a bike cannot. Same is applicable to methods.
• Polymorphism − The word polymorphism means having many forms. Typically, polymorphism occurs when there is a
hierarchy of classes and they are related by inheritance.
• Conditions – Perform some conditions on the inputs to get the desired output.
• Output – Printing the outputs.
• End – End the execution.
Advantages of Algorithms
Disadvantages of Algorithms
Example 1: Write pseudo code that reads two numbers and multiplies them together and print out
their product.
Read num1 , num2
Set multi to num1*num2
Write multi
Example 2: Write pseudo code that tells a user that the number they entered is not a 5 or a 6.
Read isfive
If(isfive = 5)
Write "your number is 5"
Else if (isfive = 6)
Write "your number is 6"
Else
Write "your number is not 5 or 6"
Example 2: Write pseudo code that performs the following: Ask a user to enter a number. If the number is between 0 and 10,
write the word blue. If the number is between 10 and 20, write the word red. if the number is between 20 and 30, write the
word green. If it is any other number, write that it is not a correct color option.
Advantages of Pseudocode
• Improves the readability of any approach. It’s one of the best approaches to start implementation
of an algorithm.
• Acts as a bridge between the program and the algorithm or flowchart. Also works as a rough
documentation, so the program of one developer can be understood easily when a pseudo code is
written out. In industries, the approach of documentation is essential. And that’s where a pseudo-
code proves vital.
• The main goal of a pseudo code is to explain what exactly each line of a program should do, hence
making the code construction phase easier for the programmer.
- Start / Stop
- Input/ Output
- Process
- Decision
- Control Flow
- Connector
Algorithm
1.Initialize sum = 0 (process)
2.Enter the numbers (I/O)
3.Add the numbers and store
the result in sum (process)
4.Print sum (I/O)
1) Sequence
2) Selection
3) Repetition
pseudo-code for a simple program that calculates the total amount payable when
you include the hourly and hourly rate
Begin
input hours
input rate
pay=hours*rate
print pay
End