Python Programming Fundamentals, Lecture notes of Computer science

These notes will help you understand the basics of programming in Python.

Typology: Lecture notes

2018/2019

Available from 02/10/2023

probalkar
probalkar 🇮🇳

7 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
elAsSMAte
Date
Date3/7/19
Page
PYTHON FUNDAMENTALS
Pthon Character
Set
Charachersetisasetoh uolidcharacdensthaialancuagecon
recognis.e characterrepresedsany letterCclpholoei),nunaeer
OrQn ctherSpecialcharocler.PathenSuppords UN1CODE encodng
thonhasmany characheísetIke A-1,a-7,0-9,$,-C
2,<, &
lokens
lheSmallestindividualottcoproaroaneiscalledtoken
heyarealsoknownas
lexicalunit.
Pathonasolloingtokenj.
keuords
nidevtiers,
a,Herals
operadorsCnd
punctuators separators
Keywords
elueuwerdis.awerd hwingaspecialmeaningtis
a
reseryed
word hyoproroaanagungehesewocl
resexedhoc specialpurpogeand mustnet bebsedas natma
idendiiernaes txamples ralselrue,del,in,aor,or uhile,
loreak,is,rom returnj,else,not,andclass,Htu,aport,
continueexcept,
dinety,elikec
or
2denkkiersUserdetimednamas)
ldetidiersaredhe dundcamevtalbuildingluldcksottherogram
Qnd are used as general
dereinologdor theiame qiveu
to
oliteretpardsotProgramforex varialulesoajecis,clasSe,
pf3
pf4
pf5
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Python Programming Fundamentals and more Lecture notes Computer science in PDF only on Docsity!

elAsSMAte

Date

Date

Page

PYTHON FUNDAMENTALS

Pthon

Character

Set

Charachersetisasetoh uolidcharacdensthaia lancuagecon

recognis.e characterrepresedsany letterCclpholoei),nunaeer

OrQn ctherSpecialcharocler.Pathen Suppords UN1CODE encodng

thonhasmany characheísetIke A-1,a-7,0-9,$,-C

2,<, &

lokens

lheSmallestindividualottcoproaroaneiscalledtoken

heyarealsoknownas

lexicalunit. Pathonasolloingtokenj

keuords

nidevtiers,

a,Herals

operadorsCnd

punctuators separators

Keywords

e lueuwerdis.awerd hwinga specialmeaningtis

a

reseryed

word

hyoproroaanagungehesewocl

resexedhoc specialpurpogeand mustnet bebsedasnatma

idendiiernaes txamples ralselrue,del,in,aor,or uhile

loreak,is,rom returnj,else,not,andclass,Htu,aport,

continueexcept, dinety,elike c

or

2denkkiersUserdetimednamas)

ldetidiersaredhe dundcamevtalbuildingluldcksottherogram

Qnd

are

used

as

general dereinologdor theiame qiveu

to

oliteretpardsotProgramforex varialulesoajecis,clasSe,

elasSMAte

Date

Page

unctians,

lists,dicdionaries, HuplesetcLL

dentiier doring cules

ct

Pthan

coAn identiieris an

and digts

n Eirst

clioracter

must bea letter ,romadtercapitalac

Smal

inclüdlitg

urnilerscore

iAidethier wnus i beakeyworol.

aAn identiier cannot

cotainany

specia charater6her

an

undeiscore

l-).

arbitrorilylongsequen.ceotlettes

ppescase

onc lower

case lettes sareHreadedi dihecety

aS

Pahahisa

case

sensi-tive language. HN

chacacteesose

sisnikeant.

