Chapter-4
INTRODUCTION TO PYTHON
PROGRAMMING
PYTHON
Python is a general-purpose high-level programming language. It can be used to write computer
programs. Python was created by Guido Van Rossum in 1991 while working at the National Institute of
Mathematics and Computer Science in Netherlands.
APPLICATIONS OF PYTHON PROGRAMMING
Python is used in:
Developing games Data Analysis
Computer Vision for the services like face Creation of websites
detection
Web Scraping (to cater data from Robotics
websites)
Scientific Computing Artificial Intelligence (for machine
learning)
Features of Python
Easy to code: it is very easy to write programs in Python as compared to other high level programming
languages.
Open-source language: Python free of cost and use it on any operating system such as Windows Mac
or Linux
Object-Oriented: Python has an object-oriented approach. This means that the programs are designed
using objects and classes that interact with each other
Interpreted language: The code you create in Python is executed line by line which makes it easy to
correct any errors.
Dynamically Typed language: Python is a dynamically typed language. This means that no need to
declare the type of variables in advance. Freely available.
Installing Python
Python is a free and easy to use computer language. Anyone can download Python software from
[Link]
Once the software is downloaded, double-click on the downloaded file and click on the Run button. The
process will automatically install Python. Each step is shown to the user and the user just has to click on
the Next button. Once the process is completed, click on the Finish button
Modes of Python IDLE
Python IDLE used in two modes which are:
Interactive and Script. Let's learn about these.
Interactive Mode
Each code line is executed and output is seen on the next line. Interactive mode gives ut line by line. It
is mainly used to test the code for errors.
To work in the interactive mode, follow the below steps:
Step 1: Click on the Start → Python 3.7 folder.
Step 2: Click on the IDLE(Python 3.7 64-bit). The Python Shell window opens where you can see the
prompt (>>>).
How does the Interactive Mode Works?
When we type on the screen next to the prompt (>>>), the interpreter converts the written command into
machine code. For the output, the machine code is converted into the human readable format by the same
interpreter.
Script Mode
In the script mode, Python does not automatically process and display the result. The script mode is
used to write longer Python codes.
Perform the following steps to use the script mode:
Step 1: Click on the File → New File option. A new file opens. Step
2: Write your code.
Step 3: Save your file. By default, Python program is saved with .py extension. Step
4: Once the file is saved, click on Run -Run Module option.
The print() Functions
Python provides two commonly used functions input() and output() for taking input and displaying
output. Let's learn about these in detail.
The print ( ) function is used to display messages defined by the user on the screen.
Data Types of Python
A data type specifies the type of value a variable can contains. For example, a person's name must be
stored as a string value whereas the person's age must be stored as an integer.
Datatypes in Python are:
int: Positive or negative whole numbers (without any fractions) Example: a = 3.
Float: Any real number in which a fraction is denoted by a decimal symbol. Example: a = 3.5
String: A string value is a collection of one or more characters put in single or double quotes.
Example: a="hello" Using Separators with print
Multiple separators can be used with print ( ) function. Below are the examples of few separators:
Comma (,):
When we use a separator between the values in print ( ) function, the values are printed with a space
between them.
Tab Space(\t):
When the '\t' separator is used with print ( ) function, the values have a tab space between them.
New Line(\n):
If a “\n” separator is used with print ( ) function, the results are displayed in the new lines,
Comments in Python
Comments in Python can be used to explain parts of the code. It can also be used to hide the code as
well. Comments enable us to understand the way a program works. In python, any statement starting
with # symbol is known as a comment.
Python supports two types of comments: Single line comment and Multiline comment.
Single Line Comment
In case, a user wants to specify a single line comment, then comment must start with the symbol #.
Multiple-line Comment
Python does not have syntax for multiline comments. To add a multiple line comment, you could insert
a # for each line.
However the text written inside triple quotes (“”or”””) is also considered as comment. You can use
triplequotes to write multiline comments. For Example:
‘’ This “””This
Is Is
Multi Multi
Comment’’’ Comment”””
Activity
What will be the output of the following programs?
1 a = 4.5 b=2 2. y = 10 x = y += 2
print (a//b) print(x)
3. a = 4 b= 1 4. var1 = 1 var2 = 2 var3 = "3" print(var1
print(a or b) + var2 + var3)
print(a > 2)
5. p,q,r = 10,20,30 6. a=”EAST POINT SCHOOL DELHI”
print(p,q,r) Print (a)
WORKSHEET
1. Multiple Choice Quetions
a) Python is a programming language.
(i) High-level (ii) Middle-level (iii) Low-level (iv) None of these
b) Which of the following variable is incorrect?
(i) NaMe (ii) _name (iii) number (iv) @value
c) Which of the following is used as a separator with print() function?
(i) , (ii) /t (iii) /n (iv) All of these
d) Which of the following is true?
(i) Comments are for programmers for better understanding of the program.
(ii) Python Interpreter ignores comments.
(iii) You can write multi-line comments in Python using triple quotes, either “”or
“””,
(iv) All of these
e) Which of the following symbol(s) is/are used for comments in Python?
(i) // (ii) “ (iii) /**/ (iv) #
2. Write 'T' for true and 'F' for false.
a) A variable can hold different types of values T
b) Comments are the most helpful part of any program T
c) The print() function allows to take input from the user T
d) The input() function is used to display the data on the screen F
3. Fill in the blanks using the words given below.
1991, Guido Van Rossum, interactive, script, variable, string.
a) Python was created by Guido Van Rossum in the year 1991
b) The two modes of writing Python code are interactive and script.
c) The storage unit of data is called a variable
d) The default data type of input is String
2. Define:
a. print ( ) function.
b. Data type
c. Separators.
d. Comments
3. Long answer type questions.
a. Differentiate between Interactive and Script mode?
b. Explain features of python language?
c. Explain some application of python programming?