0% found this document useful (0 votes)
12 views13 pages

GROUP 2 Presentation

Python Arrays,lists and sets
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)
12 views13 pages

GROUP 2 Presentation

Python Arrays,lists and sets
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/ 13

Programming

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 Slide 3


We already know that Python is an Indexing, Types Slide 4
Operations:
interpreted, object-oriented, high- Access, Add, Remove Slide 5
level programming language with Length, Slicing, Looping Slide 6

dynamic semantics. It is a computer


STRINGS AND CHARACTERS
language that can be used to build
Definition and syntax Slide 7
software and websites, automate Indexing, Accessing Slide 8
tasks, and analyze data. It's a Slicing, Length Slide 8
Membership, Multiline, Slide 9
general-purpose language, meaning String Formatting
it can be used to create a variety of
programs. Today, through these LISTS
Definitions, Syntax Slide 10
slides, we will learn more about the Slicing, Bound, Nested list Slide 11
following topics shown beside: List and methods Slide 12
Array and its
operation

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

Manish Minz Adarsh Kanhar


Group 2
2302030076 2302030078

Priyanka Kishan
2302030075

You might also like