(N

NUmeric

disits canbe used as_an ideriher namei

excep or

the

irstoháracder

(Nindentitiersin Phn Can hR_ot

unlimited

lenath

iHerals

LHera 8 are oHen

rettered

oQs constart alues

Hota tems thahas aixed value.here

acesevetoa

kind d ltercls,like stinglideral, humericliteral

booleon

idera,

Special

dera none, liteal

tollecton

StringLidera

fan

A Het endosednquotesormsa

string iteral

in

Pthon

Ea:

Probal

A

singlechor

acier

or utiple

char actetsendosed whiinl quo esase seated

asgtring

HeralA striigletad isaSeqenceor chatacters

SuRrOUnded lbypotes

alasSMAte

Date

age

äibFloating-Peint Literals

Eloating-hterods are aao_calledreal lideralsPeal

litRrals Gre

numbers hawinractionalpart.lhese

numbers Can be representedeitherinhsacnonadtecmsr

Exponetiad Hecms. 6:5.A, 3.6,33.91, 3.8xIo

A real Constaat n tactionalotamust hwe adleasione

dioitwth a decimalpai eitherberoneorattertmo

olsohawe ether

derauhitisplus iladacealicons andinexponert

oL has two potts mantissa

iH mush beoninteaec

propes sed conotant)atistallawedbyleterEexponerd

whichmust be

an inteser

puso_inussiqnprecealingt,b

nCoplexNumbens

So a numerievalueindhe dorm cr athiisreatedlas

Conaplexhuwbers ih Pahon where and b canbe

integerorrecpartadthevalueotiišo-td d

(CBooleanLiterals

A bodean liheral

inPythan id

Vacd do repsesentone c the

twa hoolesnalhuesIueocEalse Booleanis

alsacreated

underirdegerwhereIne

1g

represerhedag A-and ralse

is

LcSpesioilerolNone

inniPhonnascne iSpecladesadahidhkhoe.The nanel

lidecal

isuses odicate

absencelckivalue.alone

indhon

i

meonsthere

SMOUSeulinèocnmationorhete hathim.

AOperators

elassmate

Date

Page.

Operatas

are another okens

dhat

digger Same

coapetation

Ahen

appli.caltthe vaciales

Qnd cther

ohiectsinan

epresšion.

Varichles

and cl echsoduichdhecomputations_are

pplisdare

called

opeands.bo, opeíator sequYes_

Operondoorh Upon

here are variauapes o

operators

Brhnaetic

Oparetor.

a ocopercors(t

binarOperatorS ( X,,**,)

a a peraturs

VharoperadorsareoSe_operadorsthatrequirecneopercind

o

operateona

ofinary Operators

16inaryOperatorSrefhoase yeetatorsdha requireswo cper.cnd

aperade upas

2 ihuiseOperador

BHiseAND

Bitwise OR

BtwiseexclusiveOR (xoR)

s)ShiOperador

Shiftteft

Shst

rigkt

Adentity Opexcder

fAt-21odu2 N 4

16 not unh

lasSMAtee

Date

Page

nhem ad emphasiso expressions.,stahementsand propam

structresExample

COL]i1@

Input

and

Output

var Ccmputer Sckence

Or

2IkorrmaticsBachices

prit Cvar1,and ,va2)

OutputiComputerSclenceans lodormoticsPrctices

rawinput

EunchioninPithondlawsausertapiveinputtoprogta ranm a

keuboard butinAhe darmotdting

Nate: tasinput)hunchonisdeprecatedinputhan]

e-g

Oge irtLraw-inputC'enteraurage'))

percentoge=loatLIasinputl"ederpercentage)

input()

FunctioninPython allousa userta giveinpuloaproaramroma

kehoard butcehurnsthevalveaccordingl-

eg

oae IntGinput'ederyouroge').

C age+ wttnotproduceany error

INotenpudunctionoliwaysender 8tringualueinputhon3Soan

need int,load)AunctionCan eUsediHar dlata.conversion

Carebone ora Python Progiam

#Sunction

delindiocn

conmment

dek keyAtgFuncempname,emprale)JEunction

elassMate

Date

Page

printEmp

Name:, empntme)

printEmp

Rale :,enprole)

Function

return

Indentodian

A: 20

expression

pitCalling inproper oequence)

keyArgEunc(empame

Nik,emprole

Manoges)

Stateruets

printCollinginopposihesecuence").

heArg Funclemprole

Manaaer"

empnanneNick)

A pthenprogrcncortainthedall.owingcompanén s

