Introduction To Python

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Programming refers to a technological process for telling a computer which tasks to perform in order to solve

problems.

You can think of programming as a collaboration between humans and computers, in which humans create
instructions for a computer to follow (code) in a language computers can understand.

Algorithm
Developing algorithm to solve particular problem In mathematics and computer science, an algorithm is a step-by-step
procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.Algorithms
resemble recipes. Recipes tell you how to accomplish a task by performing a number of steps.

For example, to bake a cake the steps are: preheat the

oven; mix flour, sugar, and eggs thoroughly; pour into a baking pan; and so forth.

Goals of Algorithm Study


• To develop framework for instructing computer to perform tasks

• To introduce notion of algorithm as means of specifying how to solve a problem

• To introduce and appreciate approaches for defining and solving very complex tasks in terms of simpler
tasks;

Simple Questions based on Algorithm

Algorithm: Calling a friend on the telephone

Input: The telephone number of your friend.

Output: None

Steps:

1. Pick up the phone and listen for a dial tone

2. Press each digit of the phone number on the phone

3. If busy, hang up phone, wait 5 minutes, jump to step 2

4. If no one answers, leave a message then hang up

5. If no answering machine, hang up and wait 2 hours, then jump to step 2

6. Talk to friend

7. Hang up phone

Algorithm to create Maggie noddle:


Step 1: Start

Step 2: Take pan with water

Step 3: Put pan on the burner

Step 4: Switch on the gas/burner

Step 5: Put magi and masala

Step 6: Give two minutes to boil

Step 7: Take off the pan

Step 8: Take out the magi with the help of fork/spoon

Step 9: Put the Maggi on the plate and serve it


Step 10: Stop.

Algorithm: To find (a + b)2=(a + b)* (a + b)


Input: Two numbers a and b.

Output: Result of (a + b)2

Steps:

1. Take numbers a, b, c.

2. Input the values of a and b

3. C=a+b

4. Print c*c

Algorithm: To find Perimeter of rectangle


Input: Two numbers l and b.

Output: Result l*b

Steps:

1. Take numbers l, b, c, d.

2. Input the values of l and b

3. c=l*b

4. d=2*c

5. Print d

Conditions in Algorithm
Algorithm: To check whether a person can vote or not
Input: A number age.

Output: Yes or No

Steps:

1. Take a number age.

2. Input the age of the person

3. If(age>=18) goto step 4

Else

Goto step 5

4. Print “You can vote”

5. Print “You can’t vote”

Algorithm: To give a bonus of Rs 1000 to those employees whose salary is more than Rs 5000
Input: Salary.

Output: New Salary

Steps:

1. Take numbers salary and total.


2. Input the salary of the person

3. If(salary>=5000) goto step 4

Else

Goto step 5

4. total=salary+1000

5. total=salary+0

6. Print total

FLOW CHART
A flowchart is a type of diagram that represents an algorithm or process, showing the steps as boxes of various kinds,
and their order by connecting them with arrows. This diagrammatic representation illustrates a solution to a given
problem.
Computer Languages
The user of a computer must be able to communicate with it. That means, he must be able to give the
computer commands and understand the output that the computer generates. This is possible due to the invention of
computer languages.

Basically, there are two main categories of computer languages, namely Low Level Language and High Level
Language. Let us take a brief look at both these types of computer languages.

1] Low Level Languages


Low level languages are the basic computer instructions or better known as machine codes. A computer
cannot understand any instruction given to it by the user in English or any other high level language. These low level
languages are very easily understandable by the machine.

The main function of low level languages is to interact with the hardware of the computer. They help in
operating, syncing and managing all the hardware and system components of the computer. They handle all the
instructions which form the architecture of the hardware systems.

Machine Language
This is one of the most basic low level languages. The language was first developed to interact
with the first generation computers. It is written in binary code or machine code, which means it
basically comprises of only two digits – 1 and 0.

Assembly Language
This is the second generation programming language. It is a development on the machine
language, where instead of using only numbers, we use English words, names, and symbols. It is the
most basic computer language necessary for any processor.

2] High Level Language


When we talk about high level languages, these are programming languages. Some prominent examples are PASCAL,
FORTRAN, C++ etc.

The important feature about such high level languages is that they allow the programmer to write programs for all
types of computers and systems. Every instruction in high level language is converted to machine language for the
computer to comprehend.

Scripting Languages
Scripting languages or scripts are essentially programming languages. These languages employ a high level
construct which allows it to interpret and execute one command at a time.

Scripting languages are easier to learn and execute than compiled languages. Some examples are AppleScript,
JavaScript, Pearl etc.

Object-Oriented Languages
These are high level languages that focus on the ‘objects’ rather than the ‘actions’. To accomplish this, the
focus will be on data than logic.

The reasoning behind is that the programmers really cares about the object they wish to manipulate rather
than the logic needed to manipulate them. Some examples include Java, C+, C++, Python, Swift etc.

Procedural Programming Language


This is a type of programming language that has well structured steps and complex procedures within its
programming to compose a complete program.

It has a systematic order functions and commands to complete a task or a program. FORTRAN, ALGOL, BASIC,
COBOL are some examples
Types of Language Translators
There are mainly three types of translators that are used to translate different programming languages into machine-
equivalent code:

1. Assembler
2. Compiler
3. Interpreter

Assembler
An assembler translates assembly language into machine code.

Assembly language consists of mnemonics for machine op-codes, so assemblers perform a 1:1 translation from
mnemonic to direct instruction. For example, LDA #4 converts to 0001001000100100.

Conversely, one instruction in a high-level language will translate to one or more instructions at the machine level.

The Benefits of Using Assembler

Here is a list of the advantages of using assembler:

As a 1 to 1 relationship, assembly language to machine code translation is very fast.

Assembly code is often very efficient (and therefore fast) because it is a low-level language.

Assembly code is fairly easy to understand due to the use of English, like in mnemonics.

The Drawbacks of Using Assembler

Assembly language is written for a certain instruction set and/or processor.

Assembly tends to be optimized for the hardware it is designed for, meaning it is often incompatible with different
hardware.

Lots of assembly code is needed to do a relatively simple task, and complex programs require lots of programming
time.

Compiler
A compiler is a computer program that translates code written in a high-level language into a low-level language,
machine code.

The most common reason for translating source code is to create an executable program (converting from high-level
language into machine language).

Advantages of using a compiler

Below is a list of the advantages of using a compiler:

Source code is not included; therefore, compiled code is more secure than interpreted code.

tends to produce faster code and is better at interpreting source code.

Because the program generates an executable file, it can be run without the need for the source code.

Disadvantages of using a compiler

Below is a list of the disadvantages of using a compiler:

Before a final executable file can be created, object code must be generated; this can be a time-consuming process.

The source code must be 100% correct for the executable file to be produced.
Interpreter
An interpreter program executes other programs directly, running through the program code and executing it line-by-
line. As it analyses every line, an interpreter is slower than running compiled code, but it can take less time to interpret
program code than to compile and then run it. This is very useful when prototyping and testing code.

Interpreters are written for multiple platforms; this means code written once can be immediately run on different
systems without having to recompile for each. Examples of this include flash-based web programs that will run on your
PC, Mac, gaming console, and mobile phone.

Advantages of using an interpreter

Here is a list of some of the main advantages of using an interpreter:

easier to debug (check errors) than a compiler.

It is easier to create multi-platform code, as each different platform would have an interpreter to run the same code.

useful for prototyping software and testing basic program logic.

Disadvantages of using an interpreter

And here is the list of some of the main disadvantages of using an interpreter:

Source code is required for the program to be executed, and this source code can be read, making it insecure.

Due to the on-line translation method, interpreters are generally slower than compiled programs.

You might also like