Introduction To Programming
Introduction To Programming
The computer hardware cannot think and make decisions on its own. So, it cannot be used to
analyze a given set of data and find a solution on its own. The hardware needs a software (a set
of programs) to instruct what has to be done.
Program
A program is a set of instructions that are arranged in a sequence to guide a computer to
find a solution for a given problem.
What is programming?
The process of writing a program is called programming.
Machine language
The machine language is the only language that the computer understands.
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 1’s and 0’s.
All the commands and data values are expressed using 1’s and 0’s.
The main advantage of machine language is that the code can run very fast and
efficiently, since it is directly executed by the CPU.
However, on the downside, the machine language is difficult to learn and is far more
difficult to edit if errors occur.
Assembly language:
High level languages are similar to the human language. Unlike low level languages,
high level languages are programmers friendly, easy to code, debug and maintain.
They do not interact directly with the hardware. High level programs require
compilers/interpreters to translate source code to machine language
Programs in high level language is written using English statements.
Today almost all programs are developed using a high level programming language.
Also known as third generation programming language.
There are many high-level programming languages, and each was designed for a specific
purpose, for example C#, C++, Java, COBOL, Lisp, Python, COBOL, FORTRAN, Pascal, and
Perl.
A program written in a high-level language is called a source program or source code.
Because a computer cannot understand a source program, a source program must be
translated into machine code for execution. The translation can be done using another
programming tool called an interpreter or a compiler.
Srikanth.D, Mentor in IT, IIIT-S, Content source-Internet.
Compiler
Compiler is a program/software that translates/transforms high level source
code/program into object code/Machine language code/binary code/low level
object code.
As it scans the entire code at a time, the errors (if any) are shown at the end
together.
It generates intermediate object code.
Memory requirement is more due to the creation of object code.
Display all errors after compilation, all at the same time.
Error detection is difficult
C, C++, C#, Scala, Java all use complier.
Interpreter
An interpreter is a computer program that is used to directly execute program
instructions written using one of the many high-level programming languages.
It takes a single line of code or instruction at a time.
It does not produce any intermediate object code.
It requires less memory as it does not create intermediate object code.
Displays error of each line one by one.
Error detection is easy comparatively.
PHP, Perl, Python, Ruby uses an interpreter.
What is python?
Python is a widely used general-purpose, high-level programming language.
Applications of python
Python is used in
Features of python
There are many feature in python, some are below
Coding is easy
Python is very easy to learn the language as compared to other languages like C, C#,
JavaScript, Java, etc. It is very easy to code in python language and anybody can
learn python basics in a few hours or days. It is also a developer-friendly language.
Free and open source
We can download and install it for free. Since it is open-source, this means that
source code is also available to the public. So you can download it as, use it as well
as share it.
High level
When we write programs in python, we do not need to remember the system
architecture, nor do we need to manage the memory.
Highly portable
We can run it on different operating systems. For example, if we have python code
for windows and if we want to run this code on other platforms such as Linux, Unix,
and Mac then we do not need to change it, we can run this code on any platform.
Object oriented
One of the key features of python is Object-Oriented programming. Python supports
object-oriented language and concepts of classes, objects encapsulation, etc.
Click on Install now then installation process will go on, after you see the message “Setup
was successful”
Type Y and press enter, after you see software will install like below
Interactive mode
Script mode.
Interactive Mode:
After the prompt just type the below instruction, hit enter and observe the output/result
Print “Hello world, happy learning python”
Not only the above single line statement but we can execute sequence of statements too
Example-1
>>>x=9
>>>y=8
>>>k=x+y
>>>print k
17
Example-2
>>>a=2
>>>b=a*2
>>>c=b+a
>>>d=c/3
>>>print d
2
- Now we are good to write a small code on our own in Python.
- While writing in Python, remember Python is case sensitive. That means x & X are different in
Python.
-control D or quit () is used to close the interpreter
Script mode:
In script mode, we type Python program in a file and then use the interpreter to execute
the content from the file. Working in interactive mode is convenient for beginners and for testing
small pieces of code, as we can test them immediately. But for coding more than few lines,
we should always save our code so that we may modify and reuse the code.
Python, in interactive mode, is good enough to learn, experiment or explore, but it’s only
drawback is that we cannot save the statements for further use and we have to retype all the
statements to re-run them.
To create and run a Python script, we will use following steps in IDLE
1. Open python IDLE
2. Go to file and click on new
3. After write code
4. Save the file
5. Run/Execute the file by clicking Run from menu/press F5 to run the file
Write a python program to print “hello world, happy learning python” in interactive mode
and script mode
Interactive mode
Open python shell and then just type
Srikanth.D, Mentor in IT, IIIT-S, Content source-Internet.
A=6
B=7
print (A+B)
After the prompt and press enter to see the output.
Script mode
- Go to file in python shell, click on new file then a new window will appear, There we write
code like below
- print (“hello world, happy learning python”)
- save the file with any name with .py extension , after saving just click on run/press f5 to
see the output.
- We can use plain text file to write python code, then open terminal/command prompt then
go to file location then type the following to execute the file
python filename.py
Variable
One of the most powerful features of a programming language is the ability to manipulate
variables. When we create a program, we often like to store values so that it can be used later.
Example
x=5
y = "John"
Variables do not need to be declared with any particular type, and can even change type
after they have been set.
x = 4 # x is of type int
x = "Sally" # x is now of type str
A variable can have a short name (like x and y) or a more descriptive name (age,
carname, total_volume).
An identifier is a name given to entities like class, functions, variables, etc. It helps to
differentiate one entity from another.
Example
Age=18
In the above example the variable holds value 18, the name of the variable which is ‘age’ is
called identifier.
1. The identifier is only used to identify an entity uniquely in a program at the time of
execution whereas, a variable is a name given to a memory location that is used to hold a
value.
2. Variable is only a kind of identifier, other kinds of identifiers are function names, class
names, structure names, etc. So it can be said that all variables are identifiers whereas,
vice versa is not true.
Constants
A constant is a type of variable that holds values, which cannot be changed. In reality, we rarely
use constants in Python.
Examples
PI = 3.14
GRAVITY = 9.8
What is the difference between a variable and a constant?
Constant Variable
- Constant value cannot be changed - A variable value can be changed
- Constants are known values like pi, accordingly
gravity etc - For unknown values
- For example 5x+7=8 here 7 & 8 are - For example 5x+7y=8=0 here x and y
constants are variables
Instructions written in the source code for execution are called statements.
There are different types of statements in the Python programming language like
Assignment statement, Conditional statement, looping statements etc. These all help the
user to get the required output. For example, n = 50 is an assignment statement.
Multi-Line Statements: Statements in Python can be extended to one or more lines using
parentheses (), braces {}, square brackets [], semi-colon (;), continuation character slash (\).
When the programmer needs to do long calculations and cannot fit his statements into one line,
one can make use of these characters.
Example
Comments in python
Comments are the useful information that the developers provide to make the reader
understand the source code.
It explains the logic or a part of it used in the code.
Comments will be ignored at the time of execution.
- Starts with hashtag symbol with no white spaces (#) and lasts till the end of the line.
- If the comment exceeds one line then put a hashtag on the next line and continue the
comment.
- Python’s single line comments are proved useful for supplying short explanations for
variables, function declarations, and expressions.
Example
# This is a comment
#This is a sample program
print("Welcome to IIIT-S")
- Python multi-line comment is a piece of text enclosed in a delimiter (""") on each end of
the comment.
- Again there should be no white space between delimiter (""").
- They are useful when the comment text does not fit into one line; therefore needs to span
across lines.
- Multi-line comments or paragraphs serve as documentation for others reading your code.
Example
"""
This would be a multiline comment in Python that
spans several lines and describes geeksforgeeks.
A Computer Science portal for geeks. It contains
well written, well thought
and well-explained computer science
and programming articles,
quizzes and more.
"""
print("We are RGUKTians")
Indentation
- One of the distinctive features of Python is its use of indentation to highlight the blocks of
code.
- Whitespace/tab space is used for indentation in Python.
- All statements with the same distance to the right belong to the same block of code.
- If a block has to be more deeply nested, it is simply indented further to the right.
- You can understand it better by looking at the following lines of code
2. Relational operator
Relational operators compares the values. It either returns True or False according to the
condition.
4. Bitwise operators
6. Special operators
in and not in are the membership operators; used to test whether a value or variable
is in a sequence.
in True if value is found in the sequence
not in True if value is not found in the sequence
Operator precedence
Operands in python
A=5
B=9
Print (A+B)
Here A, B are called operands