COMPUTER PYTHON REPORT

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

Lists

 Lists are used to store multiple items in a single variable.


 Lists are created using square brackets.

Create a List:
thislist = ["apple", "banana", "cherry"]
print (thislist)
 The list is changeable, meaning that we can change, add, and
remove items in a list after it has been created.
 Since lists are indexed, lists can have items with the same value

thislist = ["apple", "banana", "cherry", "apple", "cherry"]


print(thislist)
Range of indexes

Range of Indexes
 You can specify a range of
indexes by specifying
where to start and where
to end the range.
 When specifying a range,
the return value will be a
new list with the specified
items.
Check if Item Exists
To determine if a specified item is present in a list use the in keyword
Change Item Value
To change the value of a specific item, refer to the index number:
Change a Range of Item Values
To change the value of items within a specific range, define a list with the new values, and refer to
the range of index numbers where you want to insert the new values:
Insert Items
To insert a new list item, without replacing any of the existing values, we can use the insert() method.
The insert() method inserts an item at the specified index:
Append Items
To add an item to the end of the list, use
the append() method:
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)

Extend List
To append elements from another list to the current list, use the extend() method.
thislist = ["apple", "banana", "cherry"]
tropical = ["mango", "pineapple", "papaya"]
thislist.extend(tropical)
print(thislist)

Add Any Iterable


The extend() method does not have to append lists, you can add any iterable object (tuples, sets,
dictionaries etc.).
thislist = ["apple", "banana", "cherry"]
thistuple = ("kiwi", "orange")
thislist.extend(thistuple)
print(thislist)
Remove Specified Item
The remove() method removes the specified item.
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)

Loop Through a List


You can loop through the list items by using a for loop:
thislist = ["apple", "banana", "cherry"]
for x in thislist:
print(x)

Loop Through the Index Numbers


You can also loop through the list items by referring to their index number.
Use the range() and len() functions to create a suitable iterable.
thislist = ["apple", "banana", "cherry"]
for i in range(len(thislist)):
print(thislist[i])
Tuple
 Tuples are used to store multiple items in a single variable.
 Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3
are List, Set, and Dictionary, all with different qualities and usage.
 A tuple is a collection which is ordered and unchangeable.
 Tuples are written with round brackets.

mytuple = ("apple", "banana", "cherry")

Change Tuple Values


 Once a tuple is created, you cannot change its values. Tuples are unchangeable,
or immutable as it also is called.
 But there is a workaround. You can convert the tuple into a list, change the list, and
convert the list back into a tuple.
x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)

print(x)
Unpacking a Tuple
When we create a tuple, we normally assign values to it. This is
called "packing" a tuple:

Packing a tuple:
fruits = ("apple", "banana", "cherry")

Unpacking a tuple
fruits = ("apple", "banana", "cherry")

(green, yellow, red) = fruits

print(green)
print(yellow)
fruits =
print(red)
("apple", "banana", "cherry", "strawberry", "raspber
Using Asterisk* ry")
If the number of variables is less than the
number of values, you can add an * to the (green, yellow, *red) = fruits
variable name and the values will be
assigned to the variable as a list: print(green)
print(yellow)
print(red)
Loop Through a Tuple
You can loop through the tuple items by using a for loop.
thistuple = ("apple", "banana", "cherry")
for x in thistuple:
print(x)

Loop Through the Index Numbers


You can also loop through the tuple items by referring to their index number.
Use the range() and len() functions to create a suitable iterable.
thistuple = ("apple", "banana", "cherry")
for i in range(len(thistuple)):
print(thistuple[i])

Using a While Loop


 You can loop through the tuple items by using a while loop.
 Use the len() function to determine the length of the tuple, then
start at 0 and loop your way through the tuple items by referring
to their indexes.
 Remember to increase the index by 1 after each iteration. thistuple = ("apple", "banana", "cherry")
i = 0
while i < len(thistuple):
print(thistuple[i])
i = i + 1
Set
Sets are used to store multiple items in a single variable.
* Note: Set items are unchangeable, but you can remove items and add new items.
thisset = {"apple", "banana", "cherry"}
print(thisset)

 Sets cannot have two items with same value


 Duplicate values will be ignored

To determine the length of item in set, use len( )function


thisset = {"apple", "banana", "cherry"}

print(len(thisset))

To add one item in list use .add( )


thisset = {"apple", "banana", "cherry"}

thisset.add("orange")

print(thisset)
To add items from another set into the current set, use the update ( ) method
thisset = {"apple", "banana", "cherry"}
tropical = {"pineapple", "mango", "papaya"}

thisset.update(tropical)

print(thisset)

To remove an item in a set, use the remove ( ), or the discard ( ) method.


thisset = {"apple", "banana", "cherry"}

thisset.remove("banana")

print(thisset)

You can loop through the set items by using a for loop

thisset = {"apple", "banana", "cherry"}

for x in thisset:
print(x)
Dictionary
 Dictionaries are used to store data values in key:value pairs.
 A dictionary is a collection which is ordered*, changeable and do not allow
duplicates.
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)

Dictionaries cannot have two items with the same key


Duplicate values will overwrite existing values
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964,
"year": 2020
}
print(thisdict)
You can access the items of a dictionary by referring to its key name, inside square
brackets
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = thisdict["model"]

Get Keys
The keys() method will return a list of all the keys in the dictionary.
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}

x = car.keys()

print(x) #before the change

car["color"] = "white"

print(x) #after the change


Loop Through a Dictionary
You can loop through a dictionary by using a for loop.

for x in thisdict:
print(x)

for x in thisdict:
print(thisdict[x])
myfamily = {
"child1" : {
Nested "name" : "Emil",
Dictionaries "year" : 2004
},
A dictionary can "child2" : {
contain dictionaries, "name" : "Tobias",
this is called nested "year" : 2007
},
dictionaries. "child3" : {
"name" : "Linus",
"year" : 2011
}
}
Arrays
Arrays are used to store multiple values in one single variable:
cars = ["Ford", "Volvo", "BMW"]
The Length of an Array
Use the len() method to return the length of an array (the number of elements in an array).
x = len(cars)
Looping Array Elements
You can use the for in loop to loop through all the elements of an array.
for x in cars:
print(x)

Adding Array Elements


You can use the append() method to add an element to an array.
cars.append("Honda")

Removing Array Elements


You can use the pop() method to remove an element from the array.
cars.remove("Volvo")

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy