Unit 3 Indexing Slicing and Data Structure
Unit 3 Indexing Slicing and Data Structure
1. By using index:
Python supports both +ve and -ve index.
In [2]: s="Upendra"
print(s[0])
print(s[3])
print(s[6])
print(s[-3])
print(s[-1])
U
n
a
d
a
In [3]: s="Upendra"
print(s[15]) # out of range because string is not that long
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In[3], line 2
1 s="Upendra"
----> 2 print(s[15])
Syntax:
string_Name [beginindex:endindex:step]
Here,
beginindex: From where we have to consider slice(substring)
endindex: We have to terminate the slice(substring) at endindex-1
step: incremented / decremented value ### Note :
Slicing operator returns the sub string form beginindex to endindex - 1 ### Note:
If we are not specifying begin index then it will consider from beginning of the string.
If we are not specifying end index then it will consider up to end of the string.
The default value for step is 1
In [9]: s = 'abcdefghijk'
print(s[2:7])
cdefg
In [8]: s = 'abcdefghijk'
print(s[:7])
abcdefg
In [10]: s = 'abcdefghijk'
print(s[2:])
cdefghijk
In [11]: s = 'abcdefghijk'
print(s[:])
abcdefghijk
In [12]: s = 'abcdefghijk'
print(s[2:7:1])
cdefg
In [13]: s = 'abcdefghijk'
print(s[2:7:2])
ceg
In [14]: s = 'abcdefghijk'
print(s[2:7:3])
cf
In [15]: s = 'abcdefghijk'
print(s[::1])
abcdefghijk
s = 'abcdefghijk'
In [16]: print(s[::2])
acegik
In [17]: s = 'abcdefghijk'
print(s[::3])
adgj
'earnin'
Out[18]:
'earnin'
Out[19]:
'eri'
Out[20]:
'Learnin'
Out[21]:
Forward direction
L e a r n i n g P y t h o n i s v e r y e a s y ! ! !
Backward direction
! ! ! y s a e y r e v s i n o h t y P g n i n r a e L
Syntax:
s.replace(oldstring,newstring)
Here, inside 's', every occurrence of oldstring will be replaced with newstring.
Splitting of Strings:
We can split the given string according to specified seperator by using
split() method.
vijay
dinanath
chauhan
In [6]: s="22-02-2018"
l=s.split('-')
for x in l:
print(x)
22
02
2018
In [7]: s="22-02-2018"
l=s.split() # no space in the string , so output is same as the given string
for x in l:
print(x)
22-02-2018
Data Structure
A data structure is a group of data elements that are put together under
one name. Data structure defines a particular way of storing and
organizing data in a computer so that it can be used efficiently.
In [ ]:
List Comprehensions
It is very easy and compact way of creating list objects from any iterable objects(like
list,tuple,dictionary,range etc) based on some condition.
Syntax:
list=[expression for item in list if condition]
In [9]: l1=[]
for x in range(1,11):
l1.append(x*x)
print(l1)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
In the above case, the program consisting 4 lines of code. Now for the same purpose
we will write the following code in more concised way.
print(l1)
[2, 4, 6, 8, 10]
[1, 3, 5, 7, 9]
In [15]: words=["Upendra","Jayesh","Rahul","Ajay","Puja"]
l=[w[0] for w in words]
print(l)
In [16]: words=["Upendra","Jayesh","Rahul","Ajay","Puja"]
l=[w for w in words if len(w)>5]
print(l)
['Upendra', 'Jayesh']
In [17]: num1=[10,20,30,40]
num2=[30,40,50,60]
num3=[ i for i in num1 if i not in num2]
print(num3)
[10, 20]
In [18]: words="the quick brown fox jumps over the lazy dog".split()
print(words)
l=[[w.upper(),len(w)] for w in words]
print(l)
Set Comprehension
Syntax:
s = {expression for x in sequence condition}
In [ ]:
Parameters:
- mass: Mass of the object (kg).
- angle: Inclination angle of the plane (degrees).
- friction_coefficient: Coefficient of friction between the object and the plane.
Returns:
- normal_force: Normal force exerted by the plane on the object (N).
- friction_force: Friction force opposing motion (N).
- acceleration: Acceleration of the object along the incline (m/s^2).
"""
# Constants
g = 9.8 # Acceleration due to gravity (m/s^2)
# Example usage:
mass = 5.0 # kg
angle_of_inclination = 30.0 # degrees
friction_coefficient = 0.3
In [ ]:
In [ ]:
Assignement Questions
please use assignment submission template to submit
assignment
1. Write a program that defines a list of countries that are a member of BRICS, Check whether a country is
a member of BRICS or not.
2. Write a program that has a list of numbers (both positive as well as negative). make a new tuple that
has only positive values from this list.
3. Write a program that accepts different number of arguments and return sum of only the positive values
passed to it.
4. Write a program that creates two sets-squares and cubes in range 1-10. Demonstrate the sue of
update(),pop(),remove(),and clear() functions.
5. Write a program that creates a dictonary of radius of circle and its circumference.
6. Write a unique program considering the concepts learnt upto this unit.
In [ ]: