0% found this document useful (0 votes)
43 views

Introduction To Programming

Uploaded by

ramyasri Kengam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Introduction To Programming

Uploaded by

ramyasri Kengam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

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.

What is a programming language?


 A programming language is a computer language that is used by programmers/developers
to communicate with computers. It is a set of instructions written in any specific language
(C, C++, java, python etc.) to perform a specific task.
 A programming language is mainly used to develop desktop applications, websites, and
mobile applications.
 Some examples for programming languages are C, C+, C#, Java, Python, R, Ruby and
matlab etc.

Computer software (a set of programs/a program) is written by computer programmers using a


programming language. The programmer writes a set of instructions (program) using a
specific programming language. Such instructions are known as the source code. Another
computer program called a compiler is used which transforms the source code into a
language that the computer can understand. The result is an executable computer
program, which is another software.

Classification of programming languages


We now know that programming languages are the primary tools for creating software. Thousands
of programming languages have been written till date. Each for some specific purpose.

Programming languages are basically classified into two main categories

 Low level language


 High level language.

Low-level programming language

 A low-level language is a type of programming language that contains basic instructions


(machine codes) recognized by a computer.
 It is represented in 0 or 1 forms (Machine codes/machine instructions)
 Refers to code written directly for computer.
 These low level languages are very easily understandable by the machine.

Srikanth.D, Mentor in IT, IIIT-S, Content source-Internet.


 The main function of low level languages is to interact with the hardware of the
computer.

The languages that come under this category are

 Machine level language


 Assembly language.

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:

 This is the second generation programming language.


 Assembly language is the symbolic programming language that uses symbolic
notation to represent machine-language instructions.
 It uses symbolic codes and some human-readable commands that are easy-to-
remember abbreviations, rather than numbers. Examples of these codes include ADD
for add, CMP for compare, MUL for multiply, etc.
 As we know that computers can only understand the machine-level instructions, so this
process requires a translator that converts the assembly code into machine code.
The translator used for translating the code is known as an assembler.

High level programming languages:

 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.

Srikanth.D, Mentor in IT, IIIT-S, Content source-Internet.


Python history and versions
 Created by Guido van Rossum and first released in 1991
 Python was named for the BBC TV show Monty Python's Flying Circus.
 Python was influenced by ABC language and Module-3
 In 1994, Python 1.0 was released and python latest version is 3.9.0

Applications of python
Python is used in

 Game development. (Example Pygame)


 Machine learning and Artificial Intelligence
 Data science and data visualization
 Python is used in web applications (HTML, XML, Email processing )
 Desktop GUI (Graphical User interface) applications.
 Business applications
 Audio and video applications

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.

Install and run python in windows and Ubuntu


 Go to the official site to download the python software i.e
https://www.python.org/downloads/
 Click on the option for which operating system you want to download.

Srikanth.D, Mentor in IT, IIIT-S, Content source-Internet.


For windows 10
 Right click on the file and click on run as administrator.
 Click allow to continue.

 Click on Install now then installation process will go on, after you see the message “Setup
was successful”

 Then click on close

Srikanth.D, Mentor in IT, IIIT-S, Content source-Internet.


Installing on Linux/Ubuntu machine

 Open the terminal


 Before starting you may need to update, if so then type the command
 sudo apt-get upate it will ask you to enter password then update will happen automatically

Once update is done then type the below command


 sudo apt-get install python 3.9

 Type Y and press enter, after you see software will install like below

Srikanth.D, Mentor in IT, IIIT-S, Content source-Internet.


Types of modes
Python shell can be used in two ways

 Interactive mode
 Script mode.

Python shell is like below

Interactive Mode:

- As the name suggests, allows us to interact with operating system


- Here, we type a Python statement and the interpreter displays the result(s)
immediately
- We execute small statements here.
- We can give our expressions/commands/statements after >>> (python prompt)
- Interpreter uses prompt to indicate that it is ready to execute instructions.

Let us see how it is working

After the prompt just type the below instruction, hit enter and observe the output/result
Print “Hello world, happy learning python”

