GROUP 2 Presentation
GROUP 2 Presentation
in Python
Array and its operation, Handling Strings
and Characters, List: slicing, bound,
cloning, nested list, list and methods
by Group 2 (75-78)
Introduction: TOPICS
ARRAY
Definition:
Syntax:
An array is a collection of
elements of the same type.The ar = [2, 5.5, "Hai"]
idea is to store multiple items of print (ar)
the same type together.
Types of Arrays:
There are four collection data types in the Python programming language:
List is a collection which is ordered Tuple is a collection which is Set is a collection which is Dictionary is a collection which is
and changeable. Allows duplicate ordered and unchangeable. Allows unordered, unchangeable, and ordered and changeable. No
members. duplicate members. unindexed. No duplicate members. duplicate members.le. Allows
duplicate members.
Indexing:
Python allows you to store an enumerated set of items in one place and access an item by its
position – index. Let’s take a simple example: colors = ['red', 'green', 'blue', 'yellow', 'white', 'black']
Operations: 2. Add:
Methods for different operations are subjective to the type
of array you are working on (list/set/dictionary/tuple). You
can use the append() method to add an element to an
array/list.
colors.append(‘purple’)
print(colors)
1. Access: Output:['red', 'green', 'blue', 'yellow', 'white', 'black', ’purple]
You refer to an array
element by referring to 3. Remove:
the index number. You can use the pop() method to remove an element from
x = colors[0] the array.
colors.pop(6)
print(x) You can also use the remove() method to remove an element
Output: red from the array.
colors.remove(‘purple’)
print(colors)
Output:['red', 'green', 'blue', 'yellow', 'white', 'black']
5. Loops in arrays:
You can use the for in loop to
4.Length:
loop through all the elements of
The len() method is used
an array.
to return the length of
for x in cars:
an array (the number of
print(x)
elements in an array).
Output: red
x = len(colors)
green
print(x)
blue
Output:5
yellow
white
black
Handling Strings
and Characters
Definition:
String is a sequence of characters. It is a Syntax:
collection of characters that can include message = "I love Python."
letters, numbers, symbols, and spaces .A print(message)
character is a single symbol or unit of text,
such as a letter, digit, or punctuation mark.
Indexing:
greet = 'HELLO WORLD'
Access:
We can access the characters in a string by using Length:
index values: we use the len() method to find
print(greet[1]) the length of a string or number
Output: E of characters
Or a range of characters in a string by using the print(len(greet))
slicing operator colon : .For example, Output: 10
print(greet[1:4])
Output: ELL
Membership: Multiline strings:
membership operators are used to test You can assign a multiline string to a variable by using
whether a value is present in a string.we can three quotes or three single quotes :
use the keyword in or not in message = """Never gonna give you up
For ex: Never gonna let you down"""
print('a' in 'program') print(message)
print('at' not in 'battle') Output:Never gonna give you up
Output: True Never gonna let you down
False
String formatting:
str.format() and f-strings methods are used to add formatted objects to printed string statements. The
string format() method formats the given string. It allows for multiple substitutions and value formatting
String = 'Students'
print ("Hello {}, How are you ?".format(String))
Output: Hello Students, How are you ?
Lists and
methods
Definition:
Lists are used to store multiple items in Syntax:
a single variable within square ages = [19, 26, 29]
brackets. It’s ordered and changeable print(ages)
and allows duplicate members.
Cloning: Slicing:
In Python, it is possible to access a
We can use the built-in List method copy() to copy a list
section of items from the list using the
newlist = my_list.copy()
slicing operator : .For example,
print(newlist)
my_list = ['p', 'r', 'o', 'g', 'r', 'a', 'm']
or use the built-in method list()
print(my_list[2:5])
newlist = list(my_list)
print(my_list[5:])
print(newlist)
print(my_list[:])
Output:['p', 'r', 'o', 'g', 'r', 'a', 'm']
Output:['o', 'g', 'r']
['a', 'm']
Bound:
['p', 'r', 'o', 'g', 'r', 'a', 'm']
In Python, a list bound refers to the maximum index that can be used to access an element in a list.
In other words, it’s the highest valid index value that corresponds to a specific element in the list.
List bound is length -1.
my_list = [1, 2, 3, 4, 5]
#list_bound = 4
Nested list: List Methods:
A nested list in Python is a list that Method Description
contains other lists as elements: append() Adds an element at the end of the list
my_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] clear() Removes all the elements from the list
print(my_list[0][1]) copy() Returns a copy of the list
print(my_list[1][2]) count() Returns the number of elements with the specified value
Output:2 extend()
Add the elements of a list (or any iterable), to the end of the
current list
6
index() Returns the index of the first element with the specified value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the first item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list
Group Members Adyasha Patnaik
2302030077
Priyanka Kishan
2302030075