[go: up one dir, main page]

0% found this document useful (0 votes)
227 views10 pages

Al Ict Notes - 05

The document provides information on for loops and other programming concepts in Python. It discusses for loops and their syntax, including iterating over a sequence, specifying a start/end range, and using a step value. It also covers break and continue statements within for loops, standard data types like numbers, strings, lists, tuples and dictionaries, mutable and immutable data types, and functions for working with data types like type(), id(), and is.

Uploaded by

shanketheesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
227 views10 pages

Al Ict Notes - 05

The document provides information on for loops and other programming concepts in Python. It discusses for loops and their syntax, including iterating over a sequence, specifying a start/end range, and using a step value. It also covers break and continue statements within for loops, standard data types like numbers, strings, lists, tuples and dictionaries, mutable and immutable data types, and functions for working with data types like type(), id(), and is.

Uploaded by

shanketheesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

2019 Batch

jfty; njhlu;ghly; njhopy;Dl;gtpay;

A/L Information and Communication Technology


Programming
2019 Batch
fzpdp nra;epuyhf;f nkhop (Programming Language)
For Loop
,q;F toq;fg;gLk; tPr;rpDs; Fwpg;gpl;l mwpTWj;jyhdJ kPz;Lk; kPz;Lk; nraw;gLj;jg;gLk;

For loop MdJ gpd;tUk; tbtpy; mikAk;

for iterating_var in sequence:


statements(s)

For x in range (start, end+1): For x in range (end+1):

statements statements

For x in range (start, end+1, step):

statements

gpd;tUk; FwpKiwfis fUJf

For x in range(11): Output:

print(x)

For x in range (1, 11):


Output:
print(x, end =”,”)

for r in range (1, 13, 3):


Output:
print(r)

P.Bipunaath ICT 0757746217


2019 Batch

total=0 Output:

for num in range (1, 11):

total+=num

print(toatl)

for s in “Srilanka”:
Output:
print (s, end=” ”)

For letter in “Python-programming”:


Output:
If letter ==’-’

break

print (letter)

gpd;tUtdtw;wpw;Fupa nra;epuiy jUf


1. ckJ ngaiu 8 jlitfs; fhl;rpg;gLj;Jf
2. 1 njhlf;fk; 10 tiuahd ,yf;fq;fis $l;Ljy;
3. 1 njhlf;fk; 10 tiuahd ,ul;il ,yf;fq;fis $l;Ljy;
4. 1 njhlf;fk; 10 tiuahd xw;iw ,yf;fq;fis $l;Ljy;
5. 20 ,w;F cl;gl;l rJu va;fspd; $l;Lj;njhifia tUtpisthf jUtjw;Fupa nra;epuiy
jUf

Break and continue

Break

P.Bipunaath ICT 0757746217


2019 Batch

Example 01:

for letter in 'Python' Output:

if letter == 'h':

break

print 'Current Letter :', letter

Example 02 :

var = 10 Output:

while var > 0:

print 'Current variable value :', var

var = var -1

if var == 5:

break

Continue

Example 01:

for letter in 'Python':

if letter == 'h': Output:

continue

print 'Current Letter :', letter

P.Bipunaath ICT 0757746217


2019 Batch

Example 02:

var = 10 Output:

while var > 0:

print 'Current variable value :', var

var = var -1

if var == 5:

continue

epakj;juT tiffs; (Standard Data Type)


1. Number
2. String
3. List
4. Tuple
5. Dictionary

Number

,q;F vz; ngWkjpfs; tiuaiw nra;ag;gl;L Nrkpf;fg;gLfpd;wJ. Xu; vz;iz khwp (Variable)
xd;wpDs; ,Ltjw;F (Assign) x=256 vdf; Fwpg;gLjy; NghJkhdjhFk;

,q;F 4 tifahd vz; ngWkjpfs; tiuaiw nra;ag;gLfpd;wJ.

1. Int (rhjhuz KO vz;fs;)


2. Long (ePz;l KO vz;fs;)
3. Float (jrk vz;fs;)
4. Complex (rpf;fy; vz;fs;)

Strings

trdq;fs; kw;Wk; nrhw;fis (string) khwp (variable) xd;wpDs; ,Ltjw;F mit “ ” ,w;Fs; ,lg;gly;
Ntz;Lk;.
word1="Good"

word2="Morning"

word3="to you too!"

print word1,word2

sentence=word1+" "+word2+" "+word3

print(sentence)

