Python
Python
LEARNING OUTCOMES
to:
he student will be able
. know about Python and its features.
, install Python and identify the components of Python window.
work with Interactive mode and Script mode.
l use different data types, print and input functions.
Tomato Pasta
Fiqure 7.1: Making
There are some specific computer languages, which are used to write programs. Some of the
popular examples of programming languages are Python, C, C++, LISP, Pascal, Java, PHP, etc.
In this chapter. you will learn the basics of the
Python programming language.
Python
Python is a simple programming language. It is used to write codes for
the computer programs. Python was created by
Guido van Rossum
when he was working at Centrum Wiskunde &
which isa National Research Institute for
Informatica (CWI),
Science in Netherlands. The language wasMathematics
and Computer
released in 1991. Guido van Rossum
Features of Python
Some of the features that make Python so
popular are as follows:
It is an easy to learn
It is a platform
general-purpose high-level programming language.
independent programming language, which means it can be used on
machine and in any operating system. any
It has a simple syntax.
Python is a case-sensitive
language.
It is an
interpreted language.
It is free to use and even
free for commercial redistribution.
Applications of Python Programming
Python is a popular and
easy-to-learn language. It can be used to do the
Build a website following:
Program robots Develop games
Develop artificial intelligence based Perform scientific computations
applications
Installing
Python can be
Python
easily installed on your
To download computer by
following the given steps:
Python, visit the site:
Python button under
www.pytversion
'Download the latest hon.org/downlo
forads and click
Windows', will You
on the
Download
get an .exe file.
88
puthon
Let's Recall
ython 37.4 Ahigh-level language is a
computer language that
uses letters, numbers,
and speclal symbols for
coding. It is a user-friendly
and interactlve type of
language. For example,
C++, Python, Java, etc.
? Interactive mode
* Script mode
the in Interactive
Ine Interactive mode is the default mode. To launch
> IDLE (Python 3.7).
vde, click on Start > Scroll down to Python 3.7
Figure 7.3.
The IDLE Screen appears as shown in
89
Components of IDLE Python
Just like any other application window, the IDLE Python screen has the following components:
Title Bar
Menu Bar
n 2 Cet T2
Figure 7.4: Components of Python Window
Title Bar
This is the area that displays the name of the application and the
document. It also contains
command buttons, like Minimize, Maximize/ Restore, and Close.
Menu Bar
The menu bar consists of various menus with
different options. The menus of the Python
window are File, Edit, Shell, Debug, Options,
Window, and Help. File Menu
This menu is used to work with the options related to the Open. CttO
90
StatusBar
bar tells yOu the
The Status current status of your control or cursor on Let's Recall
the window,
You can prass the F1 key
to use Python Help
ATiNyZOAE
Visit the lab and
follow the given steps to get familiar with the Interactive
The Python Integrated
Development ano
Learning Environment
mode. (IDLE) executes
.Click on Start > Python 3-7 > IDLE (Python 3-7), It will open the Python instructions, one ine at
a time.
Shell window (interactive mode) where you will see the Python
prompt
(>>).
Type the commands next to Python prompt one-by-one as shown in figure
given below, and press Enter. Python will immediately give the output for
each of them.
elcee to ytho
12.7#
Let's Know More
Fale
12+70
python
Python is an interpreted
NOTE language. It means the
Python interpreter reads
Ihe Interactive mode is useful for testing code where you type the the text line-byline and
imands, one at a time, and get the result or error immediately. converts the command
(source code) into
machine language (object
Printing a Message code). After processing
the interpreter again
converts the machine
print( )function ís Used to display the output of arny command on code into human readable
the screen. It can also be used to print the specified messages. form (output)
Kips | Cyber
Quest 91
For example:
World" ) will display Hello World
hthn 1..4 (tagn /v1. 1,4:e09139112e. Jul 8 z019, 19:29:22) IMSC V. 1916 32 bit
EYEight . ctedit3 or |icense () " or more infotmatio.
Norld
Python 3.7.4
(taqs/v3.7.4:e09359112e, Jul
(Intel)) on win32 8 2019, 19:29:22) [MSC v. 1916 32 bit
TyEe "help. "copyright", "credits" or
"license ()" for more information.
Pvtho
print('Python 15 a high level
anguage',1t has easy syntax')
high level language It has easy syntax
int (1
"10+56=, 10+56)
10-56= 66
D>> print
('10*9-50+302/10=,
10*8-50+30*2/10- 36.0 10*8-50+30*2/10)
>> print ('5 plus5,5+5, 'and 5 into 5 =.
5 plus 5 - 10 and 5 into 5 55)
25
) Variables in Python
vou need to store some food for
future use, what will you require? Well, you will
container to store the food. Similarly, when you are need a
working with the values in Python, you
require some storage location to hold the values for later use.
Named storage locations in the
computer memory, which are used to store data are called
variables. Avariable can store only
one data value at a time. When a new
value is stored in a variable, its previous value gets
Dverwritten.
UOServe the following codes to understand the use of the assignment operators:
Variable Description
a=10 #Value 10 is assigned to the variable a.
b=20 # Value 20 is assigned to the variable
b.
(-a+b #The numbers are added and the value is assigned to the variable c, i.e., 30 is assigned
to the variable c.
the variable a. So, now the value of the variable a is
# Value 20 (30-10) is assigned to
a-c-10 20, and not 10.
NOTE,
If a variable is being used without assigning a value, i.e., if the variable is not defined, it gives an
error.
The following code gives an error because the variable x is used in the print() function without
being defined.
Python 3.7 (64-bit)
rhon ..2(t8gs/v).7.219aJffco492, Dec 23 2018, 23109:25) (MSC V. 1916 b1t
e help, copyright. credits" or "license for more information.
Prn CadP
95
e output wilU be generated us the Pvthon Shell window.
Oosee the difference between the Scriot mode and the Interactive mode as shown in
Figure 7.16.
Pthon 374 Shelt
Dinh Dekp/Teeh7 Pythen.D
t Foat Bun Eile Edit Shel Rebug Qptions Window Help
Options Windcw Help
a-l0 Python 3.7.4 (tags/v3.7.4:e0935
b=20 19:29:22) [MSC V.1916 32 bit (I
Type "help", "copyright", "cred
pEAnt ("the sun of the nunbers is:", c) for more information.
>>>
* * * = RESTART: C:/Users/Dine.
Python/B7/progl.py =
the sum of-the numbers is: 30
Script Mode Interactive Mode
Figure 7.16: Program Output in Interactive Mode
Practice Time
1. Write a program to print the general
details about your school in five lines.
2. Write a program to calculate the sum of two
given numbers.
Data Types
Suppose, you have planned a holiday trip duing
for vour family members. The first vacations, and you have to book tickets
action that you need to take is to fill a
reservation done, where you need to give your form to get the
iournev, name, age, etc. Here, every piece of required details, like train number, date of
name is a string type, age is a data that you enter is of a
numeric type, and date of journey is of specific tVpe: as your
have to specify in a program, what kind of date type,
values will be stored in a specific Similarly, you
Basic Data Types in Python variable.
In Pvthon, a
data type represents the
memory can be of many types. Pyhontype has
of data
stored in a variable, The data
stored "
of value, For various standard
example, a student s marks are stored as a data types based on the type
address is stored as alphanumeric
characters. numeric value. whereas. his or no
96
Kips
he basic data types used in Python are mentioned here:
Let's Know More
int (integer): It represents integral numbers (numbers without any
A variable is a named
iractional part). 1here are three types of integers in Python: memory location that
stores the data and whose
Integer type Description value may change during
progra execution.
Integer (int) It stores values in the range of - 2147483648 to
2147483647.
Long integers (long int) It supports the values that lie beyond the range
of integers.
Soolean (bool) It represents logical values in the form of True
or False. In Boolean, Orepresents Fallse and 1 Let's Know More
represents True. reserved
ywordS are the
Key
of a programming
fractional language. They cannot be
float: It represents floating point values (numbers with used as variable names.
as well.
part). The fractional part of a floating point number may be 0
Examples of floating point numbers are 3.14, 48.6, 18.0, etc.
of characters enclosed
str (string): A string data type represents strings
").
within single or double quotation marks (' Or"
Examples of strings are 'Hello', "Myname", 218, 'Peace', etc.
are:
Some examples of data types in Python
Value Data Type
15 int
7.9 float
Computer Application str
7>3 bool
Let's Know More
Acompiler is a language
a variable by
You can assign a multiline string to processor that converts
a program written in
quotes ("
using three high-level language into
For example: machine language, and
vice versa. just like an
of data stored
as"A data type represents the type interpreter but in one go.
memory can be of
in a variable. The data stored in
many types.
Program 2:
Python 3.7 (64-bit
V.1916 64 bit
s2.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) MSC
on.
Type "help, coovright. "credits" or "license" for rore informati
aingut (enter the fist value:")
t t r the second value:")
enter the second value:300
rint (c)
bona0o
Observe in the above program, when you add the two numbers, you do not get the sum. Rather
the numbers are joined together and are printed as a string.
Program 3:
In this program, when you try to multiply these two values, you get an error. This is because
the strings values cannot be multiplied together.
To work with the values provided by the user, Python provides a method as inthe
discusse0
given example:
Program4:
thr33AShet
fe dt She bug Ortens Wde He
Fython 3.7.4
32 bit (tags/v3.7.4:e09359l12e,
(Intel)| on win32
Jul 8 2019, 19129:22) (MSC v.1914
"help". *copytight", "credits" or license ()" (or more informat ion
int (input ("Enter the
Enter the first number: 100 first nunber:)
>> bint (input ("Enter the
Snter the second number: 300second number:")
>> pint ("The sum of
The sun of the
the nunbers is:", a+b)
nurbers is: 400
Observe, when you use int() function, the values are converted to integers and are added.
Program 5:
ECUeDnh Dektog Toels 2Pheni1
le lát Fomat Bun ens ndow Hele
anint (input ("Enter the value of a:")) Python J.7.4 (tags /v).7.4:e09359112e, Jul 8 2019, 13
(Intell
TYpe "he) lp,
on winJ2
"copyright", "credits" or "license () td
beint tin
D1nt (1nput ("Enter the value of b: "))
nrint (Value of a after swaping:" a) RESTART : Ct/Users/Dinesh/besktop/Tools 7
Enter the value of at 45
print ("Valua ofb after swaping:",b) zalue of a after swaping: 25
Value of b after swaping: 45
Program 6:
taltos PtherEl4
Square
Figure 7.22: Area and Perimeter of a Output
Practice Time
1. Create a program to calculate 5% discount on the total purchase of stationery.
kilometres.
2. Create a program in Python to convert miles into
1Mile = 1.6 kilometres
to CelsiuE
3. Create a program to convert temperature from Fahrenheit
celsius =(Fahrenheit -32) x5/9
Quest
Kips | Cyber 99
Using separators with print0 function
Using (.) operator: When you use, operator as a separator among the values, the values s.
displayed with a space between them,
Program 7:
-100
Type "he lp", "copyr 1ght", "cret
PEnt la, b, e) mmmmmmm mmn RESTART: C:/Users/D!
|10 20 100
>>>
Using tab space (1t): When you use 1t' escape sequence as a separator among the values,
the
values are displayed with a tab space between them.
Program 8:
NOw, in the Python script file, add In' with the input and print
function:
Program 11:
In the above output window, in order to give a better understanding, the text and values are
placed one after another.
RECAP
+ Pvthon is a high-level language, used to write codes for the comnputer programs.
. Pthon was created by Guido van Rossum when he was working at Centrum Wiskunde
and Informatica (CW).
The Interactive mode is useful for testing the code where you type the commands one
at a time and get the result or error immediately.
The Script mode is Used Tor writing lengthy programs in Python.
Named storage locations in the computer memory, which are used to store data are
called variables.
The print() function Is Used to display the output of any command on the screen.
+ Python has various standard data types based on the types of values stored in the
variables.
is used
input() function accept the value for a variable from the user.
+ The
Hints
" Variable " Guido van Rossum " F5 " Line-by-line " Keywords
2.
OO000O
T0 evaluate an arithmetic expression, it is not necessary to use the print() function.
Avariable name can consists of letters, digits, and underscore.
3. In Python, data types represent the type of data stored in a variable.
4. In Python, only one type of data can be stored in memory.
the user.
5. The print() function is used to accept the value of a variable from
together.
6. The string values cannot be multiplied
C. Application-based questions.
display the names of fruits separated
1. Kritika's computer teacher has given her an assignmentt to
she use with the print() function?
with a tab space in Python. Which separator should
quick
is working on his mathematics project work. He has to perform some
2. Amnan
should he use?
calculations. Which node of Python programming
the output
5, >>> print(10*t 20) will give
a. 30 b. 1020 C. 2010
variable?
2. What is a
Python.
Explain the different working modes of
3.
in Python.
types? Explain any tWo data types
data
4. What are
print) function?
What is the use of
D.
103
ASTIAYY
SEGTION
MY ACTIVITY Learning While Playing
A. Write the output for the following statements.
1. >>>print(' India', 'Delhi')
2. >>>print (' India', '\t', 'Delhi')
3. >>>print ('India', '\n', 'Delhi')
4. >>>a=10
>>>b=20
>>>c=(a+b) *4
>>>print( c)
5. >>> a='10
>>> b'20'
6. >>> a-13
>>> b=20
2. >>>Print('India')
3. >>>Print (age)
4 >>>a=10
>>>b=20
>>>a+b=C
>>>print (c)
two = 2
print(three)
hello = "hello"
World = "World"
helloworld hello + " " + World
print (helloworld)
A
Execute the following statements in the interactive mode of Python.
1. >>> 2* 3.14* 5
2. >>> 4+5/38-5
B Write the code for the following programs in the Script mode.
Write a program to find the product of tWO numbers x and y, where x=150 and y=200.
1.
It takes 3 hours to drive a distance of 192 km. Create a program to calculate the
2.
average speed in km/h?
3 Create a program calculate and print the circumference and area of a circle.
Create a program to input the roll number, name, and marks in five subjects of a student.
Calculate the total and percentage marks of the student.
topic:
Discuss the following
mode
Interactive mode vs Script
10
Cyber Quest