Chapter Structure
12.1 Introduction to Python
12.2 Getting Started Python
12.3 Creating a Simple Program in Python 12.6 Operators
12.4 Running a Python Program
12.5 Notion of Data Types and Variables
12.1 Introduction to Python
Python is a high-level, object-oriented programming language. It is created by
Guido van Rossum. The Python was first released in 1991. Python is now one of the
most popular languages. Python got its name from a BBC comedy series - “Monty
Python's Flying Circus”. Python is based on ABC language and Modula-3. Python is
an easy-to-learn yet powerful object-oriented programming language.
12.1.1 The Features of Python
Following are some basic features of Python: Gudio van Rossum,
developer of Python
1. Python is a free and open source programming language.
It is easy to learn and use.
It can run on multiple platforms or operating systems such as Windows, Linux, UNIX, Mac OS, etc
2.
3.
4, Python is an interpreted language, which makes it faster than other compiled languages.
5. Itis suitable as an extension language for customizable applications
6.
Python provides interface to all databases, e.g, SQL Database, Oracle and many more.
12.2 Getting Started Python
Python doesn't come prepackaged with Windows. So it is necessary to download it from the Python's offical
website and install into the computer.
@ scanned with OKEN Scannerpownloading Python
joad Python 3.64, follow the steps
Goto the official website of Python, ie
jnen click the Python 3.6.4 button.
python 3:64.exe will be downloaded to your comput
ry ter.
python
2 Installing Python
«stall Python on your computer, follow the steps:
Go to the Download folder or where you have saved
Python-3.6.4.exe.
Double-click the file Python-3.6.4.exe.
The Setup pop-up window appears
Install Python 3.64 (32-bit)
| ee eee
> case on
Ba ancora toner
Draerjnons em
Figure 12.3: Setup pop-up window
https//wwnh
7 Python.or
oto the Downloads menu and choose the platform i
oro
perating system (e.g, Windows)
oss
@
=
ee ©
Figure 12.2: Double-clicking the Python file
Note
Make sure that the Install
launcher for all users
(recommended) and the Add
Python 3.6 fo PATH checkboxes
are checked. If the Python
Installer finds an earlier
version of Python installed
‘on your computer, the Install
Now message will appeor
as Upgrade Now (and the
checkboxes will not appear).
@ scanned with OKEN ScannerComputer Applications-Ix
3. Then click the Install Now option. You can also click the Customize installation option for manyay
installing.
4. The new Python 3.6.4 (32-bit) S
(see Figure 12.4).
5. When setup will be finished, soon a new Python 3.6.4 (32-bit)
Setup was successful message (see Figure 12.5).
was successful
J setup Progress ‘Setup was succes
iocnomtaoee
a=
Seat te eacrem tana
etup pop-up window appears with a Setup Progress message
Setup pop-up window appears with »
pyth pyth
wind ot wind
Figure 12.4 Figure 12.5
6. Then click the Close button to exit.
12.2.3 Verifying Installation
Before verifying installation, you have to set the path for Python. To set the path:
1. Right-click on This PC icon and select Properties.
The System window appears.
Sie a
45D cettil» Sm tsecety + Sem +O Senncowind
Covatiatone ‘View basic information about your computer
Dose tag Wind bin
a Minden he 7
8 sree, | ect an g Windows 10
Sm
ee en con SS CU Om 3G
teeny 1063
Senne 40pm otc
Rett Weert ety
Conn in begin
Conpnane We Gens
fatonparine ODAC
conn tain
Deni wacom
indo tin
se deisel ele stow ame
Sony dtince PUNE OE Genser
Figure 12.6: System window
238
@ scanned with OKEN ScannerLede
(see Figure 12.7)
indow appears (5
_ advanced system settings. The System Pro,
select Perties window
appears,
on Environment Variables, The Environment Varia
5 es Wi
a Figure 128)
ae oe
tomate road bese
ge ore es
soveed Sytem Ptecton Rete
a neroy ae ea ney
So eee
=e ne Cette
. Comramaecnes ce
Seana rere =
cs ed -
wo ae
ea eter cine cane
en tan ncre tion aetecemecson & —
2 neg
- Cece mton mains msn.
Gant ow mm nn see
ecteaaomcnat seu
_AOGESORCOCH teste eet J
Soe aoe
2 12.7: System Properties window Figure 12.8: Environment Variables window
go to System variables section, select Path and click the Edit button.
dit environment variables window appears.
‘ent variable window
12.9: Edit environ
ue "C:\Windows" and click the OK
rour Python installe
i ww appears (Figure 12.10)
Variables window.
6 .d location ie.
Click the New button and type YOU" '
button, The Environment variables windo
7. Then click the OK button in the Environment
@ scanned with OKEN Scanner: - - 1
8. Finally click the OK button in the Systems Properties window (see Figure 12.11).
yam Pap
comuctine Hoteoy Mewee! Sytem Pcn ete
eunutbeypdonain RnantaronsenetPe age
(eereta recnan hey emi sn aren,
som
sen
pment ennai debian
seve
rt Vs
Figure 12.11: System Properties window
Figure 12.10: Environment Variable window
9. To check whether the path is correctly set or not, first open Command Prompt (CMD) by typing CMD
in the search box.
10. In the Command Prompt type py or python. It shows the installed Python version. It means your path
is correctly set.
Figure 12.12: Verifying installation
12.3 Creating a Simple Program in Python
IDLE is the standard and the most popular Python
development environment. IDLE is an acronym of
Integrated cmpeagen
To-create a simpleVprogtam in IDLE, yifion, follow the
steps: Hello wows) d 7”
1. Click the Start buen, and select IDLE Python.
Figure 12.13: Starting Python from the Start menu
@ scanned with OKEN Scannera ree
Clicking New File option from the File menu
xample, print (“Hello World”)
code
16: Typing the Python
Figure 12.feces eas
4, Before running the program it is mandatory to save it. To save the program,
2 Save. Or press Ctrl+S from the keyboard.
go to File menu and select
> Figure 12.17: Selecting Save option from the File menu
The Save As dialogue box appears.
Figure 12.18: Save As dialogue box
5, Choose the Idtétion where you want to save the program_and type the program name
(eq, FirstProgram) in the File name box. Then click the Save button.
The program will be saved with the extension .py. 3
12.4 Running a Python Program
You can run a Python program in the following two methods:
12.4.1 Running in Python Shell
To run or execute the Python program in the Python shell:
1. Goto the Run menu and select the Run Module option (see Figure 12.19).
Or
Press F5 or Shift + F5 from the keyboard.
@ scanned with OKEN Scannert will be shown in the Pyth
ython shell (see Figure 12.20)
a cite,
| ie
|
|
ecting Run Module Figure 12.20: ython she
MD (Command Prompt)
cet the Python PATH, you cannot run the program in CMD: So
am in CMD. To run or execute Python program in CMD
Command prompt from the Start menu
ere your program saved. For example your program is saved in D d
ied.” command. Or directly press D: t
ocation wl
nge directory location by using
a Sao
Figure 12.22: Changing directory location
will be shown as
ure 12.21: Opel
press the Enter key The output
Type the file name (
Hello World"
Bin ine Command promet
@ scanned with OKEN Scannerll be shown in the Pyth
N shell (see Fig
jure 12,20),
ing Run Module
Figure 12.20: Output in the Python she
menu
M
D (Command Prompt)
Python PATH, you cannot run n in CMD, So first set the pat
in CMD:
MD. To run or execute Python progra
Command prompt from the Start menu
© 12.21: Opening Command prompt Figure 12.22: Changing directory location
fe the file name (eg, FirstProgram.py) and press the Enter key. The output will be show
lo World’
ommand prompt
Figure 12.23: Output of the program in the C
@ scanned with OKEN Scanner12.5 Notion of Data Types and Variables
he data type me
Variables t t
Rules for naming variables
12.5.1 Integers Data Type
Integer data types are used to work with numbers. Python supports
Example
pee)
—_—
@ scanned with OKEN Scannerata type
Point", ang
and it means
"$8 number with a q
er with a dec
'mal point, e.9, 07, 19 4
1gs Data Type
n Python are identified as a contiguous set of characters in between quotation marks. Python allows
airs of single or double quotes
J Single ‘' Quote
Using single ‘ ' mark.
sing Double “" Quote
ple: Using double “" mark
«makes it easy to include apostrophes
g double quote
in
two is that Us!
a you output as
it gives
‘ shone.
fference between Don't touch my pI
type “Don't touch my phone»
@ scanned with OKEN ScannerIfyou type Duch my phone’ it gives yc
program,
Using Triple ““ "” Quote
You can use triple quote to store multiple line value
Eaxmple: Using triple“ " mark.
12.6 Operators
Operators are particular symbols that are us
used in application. For example: 10 + 2
duces the output 12.
This expression pi
Python supports many kinds of operators.
12.6.1 Arithmetic Operators
The arithmetic operators are used to per
explained in Table 12.1.
Table 12.1
2, Here 10 and 2 are operands and (+), (=) signs are the operat.
his chapter, we
m various arithmetic calculations. The arithmet
‘or, because your code runs only and terminates
‘0 perform operations on operands. It returns result that can b
ill discuss only about arithmetic operators
erators are
Python Arithmetic Operators
Description
Addition (+)
fo perform addition
To perform
To perform multiplication
To perform division
This operator is used to add two or more values.
BE Example 1: Add two valves.
@ scanned with OKEN Scanneron (-)
t two values
je
© values by storing value in another variable
ation (*)
> multiply two values.
Multiply two values.
TW hyoenis oa
“ample 2: Multiply two values by storing a valve in another variable.
Bimmisoaed
tiplication =numl*num2
tint (multiplication)
247
@ scanned with OKEN ScannerDivision (/)
Example 1: Divide two values.
Example 2: Divide the two values by storing a value in another variable.
SUMMARY. j
7 python is a high-level, object-oriented programming language. It is created by coi aaa
* Some basic features of Python are
(i) Python is a free and open source programming language
ji) Itis easy to learn and use.
or operating systems such as Windows, Linux, UNIX,
(ii) It can run on multiple platforms
Mac OS, etc.
iv) Python is an interpreted language, which makes it faster than other compiled languages.
IDLE is the standard and the most popular Python development environment. IDLE is an acronym of
Integrated Development Environment.
= Python program are saved with the extension .py. The shortcut key to run a Python program is F5
or SHIFT+F5.
* The data type tells a Python ee what kind of value a variable can hold, Python includes many
in-built data types, for example, string, number, float, etc.
‘* Variables are reserved memory locations to store values. A variable has a name, a type and avalue.7
* Integer data types are used to work with numbers. Python supports two types of numbers — integers
and floating point numbers.
* Float is short for “floating point", and it means a number with a decimal point, eg,, 0.7, 10.2, et
ilinand
@ scanned with OKEN Scanneretween quotation marks. P) i
an 5 Python allows for either pairs of single
< Operators are particular symbols that are used to
«The arithmetic operators perform
'0us arithmetic calculations, So
are addition (+), subtraction (-), Multiplication (*) and division (/). C
+ Command Prompt or CMD: A command line inter
programming
+ IDLE: The standard and most popular Python development environment.
Program: A sequence of instructions given to a computer to perform certain tasks,
* Python: A high-level, object-oriented programming language.
String: Any sequence of characters (Le, letters, numerals, symbols and punctuation marks),
+ Variables: Reserved memory locations to store values,
rrr
EW EXERCISE
I. Multiple choice questions:
Python is a
(@) hardware 6) programming language —(c)_none of these
The keyboard shortcut to run a Python program is
@ 5 (b) FT © FN
Which of the following is a floating type variable?
@ 10 Mey 75 | (©) hello
Strings in Python are identified as a contiguous set of characters in between
(@) single quotes double quotes “4 both (a) and (b)
‘operations on operands.
ae (0) Variables
@ scanned with OKEN Scannerlcm nercurals
IL. Fill in the blanks: ES.
1. DLE is the standard and most popular Python development environment, i
2. Python program are saved withthe extension £0 a
3. data types are used to work with numbers.
4. are used to perform various arithmetic calculations.
OserPor -
TIL. State whether the following statements are true or false:
1. Python comes prepackaged with Windows. F
2. Python can run only on Windows Operating Systems. F
3. Python is a free and open source programming language.
4, Python variables need to be declared. F
IV. Answer the following questions:
AC What is Python?
\2> What is the official website of Python? h¢ + Ps? li www. P
<2-Wiite the steps to create a simple ‘Hello world” program.
Beet ie toyell mean by data types?
(UB What are variables?