Introduction to Python
COMPUTER LANGUAGES
Computer languages, also known as programming languages, are formal languages used to
communicate instructions to a computer. These instructions are used to perform specific tasks,
control devices, or build software applications.
As humans use different languages to speak and communicate similarly we communicate with a
computer and give instructions with the help of a computer language.
Over the years, many computer languages have been developed and they can be classified into three
broad categories:
(i) Machine language, (ii) Assembly language and (iii) High-level language.
Machine Language
A computer is an electronic machine. It can only understand the language of zeros (0) and ones (1).
This language is known as binary language or machine language.
In machine language, instructions are given in the form of strings of 0s and 1s.
It is extremely difficult for us to write programs in machine language.
Assembly Language
An assembly language is the language in which instructions are given in the form of short words
called mnemonics such as MOV, ADD and LD.
For example : Mneumonic: ADD A, byte will add A to byte and put the result in A.
Mneumonic: SUB A, byte will subtract with borrow.
It is easier to write programs in an assembly language as compared to the machine language. Writing
programs in machine and assembly language requires knowledge of the underlying hardware.
Also, the programs written in these languages are machine dependent and are not portable, i.e., a
program written on one machine may not run on the other.
High-Level Language
A high-level language is a language in which programs are written using English words and
mathematical symbols. Programmers do not need underlying hardware details to write programs in
these languages.
Programs written in high-level languages are machine independent and portable. Thus, it is very easy
for programmers to learn and develop programs in high-level languages. However, programs written
in assembly or high-level languages cannot be directly understood by the computer. They have to be
translated into their equivalent machine language form so that they can be executed by the
computer. Translators used for converting high-level language programs into the corresponding
machine language are known as compilers or interpreters.
Examples of high-level computer languages are BASIC, FORTRAN, Pascal, C, C++, Java, Python,
Prolog and LISP.
We know that languages like Hindi, English, Bengali and French have their own set of characters,
grammar rules, pronunciation, etc. Similarly, the different programming languages also differ from
each other in their grammar rules called syntax.
We must understand and follow the syntax of the language to convert an algorithm into a program.
Programming = Algorithm + Syntax
PYTHON
Python is a high-level, interpreted programming language that is known for its simplicity,
readability, and versatility. It was created by Guido van Rossum and first released in 1991.
It is used in various application areas such as web development, gaming, scientific and numeric
computing.
Let us learn about some of the useful features of Python that make it so popular and widely used.
• Easy to Learn: Python is an easy-to-learn language. It has fewer keywords and simple
structure of instructions. This allows the beginners to learn this language with less efforts.
• Free and Open Source: Python is freely available for download and can be used by anyone.
• Portable: The programs written in Python can run on a wide variety of hardware platforms. A
Python program written on a computer with Windows OS can be executed on a computer
with Mac or Linux OS with absolutely no changes.
• Extensive Library Support: Python comes with anumber of built-in libraries. Just like using
built-in design styles in Word or PowerPoint saves the designing time, using the libraries in
Python saves our time and efforts as the basic programming code is readily available.
Examples of some places where Python is used:
• The video sharing service YouTube has been largely written in Python.
• Google makes extensive use of Python.
• NASA uses Python for scientific Programming Tasks.
INSTALLING PYTHON
We need an interpreter to convert Python programs into a form that can be understood and
executed by the computer.
We can download and install the Python interpreter using the following link:
[Link] (any version from 3.5 onwards)
Here, you will learn to work in Python version 3.8.1.
Opening Python
Once the Python interpreter is installed on the computer system, follow these steps to open it: 1.
Type Python in the Search box next to the Start button.
2. Click on Python IDLE option. Integrated Development and Learning Environment (IDLE) is the
standard and most popular development environment as it helps us create, run and debug Python
programs froma single interface. The Python 3.8.1 Shell window opens as shown in Fig. below.
INTERACTING WITH PYTHON INTERPRETER
There are two ways to use the Python interpreter:
(i) interactive mode and (ii) script mode. Let us learn about these two modes in detail.
Interactive Mode
The Python Shell window opens on clicking the Python IDLE option. The window shows Python
version information on the top. This is followed by the Python prompt (>>>).
The Python prompt is an indication that the interpreter is ready for instructions. An instruction or
command is known asa statement. We can give Python statements at the prompt and the interpreter
will respond with the results.
Type 3 + 7on the prompt as shown in Fig. given below and press the Enter key. We will notice that the
result gets displayed on the next line.
The interactive mode is good for trying short pieces of code. The instructions or statements cannot
be saved for further use and we have to retype all the statements to rerun them. Hence, this mode is
not suitable when we want to create, debug and execute programs with a number of statements.
Script Mode
In the script mode, we can store the Python statements in a file and run it as many times as we want.
Let us go through an example of executing Python statements in the script mode.
1 Choose the New File option under the File menu in the Shell window.
2. Type the following code in the editor window as shown in figure below. print (Welcome to Python
Programming") We can type and edit the code just the way we do it in a text editor.
3. Now, save the file by using the Save option under the File menu in the Editor window. The Save As
dialog box gets displayed.
4. Type the name of the file and click on the Savebutton. The Python file gets saved with the
extension .py.
5. Now, execute the program. For this, press the F5 key or click on the Run Module option under the
Run menu in the Editor window.
The output of the program appears in the Shell window as shown in figure below.
KEYWORDS IN PYTHON
Keywords are reserved words. They are defined for a specific purpose and convey a special
meaning to the Python interpreter.
Table 1: List of keywords in Python
IDENTIFIERS IN PYTHON
Unlike a keyword, an identifier is a name given by the programmer toa variable, function or any
other object in a language. We will learn more about variables later in this note.
There are certain rules that we should follow while naming the identifiers. These include:
• Keywords cannot be used as identifiers
• Identifiers can be a combination of letters in lowercase (a to z), uppercase (A to Z), digits (0
to 9) or an underscore (_).
• An identifier cannot start with a digit. For example, 1variable is invalid, but variable1 is
perfectly fine.
• Python does not allow punctuation characters such as @, $ and % to be used for naming
identifiers.
• Python is a case-sensitive programming language. Thus, marks and Marks are two different
identifiers in Python.
Table 2: List of valid and invalid identifiers
VARIABLES AND DATA TYPES IN PYTHON
Variables refer to a memory location that holds a value. Variables are an important part of any
programming language. They help us store and manipulate data.
Variables are also a type of identifier. The rules for naming an identifier (discussed earlier in the
note) are applicable to variables also.
Creating Variables
Let us learn to create and use variables in Python. A Variable is created as soon as we assign a
value to it.
The assignment operator (=) is used to assign a value to a variable.
In the following example, we have created variables referring to integer, float and string [Link]
print statement is then used to display the value of these variables.
Every variable in Python refers to a value. The value that a variable points to is of a particular data
type. Let us learn more about the data types in Python.
Data Types
Number of data types are supported by Python, such as number, string, list, tuple, set and
dictionary. In this note, we will learn about number and string data types.
Number
We will discuss two number data types: int and float.
• int:
int or integer is a positive or negative number. It is basically anumber withouta decimal
point.
Examples of variables with integer values are:
a=5
b = -51
• float
float or floating-point number is a positive or negative number with a decimal point. It is a
number with a fractional part.
Examples of variables with float values are:
c = 2.9
d = -55.8
Let us understand the difference between int and float data type values with the help of the
following table.
Table 3: Examples of int and float data types
String
Strings in Python are a sequence of characters. These characters are enclosed within single quotes
( ), double quotes (" ") or triple quotes ("’ "’). Triple quotes are used when the string value extends
over more than one line.
For example, the following variables refer to values of type string:
name= "Adya"
section= ‘G’
address= "House No. -450, Newtown Kolkata-65"
message=’’’If you fail,never give up because FAIL means “First Attempt In Learning’’’
ACCEPTING INPUT AND DISPLAYING OUTPUT
Every programming language has functions or statements that are used to take input from the user and
display output to the user.
Let us learn about the built-in functions in Python that help us take input and display output.
Accepting Input
In Python, the input () function is used to take a value from the user and assign it to a variable. So, a
variable and assignment operator is placed before the input () function. A text message (prompt) to
tell the user what value to enter is written in the parenthesis of input () function.
Let us learn to use input () function.
• To accept text values:
• To accept numeric values:
To accept integer or float value, use the int() and
float () functions as shown below.
age=int (input ("Enter age"))
marks=float (input ("Enter marks"))
Displaying Output
We have already used the print () function to display the output. The print () function can be used to
display numbers, messages or values of variables on the output screen.
Let us look at some examples of print () function.
Table 4: Examples of print function
OPERATORS
Operators are special symbols that are used to perform operations on variables and values. We use
operators in everyday Mathematics, like +, -, x and ÷ to perform addition, subtraction,
multiplication and division of values. Python also has a number of operators that help us perform
operations of various types.
We have already learnt about the assignment operator (=) which is used to assign a value to a
variable.
There are a number of other operators in Python. In this chapter, we will learn about the arithmetic
operators.
The following table lists some of the arithmetic operators in Python.
Table 5: Few Arithmetic operators in Python
Try the following examples on the Python Shell:
Remember
/ Operator gives the quotient and
% symbol gives the remainder
The + operator can be used to join or concatenate two strings. It joins the two strings with no space
between them.
Concatenating two strings
>>>msg1=" Hello"
>>> msg2="World"
>>> print (msg1+msg2)
HelloWorld
PYTHON PROGRAMS
Let us now write some simple programs in Python.
It is advisable to write these programs in Script Mode as it will be easier for us to debug them and
use them whenever required.
Program 1:
Write a program that takes the name of the user and then displays a welcome message to the
user.
The welcome message should be:
<User Name> - welcome to the world of Python Programming
Code
#Program to display a welcome message
name=input ("Enter your name: ")
msg= name + “ -welcome to the world of Python Programming”
print(msg)
Output
Enter your name: Narayan
Narayan - welcome to the world of Python Programming
Program 2:
Write a program to take name and country from the user and then display the following
message to the user:
Welcome <user name>
Your country <country name> is a beautiful country
Code
# Program to display a message to the user
u_name = input ("Enter your name: ")
c_name = input ("Enter name of the country: ")
line1 = “Welcome "+ u_name
line2 = “Your country ”+ c_name + “ is a beautiful country”
print (line1)
print (line2 )
Output
Enter your name: Vedika
Enter name of the country: India
Welcome Vedika
Your country India is a beautiful country
>>>
Program 3:
Write a program to take three integers from the user and display their sum.
Code
# Program to take three integers from the user and display their sum
num1 = int (input (“Enter the first number:”))
num2 = int (input (“Enter the second number:”))
num3 = int (input (“Enter the third number:”))
sum = num1+ num2+ num3
print (“The sum of three numbers =”, sum)
Output
Enter the first number: 58
Enter the second number: 80
Enter the third number: 65
The sum of three numbers = 203
>>>
Program 4:
Write a program to take length and breadth of a rectangle from the user and display its area
and perimeter.
Code
# Program to display area and perimeter of a rectangle
l = float (input ("Enter the length of the rectangle: "))
b = float (input ("Enter the breadth of the rectangle: "))
area = l*b
perimeter = 2*(l+b)
print ()
print (“Area of the rectangle =”, area)
print (“Perimeter of the rectangle =”, perimeter)
Output
Enter the length of the rectangle: 24.5
Enter the breadth of the rectangle: 8.5
Area of the rectangle = 208.25
Perimeter of the rectangle= 66.0
>>>
*****************************************************************************************