Just try the below statements after prompt


Print 5+7

Srikanth.D, Mentor in IT, IIIT-S, Content source-Internet.


5+7
7*8/2
5>2

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

Sample program in python.

 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.

 A variable is a name which refers to a value.


OR
 Variables are containers for storing data values.

A variable is created the moment you first assign a value to it.

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

 String variables can be declared either by using single or double quotes:


x = "RGU"
# is the same as
x = 'RGU'

 A variable can have a short name (like x and y) or a more descriptive name (age,
carname, total_volume).

Rules forPython variables:


 A variable name must start with a letter or the underscore character
 A variable name cannot start with a number
 A variable name can only contain alpha-numeric characters and underscores (A-z,0-
9,and _ )
 Variable names are case-sensitive (age, Age and AGE are three different variables)
Srikanth.D, Mentor in IT, IIIT-S, Content source-Internet.
Valid variable names Invalid variable names
myvar = "rgukt" 2myvar = "rgukt"
my_var = " rgukt " my-var = "rgukt"
_my_var = " rgukt " my var = "rgukt"
myVar = "rgukt"
MYVAR = "rgukt"
myvar2 = "rgukt"

Assign Value to Multiple Variables


Python allows you to assign values to multiple variables in one line

Example And you can assign the same value to multiple


variables in one line
x, y, z = "Orange", "Banana", "Cherry"
Example
print(x) x = y = z = "Orange"
print(x)
print(y)
print(y)
print(z)
print(z)

Python Reserved words/Key words


 Keywords are defined with predefined meaning and syntax in the language.
 Keywords are the reserved words in Python.
 Key words that cannot be used as variable names, function names, or any other identifiers
 In Python, keywords are case sensitive.

Srikanth.D, Mentor in IT, IIIT-S, Content source-Internet.


Identifier

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.

Rules for writing identifiers

 An identifier cannot start with a digit.


o For example 12age is invalid, but age12 is a valid name.
 Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or
digits (0 to 9) or an underscore
o For example names like myClass, var_1 and hello12_world, all are valid example.
 Keywords cannot be used as identifiers.
o For example and=2 if=’hello’
 We cannot use special symbols like !, @, #, $, % etc. in our identifier.
o For example hi@hello, age#12, x@y
 An identifier can be of any length.

Key Differences between Identifier and Variable

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

Srikanth.D, Mentor in IT, IIIT-S, Content source-Internet.


Statements in python

 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

Declared using Continuation Character (\):


s=1+2+3+\
4+5+6+\
7+8+9

Declared using parentheses () :


n = (1 * 2 * 3 + 7 + 8 + 9)

Declared using semicolons(;) :


flag = 2; ropes = 3; pole = 4

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.

Types of comment in Python

Python single line comment

- 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")

Srikanth.D, Mentor in IT, IIIT-S, Content source-Internet.


Multi-line string as comment

- 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

Example for indentation


Finding larges among x, y and z

Srikanth.D, Mentor in IT, IIIT-S, Content source-Internet.


Operators in python
Operators in general are used to perform operations on values and variables in Python. These
are standard symbols used for the purpose of logical and arithmetic operations.

Different types of operators


1. Arithmetic operators

/ Symbol gives float value (decimal)


// symbol gives floor value (2.66 floor value is 2, 3.32 floor value is 3)

2. Relational operator
Relational operators compares the values. It either returns True or False according to the
condition.

Srikanth.D, Mentor in IT, IIIT-S, Content source-Internet.


Truth table

3. Logical operators (or, and, not)

4. Bitwise operators

Bitwise operators acts on bits and performs bit by bit operation.

Srikanth.D, Mentor in IT, IIIT-S, Content source-Internet.


5. Assignment Operator

Assignment operators are used to assign values to variables:

6. Special operators

Srikanth.D, Mentor in IT, IIIT-S, Content source-Internet.


6. Membership operator

 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

The values that an operator acts on are called operands.

A=5
B=9

Print (A+B)
Here A, B are called operands

Srikanth.D, Mentor in IT, IIIT-S, Content source-Internet.

You might also like