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

Python

Uploaded by

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

Python

Uploaded by

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

PYTHON

Python is a
popular high-level
pu eprogramming,
1989. Python is
extensive
programming
Put Programming
as
language. It is powerful language used
lang 8ene
a tor

games ively used to developed by Guldo van Rossuum.


He started applyingyno
develop:
Graphical User Interface websites
programs web applications
FEATURES OF PYTHON
.Easy co t s
easy to very
programming tanguages. This is
write programs in
Python
the English language. because the
as
compared to otner
ng
language used to write Python code
open-source language: Python is a free and
language is one
which can be open-source
Python free of cost and use it easily improved and distributed
by
prograrmming
language. An
open
on
any operating anyone. You can dov
system such as Windows, Mac or
object-Oriented: Python has an Linux.
designed using objects and classesobject-oriented
that
approach. This means that the
interact with each other. progra
Integrated and
Extensible
language: We can easily integrate
C, C+t. We can also write and
compile a Python code in CorPython
as
with other
languaE
C++.
Interpreted language: The code
you create in
it easy to correct any errors. On
Python is executed line by line whic
execution, a Python code is
intermediate form. 1his is known immediately convertec
as
byte code. The byte code makes it easier
the code in future. to execun

.Dynamically Typed language: Python is a


dynamically typed language. This means th
not need to declare the type of variables in advance. For example, if you write a =
15,
need to specify the variable a as an
integer or string.

INSTALLING PYTHON
Follow these steps to download and instll the Python software:
Step 1: Open the website http://www.python.orgldownloads.
Step 2: Click on the Download Python 3.8.3 button.

Introduction to P
OVARIABLES IN PYTHON
aabies are memory reference points where we store values which can be accesed or chaniged
r h e hames the variables
given to are known as ldentifiers. In Python, we oo opecity
p eot variable because Python is a dynamically typed language and it identies the varinble type

utomatically. Let's understand this with the help of the glven examplies.
num 0.4

Price 30.3
TOTAL round(price num,2)
print (ToTAL)

Deciaring and Initializing a Variable


in Python, variables are declared and initialized at the same time in the following way:

a 10
20

PELnt a a)

print ("b-", b}
20 and b - 40 will be printed. You can also assign the same value
On the output screen, a
to multiple variables at the time in the following way:

a b 20
print ("a", a)

print ("b=", b)

On the output screen, a 20 and b 20 will be printed.


You can also assign multiple values to multiple variables in the same line in the following way:

age, grade="Deepak", 12, *VII'


name,

print ("Name is", name)


print ("Age is", age)
print ("Grade is", grade)
O n the output screen, Name is Deepak, Age is 12 and Grade is Vll will be printed.

You must follow the given rules while creating and naming the variables:

A variable name must start with aletter or underscore character


A variable name cannot start with a number.

100 Touchpad PLUS (Version 2.1)-VI


python

fatest
version
for Windoy
Downtoad
the

Downiloading Pytho
can either
software.
You
downloaded
software
in t h e
on the the
o r locate
Double-click

Step 3 screen
b o t t o m of the WarningB
l o o k at the The Security
computer.
folder of your
Downloads
dialog box appeas window appears.

button. The Python Setup


Click on Run
Step 4 checkbox.
to PATH
Select the Add Python 3.8
Step 5
Now button.
Click on Install computer.
step 6 installed o n your
will be successfuly
Arter a few seconds, Python

PROGRAMMING IN PYTHON Interactive Mode.


Mode and

Python has two basic programming modes Script

in Interactive Mode
Working which gives
immediate
command line shell
Interactive Mode is a one
i n t e r a c t i v e mode,
we type
command. In
result for each command and gives
command at a time. Python
the executes given
the output.
Mode:
and work in Interactive
Follow these steps to start Python
Click on Start button.
Step 1
Scroll down the list of programs
and click on Python 3.8
Step 2:

folder PType here to serch


Click on IDLE (Python 3.8 64-bit). Opening Pythona
Step 3:
IDLE stands for Integrated Development and Le
The main IDLE window appears.
Environment. It is a code editor which helps you to write and execute Python program-
editor itself.

Touchpad PLUS (Version 2.1}V1


rhe program will
execoe o u p t will bee shown in the Shell window

Crtad /etA/i/eege/ y/y Tech Fact


tie Interactive Mode is
very useful for
a code as
testing
Cufput it
displays
he errors one by one.
INPUT AND OUTPUT
Python provides two commonly used functions input() and
print) for input and output.
The input0 Function
The input0 function takes the user's input while a
inputO function is as follows: program executes. The general syntax of the
input (sprompt>]
Here, prompt is the
string or
message we wish to
name
display on the screen. Example:
1nputCEnter your name: ")
>>> Enter your name: "Mohan"
The print() Function
The print( function prints or sends
the output to the
standard output device, which is
monitor. This function auto converts the items to usually aa
function will
strings, i.e., if you try printing a numeric value, the
print() automatically convert it into equivalent
also introduces spaces between items
string and print it. The print() function
automatically.
The general syntax of the print) function is as follows:

print (<expression 1><expression 2>...1


For example
print ("Python is easy").
Let's create a program to use the input() and print() functions.

yh 3
inputprintpy-C/Usenv/ADMIN/AppDsta
Ele dieFgrmat Bun gptionsondow Bep Python 3.c.3 (tag/v.8.3:6tic32, May 13 2020.
age1nput (Enter your age:) 22:3702) C w.1924 64 bit (ND64)1 on win32
P pyEight credita or 1icens
name1nput (Enter your name:) N tomatkon
print (name, "your age is", age)
BESTART: CrUsers/AKDN/Appbata/ Local/ Progtas
YeRon/rEhonaAnputpeint-py
Ankcit your age is 2

In3 Colt 23
Using input(0 and print) functions

Introduction to Programming
of Python Window
ponents
Thereea two components of Python loLE window
e n u Barn The Meru Bar of Python iOLE window is similar t o t h e Menu Bar of other program

u s mernts such as File, Edit, shell, Debug, Optiorns, Window and Help.

cursor after the symbol (>>)


in the window, This is koowwn as
Prompt
Fo ou wil see a blinking mto ython and
the user to enter c o m m a r o u r e c y get an ou
T h e Prormpt alows
stantly by presstng the Enter key.

Menu bar

g/vi.tiitici, Hay 13 3030, 331271623 tnne 9 6 HE


(AMDE4)1 tor m o e infometion,
p " , eopyetgh eredite or
15en 0

Prompt
Components of 1DLE

Type the commands at the prompt and you will immediately get the results. Simply, type
press the Enter key. The result 6 appears on the screen. Try more values and see what hapr

ton oS Cga/.dJtec632, May 13 1020, 2a i37103) sC v.1934 4


y p e h e l p , copyEight", aredita" or "11cenne (3 tor ore infomation.

6/ 2
3.0

yping commands

Working in Script Modethe commands entered by the user in the form of =


not save
The Interactive Mode does
Mode saves the commands
entered by the user in the form ofa program. To
Script into four parts. Let us discuss t
working of ScriptMode, we have divided the process
New File
Creating a

Follow these steps to create a new file:


window.
Step 1: Open the Python IDLE
A drop-down menu appears.
Step 2: Click on the File menu.

editor window with a blank


Step 3: Click on the New File option. A new
Sele
file will appear on the screen.