aExpressicns

bStahements

Coxnmertts

Funchion

Clock and îndentotion

aExpressionwh isevaluatedanclproduceresutEsq (20+4)/

b.Stodeme înstructionhaddoessomething-

20

pritCallinginpropexSeqvence

29

C.Commens

whichis

readableorprogrammer butignare by

Pthon

imerpreter

Single line

comment

Which

begtnswth #Sign

nMuti

line

omment(docstring)

Eherwritemultipleline

beanning

wth

Sgn

or use

driplequcted

multiple

line

G

hisis mg

irs

G

eython_mutiline

Commen

AFuncion'A codethad hos Sornenameandttcon be reused.ea

elassmate

Date

Page

Glabal

Voriable

del un.C)

print()

Calling

voriable nsideunL)

unC)

print(x)

CellingVoriable"a cutsideun.C

Dunamic yping

Data dype ctavariabledepend/change upondhe valueass.igned

o a variablecn each nextSta ement.

X= 25

X:pthon

#x

varialedada dype

change to stringonjust

next

inegerype

Iine

Now poamer Mouldoe awarethatnctowrilehkethis

Y= x| #error| String cannctbe devided

Output

printO

FunctioninPchon is used oprit outputon dhe scseen.

Suax o Pritfunction

printlexpressicn/ylable)

e-g

print

G22)

Output- 122

prinhella

India'2)

Oukput hello

India

alassMate

Date

Page

print

Camputer,

Science?

print CCompuer,Sciente,Sep

prinlCCamputer,

Science, sep:R2,end:)

Output Computer Scienge

ComputerA

Science

ompuler &

Sdence.

put From

Keuboard

Nariable = înput(<

Promph to

display 22

e hame= inputwhatisuourhcame:2)

e

lnputO tunction

alwaysreturnso

valueok strinq

duPe

uou

enterînteaer

volveîtuillbe treahedasshring

Qge

int (inputl"hlhat iauage :)

upelage

Reading Numbers anc PossilbleErrarsaSsaciateduthit

Strina-

String values_Ccnnetbe_uSedhor _arthnmetic_orsthernunmeric

Cpercdicns.So itauSerneeds toread nundoets aeielyusing

input willnat Julh:ithepurpose,he unclions in_and

loadwil Conver o reluindhe Naluesreceivedhsouga

npLt IntocoirespondingInte6ervalvesand loatingpsiht

Vadues repecivey

Naricklèii(inputbcouai)

Varichle loatCinputCpronoh))

Ea-

user plons

t

inptintegeror loodin-eaiulnumaber

usin

input()

inside

inO or

loadc

elasSMAtee

Date

Page

cube

A*

print( Cube ot A is,

cube)

iQwhatdo you eanlay hone lHeralanclwhen doestbeinq

uSe

None tera lsa 6pecig ea wiehisused to

indicaie

Senmetingha has rdbeencreaded hilaeingused when

here s no

Value

2ldexrtiheupes o llerds

a23.

in

aü rue

Nonee

alse

iizfalse"

pElooting nnlaerlideta

d ndeger

nulber liHera

iiBealean

lderg

Stin lder

Speciat

litera plone

yiGaaleanliHera

iStringlidera

Lnfin

CCl

30

hHe o Pethom_code whichipul6aHempecadureinCelcius

and

Conver iinactoateiheit.

AQ.wrdea Pthoncode aichaapitslegthand lbreath.

Eind areac rectongle-

lASSMAte

Date

Page

32

:bat Cinput

()

inttiput

t) : x

2/

M

print (4= Ox+

print The

demaenature

n

takrenhet

" f)

AG ntCin pu)

bintinput)

print (b)

Swhc will be theouput

ot he

dloing code

hag mext

print (n)

-2,

22

prit(, y

15

122

(oWrtea

Pthon

Code

which

inpohs

principle,

cate

and

me

and

pri

bimple

inderest

p intCinput( Ender principle ")

intinput( Enter rale2))

in (inpui(CEder

Aime

"))

sip*1At

/i