0% found this document useful (0 votes)
21 views9 pages

Print List Operations and Methods

Uploaded by

moonset.fah
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)
21 views9 pages

Print List Operations and Methods

Uploaded by

moonset.fah
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

31 index start o !!

[11 Oct 2004] eX1 eX2


(10 30] empty-list- ()
Operators
1
LIST age 10
:

1"A"]
,

new- list (1 2)
empty-list + +
age e
=
(15 ,
35
,
10] -

Concatenation Opr list Engineerin


:

(1) print (new-list)


:
1. . 17 2 ages agent agee
print cages)
: List 1 + list (12 A)
Max 110 ,
20
,
30 15 35 40)
, , ,
,

eX1 eX2

his
(1 2 3)

Repetition Opr
num1
=
, n = 10

listons
,

2 .
(41 :
n nume" num1 ** zeros = 10]
*
n

print(num) print(zeros

: list n [ 1 2
, ,
% ,
7 2 3
, , ,
1 4
, ,
%] 10 ,
0 0 0 0 0 0 0 0
, , , , , , ,
0
,

eX1 eX2

(17 45]
Philist
IDS =
40 %77
IDS [11 93]
(Checking Opr) meanor in Var
20
, ,
777
: ,

3 in : , , ,

user ID -
:
int (input ('Enter w's
User ID1 :
20
if
-

user-ID in JDS :

varis an en
IDr

syntax
=
77
print c'Yes'
user .

print /user.1p9 in IDs) True else


/
:

if else print (user JD2 -


Y9

in IDS) flase print ('Ne's


um

List Methods (function list) : list .


method-names eX1 exe (nong list Pargi win users

lis + 1 < 1 5 7 9]
n :
int input en : 's

append methods (imm netw monimieme)


=

, ,

list
,

list
1 item []
=

list1 Append (7)


for
.
.

i in rangens :

print clists
1101 Var gun des)
listi Appende is
list 1 Appende item (
.

Stax
:
(1 5 7 9
, ,
77] print clists
, ,

2 .
Insert methods (lon items indexin (11 ,
2
,
3, 41) Insert (150) .

list 1. Insert (index items (1 50, 2 9 4)


Max
:
, , ,
,

·ninazera
!

3 .
Remove methods Inwa item Woman listinvitin (111 , e 33 , ,
14 222) . remove cre
,

liste remove items ((17


Max
:
.
,
33 14
,
223)
,

* error ; is item inte list -


check now

& Sort
.
methods (long min-max) (17 ,
3 ,
0 2
,
,
51) .
Sort

syntax
: list sorts ( (2 3, 4 , 5, 7]
w ,
,

ex

built-in function del list/index Seq (17/ 33)


:

Delete :

del seg1[I]
no item any index
print (segi) [11 ,]
del sege [i]

printssegt) (17)
Useful built-in function * Flem : En

1. sumclists =
Minniznessiglist e .

g .
sum( (1 ,
2 3
, ,
12) 10

. max clists :

Mansonialist e .

g .
max (13 7 2 , , ,
11) 7

· . minclists =
Mahainatalist e .

g min (13 7
.

, ,
2
,
11)

LIST Referencing liste Listi in indextrons Tita copy iswan !


:
copy
: :

1 .

Copy by append : e .

Copy by +

list 1 = <7 2 B 4]
, , ,
list 1 = [1 2 3 4)
,, ,

lista-1] liste = <1 + list

for item in list 1 :

&
list 2 append c item

&D List : List now list (neetwlist inte Int) : : list <it (j)
ex list : ((1 ,
2
,
19 ,
12)

print(listcos) (1 2)
,

print clisticosis e

print clisticislos) a

List comprehension nonse ne listing whateverewia !


PosteInt
:
:
&
&
Syntax : listi :
(expression for item in Iterable) ; expression sinremitie list Parsi
:

item :
Bakinwania iterable
interable -
list2 (inmoni
UC

118 Oct .
2024] Destinen

function-function namec,enent nemesialrun norant output


#121innigan donzini is
# 1 ninige

Creat our own function mainten mentgen sign

Syntax
nu
: def function- name (optional parameters) :

Statement

Statement

exc :
functionmin printe
2 function Tatian
def print. name
.

is :

3 .
mings enginee
: Prnfunction print "Alice" o Not output

Print name o Alice

det -name (Varisonmen


infunction
one
parameter (wim)
def greeting /names :

print : "Hello" name ,


name : Alice

greeting ("Alice") :Hello Alice

Multiple parameters (i #functionn penin on


mmsal) a
parameters parameters -

def say hellos names ,


namee name) :
,

print :"Hello" names) positional


,
names
,
namee
argument
hellos !"A" "B" "c"Hello ABC
say, , ,

Keyword (ironi function in specify Go variable in argument


arguments e

say- hellos (names


:
'Alice' ,
names :
'Bob' ,
names :
'Chris's Aesnis errors position

Default value (seta default Ti ; user lgin x parameter ,


user por initis
def say hellos (name i :
'Defaults' ,
namee :
'Defaulte'

print("Hello, names
,
namee

Hello Defaulty Default


say- helloses
+ 2

say-hellose''Alice' ,
namee - Bob's Hello Alice Bob
·

I minion ta function]

ex def calex ye local wa (n function box sonwomen


,
:
variable return

x + variable In faction incoonra must


2
y
=

printies caritoneonata Ariblingit's micagisa/output :

calce ,
b) by Emenia

def gen-charc :

verwin
"Hello)
output gen-chase ·

print c outputs Hello

ex : mis function minieres list def meanest :

Twen list Mitwesine take in return sumcst/lenest)


-
nemininitiatio returns mean (1
,
e 3
, ,
4) v R 5 .

multiple return value enismnizzimmsan

·
return values Valuez Values
, ,

·
antia 1 .
mineisiksin nat ex def multiples :

2 .
Nig , asiksin an In Tuple return 1 2 3
, ,

X
,y

print
,
2

(x,
:

y
multipless

,
2) 0123
you

&
outputs : muliples
(b)
print (output says
of

print (output (13) &e

print (output sejs o

input ~ function return


·
Output
127874min) (local Variable
122 Oct 2024) return mosmingiensin !
main function (frien function nearity def main (

: Manis
def print-welcome) :
(1) (2)
print, 'Welcome ' ..

print :'This is ..
'I

def cal arg -


:

X =
int(input(s)

y
=
int (input()
return (xtysle
def main es :

print- Welcome
cal
ang argis
=
-

print of 'This arg is


Larg : ef3's
main()

Global (orwanwennies function (e) a


global variable name

* Wh function Tenenniest im interwid


Ente global Erministers
&
#Named Constant =
Global Constant ·

Missis


function inolenitenes
Library
importnew importlibrarynaesion
(see

names

1) random import random


1 .
1) random. randing (start , end ongmanvan start is end

1 .
11 random , seedlines a ser mini ninnclimsien
es math (numwim &miner's import math
function
2 1) math sin(x) math re math sareesox ; X is radian 10-24)
.
.
.

expex) .

math cos(x) . math .


log(x)
math ,
tan (x) math .
pow (base ,
p)-base
constant 2 41.

2 .
2) math. e
3 1415
.

math .
pi
3) I def returne :

return 1
,
2,

1) X, y , 2 "
1 2 3
, ,
01
(1 NOV 2024] Infunction same list unis append/insert 2) tup =
1, 2
,
3 0 (1, 2
,
3)

luple-in list ,Now billon return amomo tup-Ivare, tup = I vari


,
vare
,
vary

tuple - lista list (tuples ↓


String list

stringSequencii a

print (my-string (03) - A

print( my-strings-1) -D EX : count

target-'T'
a
target chars
:

& my-string
= 'Hello There'

for "Juliet' 5 "Juliet'


loop
" count = 0
name name :

! for char in
my- string
:

for char in for range dencnames


name : i in if char--target
C
count++ 1

print chars t
print (name (i)
print (counts -4

name str
2 O end 1
Slicing-winstring + Str/start :
stop :
steps aie
rangecisions tops & list index

# Miss single- An string Fursi ex :


str : de
1) print (str(1 : p : 15 tell

Brn list tuple in


# a e print (str(1 : 15) - ell

(strc 12) Hell


print
o
3) :

10
print 3) -
(str(3 :

5) print (Stuc :: 2) :Ho


e
print (Str( :: -13) =
offel creveses

a nina : In one not (1008 output

String Comparison - Interior string (s , ) output : True/false *error an Iba

aminer iAs

String testing methods = checkstring output-true/false -


String .
methods-name

1 is alnums-oiconais or wiring

2 is alphas-oicons
is
digitis =
wiring

& .
is lowers =
mension

5 .
is upper-risingstan ! * check me string slicing I

6 is spaces =esins ,
Esseningi
zig19
S
Creating new
string methods (return a new string
& ex :
my-str
'Abc
=

lowers-return string invisionsonanza


D
new
new-ser-my-str lower
-

.
.

uppers) -
return new
string invision stonege print ( new. str) o abe

Strip family methods


-

.
~strips) =

Nissan (a) & We ne


-

. Istripes = ngnin (a) a not one


9

. stripes :
nwng nins
-

.
Stripc'char's :
an snasmin s e zane

find (substring)
·
=
m index was
substring (index of char) ,
miss output--

ex :
Str =
Heltette'

new str :
Str .
replace C'e 'O'
,

replace (to be replace',new-string's Imm A B (Nazis Hottolto


Man
:
· ,

print (new-strs

Split)) Split Brin list min by mine split splitter


=
·
I .

"

ex :
my-str
=
"ABCD ef of

Ist-my-strsplit ? "
"

printests o "AB" "CD" "Ef" "ON"


, , ,

infor list
string operators
*: "ab" ** - "ababab"

+: "ab"t'ed" w "abea"

check in'abc's True


in :
substring in
string a

" ,
,"
-

3 C x
&

folder

inmonistringAnythin
-
work ns a
dict-werowing in key
&

value
w

Values 3 list entiis de


syntax (keyl Keyb
:

: Values Keye : Values : - in


, ,

dict a Penisimia Keyshade


& my-dict = 1 'A' : e
,
'B' : 2 'c' : g)

Accessing Value
,

dict <key
syntax
:
print (my-dict (A1) -

print (my-dict(c)s -a

print (mydicts o
I'd' : 1
,
'B' :
2
,
'c'ib}

key-Gainin string/tuple/in/ float immutable

value
anything mydict-["Anna" ,"Bob" "Hello" "Mary" 1 3) I
11 -
31 :
,
2 : :
2
,
, ,
,

Updatingedict intralizizata ex mydict-["Anna" 11 31 ,"Bob" "Hello" "Mary" : :


,
2
,
:
,
I
print
syntax
: dic + (key] :
new- value
my-dict("Annas-10010
(" Bob") (4 3]
my-dic
=
+ 2
,
,

in operator chekkey in dict print (my-dicts - [Anna : no " Bob"


,
:
112 3)
,
, "Mary" : 33
input (Enter key: :
"Alice" my-dict true
-

in query
if query in dict :

"fah" False
my-dictin print (dic +
-
(query

len function sizion items Thi dict dict-[ "Alice" : "Bob"


↓ can't access key is dict princ (dict <"Mansion error !!

Creating an
empty
dict Can be
#
ingate empty dict
se dict l neimsien Fritz

=
wite dict elists
my-dict-[} empty dict e EFati
on
student : : 10
,
names : I'Fahl ,
'Oue']

my-dict"Alice's 1 'due' 153


:

scores : I'10 115] :


,

student 23
my-dict" Bob "
=
=
2
for i in
rangeden names :

print cmydicts -Alice" : 1


,
"Bob" 2] :
student [name(is) =
score (i)

for key
dicplooping loop makey ! in dict

ex mydict
=
(Alice' 'Beb' : ,
n
, 'Mary : key 'Alice' :

,
Value -
1

for key 'Bob' Value


in
my-dict :
key :

,
:
2

print of 'Key =
&key 3 ,
Value :
[my-dict(keys' Fey 'Mary' :

,
Value :

methods syntax : dict methodes


.

keycs :
reture all key in list !I d. keyses

·
Value is :
reture all Value 18 list ! Id .
values

ottem : reture all pairs (key ,


Values In list was tuple/d items
~
get s Value Pr key ,ki key if dict a return default

syntax : key Defaults ex my dict E "Alice" : A "Bob" : 3


get
:

, ,

my-dect get "Alice" "Not found" A-

Counting by dic my-dect getc"fah" "Not found's Not found


-

idea Ta key 1 vegilain: main T value Beswinwesvein wel


:

e fruits
:
L'Apple' 'Apple' , Banana' , Banana' , Orange' Banana']
, , e his in Brows ten :

fruit-dict =
93 loop 1 :
Apple (Not in Fruit dict +1 sentences : "This is a cat meow meow meow cat "

for fruit in fruits :


loope Apple: +1 +
> worlds : sentences Split
it fruit fruit dict
1Apple
in : worlds. bict : 23

fruit dice fruit +-


: e
,
'Banana' : 3
,
'Orange' 13 :
for world in worlds :

else wit dict(trintfruit worlds-dict (worlds -


world dict (world 0 +
.

get ,

- n e
print (world-dicts
Nested dic
, gender' ''grade :Al 'b' :Age
Value Value

user : 'Rice' E'Age' : : es :


, ,
:
so , gender' M,grade :B'Y} :

print user sAlice's o


['Age' , gender' '', 'grade : I
: es :

print (user /'Alice's 'Age's) wes

can be anything list Istr/floar/tup


&
Dict of list score :L'Alice' : (10 ,
20
,
301
,
'Anna : (15 ,
20 101 3
,

print is core <Alice's (13) - 20


score (Alice's Appended

print score <Alice's - (10 ,


20
,
30, 50)

List of dict
Isi : / l'Alice' 13 :
,
EBob' 15 :
,
[Anna' :
3/

You might also like