P.Bipunaath ICT 0757746217


2019 Batch

Lists

juTfs; %yfq;fis xU njhFjpahfNrkpj;Jitg;gjw;F List gad;gLj;jg;gLk;

,q;F ntt;NtW juT tiffis nfhz;l %yfq;fis Nrkpf;fyhk;.

List ,Ds; cs;s %yfq;fspy; khw;wq;fis nra;aKbAk;

Example:

S= *2, 4, “hello”+

R= [256, 72.5, ‘maths’, “marks”+

Nested List

Output:
u=[125, 256, 'world']
([125, 256, 'world'], [56, 25, 20, 'Hello'])
v=u,[56, 25, 20, "Hello"]

print(v)

1. Njitahd ,lq;fspy; Njitahd %yq;fis vLj;jy;


a= [2, 4, 6, 8, 10]
a [0]=2 a [-5]
a [1]=4 a [-4]
a [2]=6 a [-3]
a [3]=8 a [-2]
a [4]=10 a [-1]

Example:
Output:
q= ['p', 'r', 18, 30, 12, 15]

print (q[1]*3)

Output:
m=['A',5,18,30,12,15]

print (m[3]*3)

Output:
q= ['p', 'r', 18, 30, 12, 15]

print (q*3)

P.Bipunaath ICT 0757746217


2019 Batch

Output:
m=['A',5,18,30,12,15]

print (m[1:3])

2. List ,dJ mstpid (Length) my;yJ List ,Ys;s %yq;fspd; vz;zpf;ifia ngwy;

m=['A',5,18,30,12,15] Output:

print (len(m))

3. List ,Ds; xU %yfj;ij ,Wjpapy; Nru;f;fKbAk;

m=['A',5,18,30,15]
Output:
m.append(12)

print (m)

4. List ,Ds; xU %yfj;ij Fwpg;gpl;l ,lj;jpy; Nru;j;jy;

m=['A',5,18,30,12,15] Output:
m.insert(2,13)

print (m)

5. List ,Ys;s %yfq;fis VWtupir (sort) nra;jy;


m=[7,5,18,30,15]
Output:
m.sort()

print (m)

6. List ,Ys;s %yq;fis reverse order ,y; khw;wy;


m=[7,5,18,30,15]
Output:
m.reverse()

print (m)

7. List ,Ds; cs;s %yfj;ij/%yfq;fis ePf;Fjy;

m=[7,5,18,30,15] Output:

del m[2]

print (m)

P.Bipunaath ICT 0757746217


2019 Batch

Output:
m=[7,5,18,30,15]

del m[:]

print (m)

m=[7,5,18,30,15] Output:

del m[1:3]

print (m)

8. List ,Ds; cs;s %yq;fis jdpj;jdpNa fhl;rpg;gLj;jy;

m=[7,5,18,30,15] Output:
for x in m:

print (x)

Tuples

 ,JTk; List ,dJ tifiar; rhu;e;jJ. ,q;Fk; ntt;NtW juT tiffisf; nfhz;l
%yq;fis Nru;f;f KbAk;
 ,jDs; khw;wq;fis Nkw;nfhs;s KbahJ
 ntw;W (empty) tuple MdJ ntWikahd milg;Gf;Fwpr; Nrhbfspdhy; cUthf;fg;gLfpd;wJ.

Months= (“January”, “February”, “March, “April”, “May”, “June”, “July”, “August”, “September”,
“October”, “November”, “December” )

 Tuple ,Ys;s %yq;fis print nra;tjw;F


Print (months)

Output:

 Tuple ,y; cs;s xU %yfj;ij (element) fhl;rpg;gLj;Jtjw;F


Print (months[0])

Output:

P.Bipunaath ICT 0757746217


2019 Batch

 Tuple ,y; vs;s Fwpg;gpl;l rpy %yq;fis fhl;rpg;gLj;Jtjw;F

Print (months[1:3]) Output:

Print (months[2:])

Output:

Dictionaries
 Phonebook ,y; ngaUk; njhiyNgrp ,yf;fKk; (phonebook) gjpe;J itj;jpUg;gJ Nghy;
Dictionary ,q;F gad;gLk;
 Xt;ntHU key xt;nthU ngWkhdq;fs; toq;fg;gLk;
D= {key1: value1, key2:value2, key3:value3}

Phone= {‘A’: ‘0757746217’, ‘B’: ‘0767746217’, ‘C’: 0757082148, ‘D’: ’0752388988’ }

1. Dictionary ,id fhl;rpg;gLj;Jtjw;F


print (phone)
Output:

2. Dictionary ,ypUe;J key I gad;gLj;jpvalue I fhl;rpg;gLj;Jtjw;F

print (phone[‘A’]) Output:

3. Dictionary ,w;Fs; Gjpjhf Xu; key, value it Nru;g;gjw;F


phone[‘E’]=’0757746216’

4. Dictionary ,w;Fs; cs;s Fwpj;j key xd;wpd; value tpy; khw;wk; nra;tjw;F

phone[‘A’]=’0757746214’

5. Dictionary ,w;Fs; cs;s Xu; key and value it ePf;Ftjw;F


Del phone[‘A’]

6. Dictionary ,w;Fs; cs;s rfy key and value fisAk; ePf;Ftjw;F

phone.clear()

7. Dictionary ia ePf;Ftjw;F
del phone

P.Bipunaath ICT 0757746217


2019 Batch

juT tiffs; (Data Types)


juT tiffs; (Data Types) ,U tifg;gLk;

1. Mutable Date type


nra;epuy; xd;iw nraw;gLjjjg;gLj;Jk; NghJ ,ilapy; khw;wq;fis Nkw;nfhs;sf; $ba juT
tiffs; khwf;$ba juT tifiar; (Mutable Data type) rhu;e;jdthFk;
Example:
List, Dictionary, class

2. Immutable Data type


nra;epuy; xd;iw nraw;gLjjjg;gLj;Jk; NghJ ,ilapy; khw;wq;fis Nkw;nfhs;sf; Kbahj
juT tiffs; khwf;$ba juT tifiar; (Immutable Data type) rhu;e;jdthFk;.
Example:
Integer, String, tuple

Iterable Object
igj;jd; nkhopapy; gy jlitfs; rpy cUg;gbfis kl;;Lk; jpUk;gg; ngWtjw;Fg; gad;gLgit
iterable object vdg;gLk;

Example:

List, Dictionary, Tuples

Note:

Type (), id (), is

 Fwpg;gpl;l khwpfspd; juT tifia ,dq;fhz;gjw;F type() vd;Dk; nra;if gad;gLj;jg;gLk;


 Fwpg;gpl;l juT tiff;Fupa milahsj;ij mwpe;J nfhs;tjw;F id() gad;gLj;jg;gLfpd;wJ
 ,uz;L khwpfspw;F toq;fg;gl;Ls;s juTfs; rkdhf cs;sikia mwptjw;F is vDk;
%yr;nrhy; gad;gLj;jg;gLk;

x= “python”
Output:
y= “python”

x is y

a= 20
Output:
b= 30

a is b

P.Bipunaath ICT 0757746217


2019 Batch

Output:
X= 2.7
<class ‘float’>
type(x)

type([2,5,6]) Output:

type([2,5,6]) Output:

Id(44) Output:

Exercise:

1. 1 ,ypUe;J 100 tiuthd vz;fis ntspaPlhf ngWtjw;fhd igj;jd; FwpKiwia (Python


Code) vOJf.

2. 100 ,Yk; Fiwthd xw;iw vz;fis ntspaPlhf ngWtjw;fhd igj;jd; FwpKiwia (Python
Code) vOJf.

3. 100 ,Yk; Fiwthd ,ul;il vz;fis ntspaPlhf ngWtjw;fhd igj;jd; FwpKiwia (Python
Code) vOJf.

4. 100 ,Yk; Fiwthd rJu vz;fis ntspaPlhf ngWtjw;fhd igj;jd; FwpKiwia (Python
Code) vOJf.

5. 100 ,Yk; Fiwthd Kf;Nfhz vz;fis ntspaPlhf ngWtjw;fhd igj;jd; FwpKiwia


(Python Code) vOJf.

6. ahjhapDk; Xu; ,yf;fj;jpid cs;sPL nra;Ak; NghJ mt;tpyf;fj;jpd; kWjiy ntspaPlhf


tUtjw;fhd igj;jd; FwpKiwia (Python Code) vOJf.

7. cau;ju tFg;;nghd;wpy; ICT ghlj;jpy; 30 khztu;fs; ngw;Wf;nfhz;l Gs;spfspd; ruhrup> cau;T>


,opT Mfpatw;iw fhl;rpg;gLj;Jtjw;fhd igj;jd; FwpKiwia (Python Code) vOJf.

P.Bipunaath ICT 0757746217

You might also like