Introductio
A variable name can only contain
numbersap terscores (
atpharnurneric characters (al the letters of the alphalbet and
Vartable narnes are
case sensitive
Variable names Cannot contan arny special character or symbol
OLer's CATCH UP
Circle the incorrect variable in the
following code.
Roll no- 201 Student Name"Chirag
Section -'A

DATA TYPES
A data type specifies the type of value a variable can contains. For example,
be stored as a
string value whereas the
person's age must
a
person's name must
be stored as
Python are: an integer. Data types in
.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.
string:A string value is a collection of one or more
Example: a 35
characters put in single or
Example: a="hello" double quotes

cOMMENTS IN PYTHON
Comments Python can be used to explain parts of the code. It can also be
in
used to hide the coem
as well. Comments enable us to understand the way a program works. In
python, any stateme
starting with # symbol is known as a comment. Python does
not execute comments. Commentsa
not part of the program, but they do enhance the interactivity of the program and make it reada
a

ython supports two types of comments: Single line comment and


Multilinecomment
Single Line Comment
ncase, a user wants to specifya single line comment, then comment muststartwith the symb
Example
Program: Output:
#printing a string HelloWorld
print( "Hello world")

Introduction to Prograrmn
of the above pro8ram is:
The result

4 cceg/ r 9 , Hay 13
help eedits or
3636, 333627 MC. 1924
1ionss 0 tor m
nto
atsonal eperstore.py
dpak/Aepbata/lecsl/Progtamn/yt h 39/tel

PRECEDENCE OF OPERATORS
Precedence of operators determines the order in which the
orecedence in
Python is listed in the operators are executed. The operat
following table. The highest
precedence is at the top
Operatoor
Name

Parenthesis
Exponent
Multiplication, Division, Modulo, Floor Division
Addition, Subtraction
Comparison
+, 5,**=, ll=Assignment
and, or, not
Logical

SOME MORE PROGRAMS


Program 5: To subtract two numbers entered by the user.

T E CEnter the firat


-ne
Lnt (input (Enter the second numbert
nup.
ert
)

aub a-b
print ('Difference ot two numbers ias', aub)|

Introduction to Programm
Example Output
Name Destrigtion (eu7 arnd ys

Divides left hand operand by rigpt hand xIy


DivIalen
operand,
3y
Modukis
Dvides fert hand operand by right hand
e t a r d ard returrns remalrer

Exponentiatto o s enponential (power) calculation Y


On operands

Floor or integerOvides ond cuts the fractional part from the IlY
divis result
Program To perform all tfhe arithmetic operators

pkas

The result of the above program is:

Caun/v3.8.3168o8 32, May 13 2020, 22i37102)


1924 64 bit AMD64)1 on win32
1koenae () tor wore h
Type help", oopyright". "oredita" or

Formation.

-RESTART C/Uaeta/deepak/Appbata/l4ocal/Progtama/Python/Pytbo
n38/Arkthmetie operatoEs.py
13

6561

Introduction to Pm
Mullpfo- ine Commo

iert
9 P or so
fnange Heio orld

(Prngrwm

prHCHoWorey A
priHa)

print("Akshe
5 5619A.Yo t
guotes( " o )
s0 60eted
insde toiple
However, he teatfowritten
wrte
t n e
6omnerts:
For e s a r n g i e

e e gotes
Muls31e

OR Comens#n
MulEEEe

Consent'7

oPERATORS o n perform
aimetic and logr
special syritvols
are caed oper ariis. For evange,
be defined as
dan
In Python, operators use to get the oupt
the operators
computation.
The values whicdh
and the minus ( sgn is the operstor,
10-5, 10 and 5 are operands
in the expression

in the folloing.categories,
Python divldes the operators

Arithmetic Operators arthmetic.opertos


performarithmetic.operations betweentwo operanids.The
rithmetic operators
are defined in the following tables
Ezampie Outpi
Description
Operator Hame
(a7 and y:3
Adds values on either side of the operator Ky 10
Addition from left hand -Y
Subtraction Subtracts right hand operand
operand.
Multiplication Multiples values
on ether side of the operator 21

102 Touchpad PUB (Version 2.1)


marks of three subject
Program 6 To calculate the average

ACTOPEPA

volume and surface area of cylinder


7 To calculate
the
Program

Python is one of the


ofical languages at

Gooe

----
The name Python Lars BAeK-Up
Python isa popular highlevel programming language
was derved from the
Sntish comedy series Interactive Modeis a
command line shell which gves
immediate result for each command.
"Monty Python's
Fying Grcus" IDLE stands for Integrated Development and Leaming
Environment
The input) function takes the user's input whille a program
executes.
The print0 function prints or sends the
outputto the
standard output device, which is usually a monitor.
ccording to a sunvey, Variables are memory reference points where we store
in the UK in 2015, values which can be accessed or changed later.
Python overtook A data type specifes the type of value a
variable can
ench to be the most contains.
opular language Comments enable us to understand
the way a
aught in primay works.
program
schools Operators can be defined as special symbols which perform
arithmetic and logical computation.
Arithmetic operatorsS perform arithmetic
between two operands operations
The assignment operators are used to assign the value of
the right expression to the left operand
n does not need
Relational operators are used to compare the value of
operands and returns True or False accordingly. the
Diler to execute two
program.
Precedence of operators determines the order in which the
operators are executed.

pad PLUS (ersion 21


Relational Operator
Relational operators are
used to compare the value of the nd
two opnos
d n g y . The relational operators are described in the following a o
returms True or False

Operator Name Description Example Output


(xoB and y-6)
x y
tqual t checks if the values of two operands FALSE
condition
are equal or not. If yes, then the

becomes true. X Y
Not
equalt checks if the values of two operands are TRUE
then
If the values are not equal,
equal or not.
thecondition becomes trud TRUE
ureater t checks if the value of left operand is B8reater >y
if yes, then
than than the value of right operand.
the condition becomes tru
of left operand is less x<y FALSE
Less than it checks if the value
t h a n the value of right operand. If yes, then
the condition becomes true is greater x >=y TRUE
if the value of left operand
Greater
t checks if
operand.
than or
than or equal to the value of right
equaltoyes, then the condition becomes true
left is less
the value of operand x<=y FALSE
Less than
it checks f
or equal to than or equal to the value of right operand. If
becomes true.
yes, then the condition

Program 4: To show all the relational operators' functions.

exasples of relational operatota


- 332
greater than b
check t ais
PE t a ia less than
print (a c b)
to b
fcheck f a ia equal
print (a D
Bcheck if a is not equal tob
prin i s greater than equa to b
print (ab
Fcheck t a ia less than equal to b
pElnt (a < b

106 Touchpad PLUS (Version 2.1)-VM


Assignment Operators
operators are used to assign the value of the rgght expression to the left ona
The assignment operators are described in the following taDe
operand,
OperatorName Descipto
the value of operand on the right side to the left e
n tassijgns
side operand.
t adds right operand to the lett opet alno and a5s1gns the
Addition
to e f t operand. xom5 5 equivalne to xX*3,
resuie
uotraction t subtracts right operand from the lert operand and assigns
the result to left operand. x 3 15 equivalent to xx-3,

u r t i p l i c a t i o n i t multiplies right operand w I n the t e t operalna and assigns

ment the result to left operand. xm3 5 equivaient to xX3,


I t divides left operand with the right operand and assigns
Division
assignment the result to left operand. x/a3 is equivalent to x=x/3.

Remainder I t takes modulus of two operands and assigns the result t o x


to x=XE3.
assigment e t operand. xE=3 is equlvalent
Floor division It performs floor division on operators and assigns the
xlla3
assighment value to the left operand. x/=3 is equlvalent to xex//3.
* Exponentiation It performs exponential (power) calculation on operatorsx
usgnent and assigns the value to the left operand. x**=3 is
equivalent to x=x*3.

Program 2: To perform all the assignment operators.

n of ans1gnmant operatot
b

a+b
)
nE
PELnt (o)

print
c / a
nt (o)

rint

print
PELRE (C

04 Touchpad PLUS (Version 2.1-V1


Thenutput of the above progra ist

-
yight, oeit
A se"et
pe/hvyteta/lonsi/Beon/yn/ytn18/a*
tos
ataa

Logical Operators
Logicaleo p rators are used to
evaluate and
decide. Python
operatoraeDescription
supports the following logjcal operators
AND t
operands are Example (xa)
and returns true, if both
true. Output
(x5) and (x<10) TRUE
OR
It returns true, lt one of the
is true. operands x5) or (x<2) TRUE
not
NOT It reverses the result, returns
the result is true or vice false, if not [(x<5) and (x 10)) <
FALSE
versa.
progran 3: To perform all the logical operators.

b rala9

print and b)
pEint (not a)
print (not b)

he result of the above program will be:

PVEnon 3.0.3 (taga/v3.0.316teo632, Hay 13 2020, 22137102) MBC V


1924 64 bit (MD64) 1 on win32
for nors info
Type "help", "copyright". "aredita"
or
"1icenas ()
Kmatlon
RESTARTI CI/Users/deepak/AppData/Loonl /Prograna/ Python/Python3
8/10g1oal operatora-Py
Talbe

Introduction to Programiming

You might also like