Python basics notes .2 new
Python basics notes .2 new
strings
Strings in python are stored as individual character in continuous location with
two way indexing for each location.
INDEXING
Backward indexing -6 -5 -4 -3 -2 -1
S= H E L L O !
Forward indexing 0 1 2 3 4 5
>>>L= ”PYTHON”
>>>L[0]
>>>P
>>>L[-2]
>>>O
Traversing a string :
Traversing refers to iterating through a element of a string one
character at a time .
Code=”powerful”
for c in Code :
print (c,”~”end=””)
o/p:
p~o~w~e~r~f~u~l~
String Operators
Concatenation (+) Replication (*) Membership (in,not in)
>>>s1=”power” >>>3*”hi” >>>S=”hello”
>>>s2=”ful” >>>”hihihi” >>>“o” in S
>>>s1+s2 >>>True
>>>”powerful”
String slicing
• String slice refers to part of a string containing some continues
characters from the string.
• For index n,s[:n]+s[n:] will give you the original string s
• String [::-1] is an easy way to reverse the string
string functions
Unpacking a string
>>>s=”string”
>>>x1,x2,x3,x4,x5,x6=s
>>>lst=list(s)
>>>tup=tuple(s)
>>>s
“string”
>>>x1,x2,x3,x4,x5,x6
(“s”,”t”,”r”,”i”,”n”,”g”)
>>>lst
[“s”,”t”,”r”,”I”,”n”,”g”]
>>>tup
(“s”,”t”,”r”,”i”,”n”,”g”)
Lists
like strings, lists are a sequence of values. A list is a data type that can
be used to store any type and number of variables and information.
A list in python is formed by enclosing the values inside square
brackets []. Unlike strings , lists are mutable data type.
List slicing
List[start:stop:step]
l=
-9 -8 -7 -6 -5 -4 -3 -2 -1
100 200 300 400 500 600 700 800 900
0 1 2 3 4 5 6 7 8
>>>l[5:]
[600,700,800,900]
>>>l[-9:-5]
[100,200,300,400]
>>>l[::-1]
[900,800,700,600,500,400,300,200,100]
Creating a nested list
L=[1,2,3,”abc”,[“c”,”s”,”c”],(2,3,6)]
>>>l[2:3]
[3]
>>>l[4][1]
“s”
Built in list functions and methods
Function Description Example
len(list) Returns the total L=[1,2,34,5]
length of the list len(L)
4
max (list) Returns the item max(L)
with maximum value 34
in the list
min(list) Returns the item min(L)
minimum value in 1
the list
list(seq) Converts a sequence A=1,2,3,4,”dj”
into list list(A)
[1,2,3,4,”dj”]
sum(list) Sums up all the sum(L)
numeric values 42
present in the list
Method Example
append(item) >>>Lst=[1,2,3]
>>>print(Lst.append(4))
[1,2,3,4]
extend(item) >>>a=[12,23,34]
>>>Lst.extend(a)
>>>print(Lst)
>>>[1,2,3,4,12,23,34]
index (item) >>>print(Lst.index(1))
>>>0
insert(index,item) >>>Lst.insert(3,4)
>>>print(Lst)
>>>[1,2,3,4]
sort() >>>print(Lst.sort())
>>>[4,3,2,1]
remove(item) >>>l=[1,11,2,22,1,4]
>>>print(l.remove(1))
>>>l
>>>[11,2,22,1,4]
reverse() >>>print(l.reverse())
>>>[1,2,4,11,22]
count(element) >>>=k[1,2,3,4,1,2,3,4,1,1]
>>>print(k.count(1))
>>>4
Tuple
The tuple is a sequence of immutable Python objects.
Iterating through a tuple
Elements of tuple can be accessed sequentially using loop.
Example:
Traversing in Tuple:
Tup=(5,11,22)
for i in range (0,len(Tup)):
print(Tup[i])
o/p:
5
11
22
Tuple operation Explanation Example
Creating a Tuple New tuple can be T=()
created by enclosing T=(3,3,’a’,[1,2])
in simple T=(5,)
parenthesis
Accessing tuple each item is stored t= (1,2,’p’,’*’,50.5)
elements individually and print(t[2])
accessed through o/p: p
indexing
Slicing in a tuple slicing can be done t= (1,2,’p’,’*’,50.5)
using : (colon) print(t[1:2])
operator. Slicing o/p: 2
uses indexing, either
positive or negative.
Concatenating 2 joining the tuple t= (1,2,’p’,’*’,50.5)
tuples elements with t1=(3,)
another tuple using t2=t+t1
+ operator print(t2)
o/p:
(1,2,’p’,’*’,50.5,3)
Repeating elements repeat the elements t=(‘Good’)*2
of a tuple of the tuple by a print(t)
specified number of o/p: GoodGood
times. They are not
copied but are
referenced multiple
times.
Using membership these operators are t= (1,2,’p’,’*’,50.5)
operators used to check 1 in t
whether a value o/p: True
exists in the tuple or t= (1,2,’p’,’*’,50.5)
not. ‘z’ not in t
o/p: True
Traversing a tuple
using loop
Dictionary:
Dictionary is a data type in python. It is an unordered collection of
items (order may be changed after defining). Each item has a key :
value pair.
Dictionary is a mutable data type.(the values can be added,
removed, and changed at any time).
*Note: The keys in the dictionaries are immutable.
D={1:’a’,2:’e’}