SlideShare a Scribd company logo
1 of 20
Tuples and dictionaries
Module 3 Functions, Tuples, Dictionaries,
Data processing
Module 3 Tuples and dictionaries
Sequence types & mutability
sequence types
• is a type of data
• is data which can be scanned by the for loop
2 kinds of Python data
• Mutable data can be freely updated at any time
• Immutable data cannot be modified in this way
Module 3 Tuples and dictionaries
What is a tuple?
tuple_1 = (1, 2, 4, 8)
tuple_2 = 1., .5, .25, .125
print(tuple_1)
print(tuple_2)
(1, 2, 4, 8)
(1.0, 0.5, 0.25, 0.125)
create a tuple
empty_tuple = () one_element_tuple_1 = (1, )
Module 3 Tuples and dictionaries
Do not modify tuple's contents!
my_tuple = (1, 10, 100,
1000)
print(my_tuple[0])
print(my_tuple[-1])
print(my_tuple[1:])
print(my_tuple[:-2])
for elem in my_tuple:
print(elem)
1
1000
(10, 100, 1000)
(1, 10)
1
10
100
1000
my_tuple = (1, 10, 100,
1000)
my_tuple.append(10000)
del my_tuple[0]
my_tuple[1] = -10
print(my_tuple)
AttributeError: 'tuple' object has no attribute 'append'
Module 3 Tuples and dictionaries
How to use a tuple
my_tuple = (1, 10, 100)
t1 = my_tuple + (1000,
10000)
t2 = my_tuple * 3
print(len(t2))
print(t1)
print(t2)
print(10 in my_tuple)
print(-10 not in my_tuple)
9
(1, 10, 100, 1000, 10000)
(1, 10, 100, 1, 10, 100, 1, 10, 100)
True
True
var = 123
t1 = (1, )
t2 = (2, )
t3 = (3, var)
t1, t2, t3 = t2, t3, t1
print(t1, t2, t3)
(2,) (3, 123) (1,)
Module 3 Tuples and dictionaries
What is a dictionary?
each key must be unique
a key may be any immutable type of object
a dictionary holds pairs of values
the len() function works for dictionaries
a dictionary is a one-way tool
Module 3 Tuples and dictionaries
How to make a dictionary?
dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"}
phone_numbers = {'boss': 5551234567, 'Suzy': 22657854310}
empty_dictionary = {}
print(dictionary['cat'])
print(phone_numbers['Suzy'])
chat
22657854310
dictionary = {
"cat": "chat",
"dog": "chien",
"horse": "cheval"
}
words = ['cat', 'lion', 'horse']
for word in words:
if word in dictionary:
print(word, "->", dictionary[word])
else:
print(word, "is not in dictionary")
cat -> chat
lion is not in dictionary
horse -> cheval
Module 3 Tuples and dictionaries
keys(), sorted()
dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"}
for key in dictionary.keys():
print(key, "->", dictionary[key])
horse -> cheval
dog -> chien
cat -> chat
dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"}
for key in sorted(dictionary.keys()):
print(key, "->", dictionary[key])
cat -> chat
dog -> chien
horse -> cheval
Module 3 Tuples and dictionaries
items() & values() methods
dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"}
for english, french in dictionary.items():
print(english, "->", french)
cat -> chat
dog -> chien
horse -> cheval
dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"}
for french in dictionary.values():
print(french)
cheval
chien
chat
Module 3 Tuples and dictionaries
Modifying and adding values
dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"}
dictionary['cat'] = 'minou'
print(dictionary)
{'cat': 'minou', 'dog': 'chien', 'horse': 'cheval'}
dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"}
dictionary['swan'] = 'cygne'
print(dictionary)
{'cat': 'chat', 'dog': 'chien', 'horse': 'cheval', 'swan': 'cygne'}
Module 3 Tuples and dictionaries
Tuples and dictionaries
• you need a program to evaluate the
students' average scores;
• the program should ask for the student's
name, followed by her/his single score;
• the names may be entered in any order;
• entering an empty name finishes the
inputting of the data;
• a list of all names, together with the
evaluated average score, should be then
emitted.
school_class = {}
while True:
name = input("Enter the student's name: ")
if name == '':
break
score = int(input("Enter the student's score (0-10): "))
if score not in range(0, 11):
break
if name in school_class:
school_class[name] += (score,)
else:
school_class[name] = (score,)
for name in sorted(school_class.keys()):
adding = 0
counter = 0
for score in school_class[name]:
adding += score
counter += 1
print(name, ":", adding / counter)
Module 3 Tuples and dictionaries
Key takeaways: tuples
Tuples are ordered and unchangeable (immutable) collections of data.
You can create an empty tuple, one-element tuple.
You can access tuple elements by indexing them.
Tuples are immutable, which means you cannot change their elements
You can loop through a tuple elements .
Module 3 Tuples and dictionaries
EXTRA: tuple(), list()
my_tuple = tuple((1, 2, "string"))
print(my_tuple)
my_list = [2, 4, 6]
print(my_list) # outputs: [2, 4, 6]
print(type(my_list)) # outputs: <class 'list'>
tup = tuple(my_list)
print(tup) # outputs: (2, 4, 6)
print(type(tup)) # outputs: <class 'tuple'>
tup = 1, 2, 3,
my_list = list(tup)
print(type(my_list)) # outputs: <class 'list'>
Module 3 Tuples and dictionaries
Key takeaways: dictionaries 1
Dictionaries are unordered*, changeable (mutable), and indexed
collections of data.
If you want to access a dictionary item:
pol_eng_dictionary = {"kwiat": "flower","woda": "water","gleba": "soil"}
item_1 = pol_eng_dictionary["gleba"] # ex. 1
print(item_1) # outputs: soil
item_2 = pol_eng_dictionary.get("woda")
print(item_2) # outputs: water
Module 3 Tuples and dictionaries
Key takeaways: dictionaries 2
If you want to change the value associated with a specific key:
To add or remove a key (and the associated value):
pol_eng_dictionary = {"kwiat": "flower","woda": "water","gleba": "soil"}
pol_eng_dictionary["zamek"] = "lock"
item = pol_eng_dictionary["zamek"]
print(item) # outputs: lock
phonebook = {} # an empty dictionary
phonebook["Adam"] = 3456783958 # create/add a key-value pair
print(phonebook) # outputs: {'Adam': 3456783958}
del phonebook["Adam"]
print(phonebook) # outputs: {}
Module 3 Tuples and dictionaries
Key takeaways: dictionaries 3
You can use the for loop to loop through a dictionary:
pol_eng_dictionary = {"kwiat": "flower"}
pol_eng_dictionary.update({"gleba": "soil"})
print(pol_eng_dictionary) # outputs: {'kwiat': 'flower', 'gleba': 'soil'}
pol_eng_dictionary.popitem()
print(pol_eng_dictionary) # outputs: {'kwiat': 'flower'}
pol_eng_dictionary = {"kwiat": "flower","woda": "water","gleba": "soil"}
for item in pol_eng_dictionary:
print(item)
# outputs: zamek
# woda
# gleba
Module 3 Tuples and dictionaries
Key takeaways: dictionaries 4
If you want to loop through a dictionary's keys and values:
pol_eng_dictionary = {"kwiat": "flower","woda": "water","gleba": "soil"}
for key, value in pol_eng_dictionary.items():
print("Pol/Eng ->", key, ":", value)
To check if a given key exists in a dictionary:
pol_eng_dictionary = {"kwiat": "flower","woda": "water","gleba": "soil"}
if "zamek" in pol_eng_dictionary:
print("Yes")
else:
print("No")
Module 3 Tuples and dictionaries
Key takeaways: dictionaries 5
To remove a specific item:
pol_eng_dictionary = {"kwiat": "flower","woda": "water","gleba": "soil"}
print(len(pol_eng_dictionary)) # outputs: 3
del pol_eng_dictionary["zamek"] # remove an item
print(len(pol_eng_dictionary)) # outputs: 2
pol_eng_dictionary.clear() # removes all the items
print(len(pol_eng_dictionary)) # outputs: 0
del pol_eng_dictionary # removes the dictionary
To copy a dictionary:
pol_eng_dictionary = {"kwiat": "flower","woda": "water","gleba": "soil"}
copy_dictionary = pol_eng_dictionary.copy()
Module 3 Tuples and dictionaries
LAB Practice
29. Tic-Tac-Toe
Congratulations!
You have completed Module 3
the defining and
using of functions
the concept of
passing
arguments in
different ways
name scope
issues
tuples and
dictionaries

More Related Content

What's hot

Puppet Language 4.0 - PuppetConf 2014
Puppet Language 4.0 - PuppetConf 2014Puppet Language 4.0 - PuppetConf 2014
Puppet Language 4.0 - PuppetConf 2014Puppet
 
Python Dictionary
Python DictionaryPython Dictionary
Python DictionaryColin Su
 
Pa1 session 3_slides
Pa1 session 3_slidesPa1 session 3_slides
Pa1 session 3_slidesaiclub_slides
 
Introduction to Search Systems - ScaleConf Colombia 2017
Introduction to Search Systems - ScaleConf Colombia 2017Introduction to Search Systems - ScaleConf Colombia 2017
Introduction to Search Systems - ScaleConf Colombia 2017Toria Gibbs
 
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...Matt Harrison
 
Python Puzzlers - 2016 Edition
Python Puzzlers - 2016 EditionPython Puzzlers - 2016 Edition
Python Puzzlers - 2016 EditionNandan Sawant
 
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...Matt Harrison
 
Farhana shaikh webinar_dictionaries
Farhana shaikh webinar_dictionariesFarhana shaikh webinar_dictionaries
Farhana shaikh webinar_dictionariesFarhana Shaikh
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to PythonUC San Diego
 
A Search Index is Not a Database Index - Full Stack Toronto 2017
A Search Index is Not a Database Index - Full Stack Toronto 2017A Search Index is Not a Database Index - Full Stack Toronto 2017
A Search Index is Not a Database Index - Full Stack Toronto 2017Toria Gibbs
 
Ejercicios de estilo en la programación
Ejercicios de estilo en la programaciónEjercicios de estilo en la programación
Ejercicios de estilo en la programaciónSoftware Guru
 
PyLecture4 -Python Basics2-
PyLecture4 -Python Basics2-PyLecture4 -Python Basics2-
PyLecture4 -Python Basics2-Yoshiki Satotani
 
Elixir pattern matching and recursion
Elixir pattern matching and recursionElixir pattern matching and recursion
Elixir pattern matching and recursionBob Firestone
 

What's hot (18)

Sets in python
Sets in pythonSets in python
Sets in python
 
Oop lecture7
Oop lecture7Oop lecture7
Oop lecture7
 
Puppet Language 4.0 - PuppetConf 2014
Puppet Language 4.0 - PuppetConf 2014Puppet Language 4.0 - PuppetConf 2014
Puppet Language 4.0 - PuppetConf 2014
 
Python Dictionary
Python DictionaryPython Dictionary
Python Dictionary
 
Pa1 session 3_slides
Pa1 session 3_slidesPa1 session 3_slides
Pa1 session 3_slides
 
Dictionary in python
Dictionary in pythonDictionary in python
Dictionary in python
 
Introduction to Search Systems - ScaleConf Colombia 2017
Introduction to Search Systems - ScaleConf Colombia 2017Introduction to Search Systems - ScaleConf Colombia 2017
Introduction to Search Systems - ScaleConf Colombia 2017
 
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
 
Python Puzzlers - 2016 Edition
Python Puzzlers - 2016 EditionPython Puzzlers - 2016 Edition
Python Puzzlers - 2016 Edition
 
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
 
Farhana shaikh webinar_dictionaries
Farhana shaikh webinar_dictionariesFarhana shaikh webinar_dictionaries
Farhana shaikh webinar_dictionaries
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 
A Search Index is Not a Database Index - Full Stack Toronto 2017
A Search Index is Not a Database Index - Full Stack Toronto 2017A Search Index is Not a Database Index - Full Stack Toronto 2017
A Search Index is Not a Database Index - Full Stack Toronto 2017
 
Slicing in Python - What is It?
Slicing in Python - What is It?Slicing in Python - What is It?
Slicing in Python - What is It?
 
Ejercicios de estilo en la programación
Ejercicios de estilo en la programaciónEjercicios de estilo en la programación
Ejercicios de estilo en la programación
 
PyLecture4 -Python Basics2-
PyLecture4 -Python Basics2-PyLecture4 -Python Basics2-
PyLecture4 -Python Basics2-
 
Swift tips and tricks
Swift tips and tricksSwift tips and tricks
Swift tips and tricks
 
Elixir pattern matching and recursion
Elixir pattern matching and recursionElixir pattern matching and recursion
Elixir pattern matching and recursion
 

Similar to Python PCEP Tuples and Dictionaries

Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionarySoba Arjun
 
Learn python - for beginners - part-2
Learn python - for beginners - part-2Learn python - for beginners - part-2
Learn python - for beginners - part-2RajKumar Rampelli
 
Java script objects 1
Java script objects 1Java script objects 1
Java script objects 1H K
 
Class 5: If, while & lists
Class 5: If, while & listsClass 5: If, while & lists
Class 5: If, while & listsMarc Gouw
 
Class 6: Lists & dictionaries
Class 6: Lists & dictionariesClass 6: Lists & dictionaries
Class 6: Lists & dictionariesMarc Gouw
 
ITS-16163: Module 5 Using Lists and Dictionaries
ITS-16163: Module 5 Using Lists and DictionariesITS-16163: Module 5 Using Lists and Dictionaries
ITS-16163: Module 5 Using Lists and Dictionariesoudesign
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayUtkarsh Sengar
 
Python quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung FuPython quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung Fuclimatewarrior
 
Beginners python cheat sheet - Basic knowledge
Beginners python cheat sheet - Basic knowledge Beginners python cheat sheet - Basic knowledge
Beginners python cheat sheet - Basic knowledge O T
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxAbhishek Tirkey
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxGauravPandey43518
 
The Ring programming language version 1.10 book - Part 30 of 212
The Ring programming language version 1.10 book - Part 30 of 212The Ring programming language version 1.10 book - Part 30 of 212
The Ring programming language version 1.10 book - Part 30 of 212Mahmoud Samir Fayed
 
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptxLecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptxlokeshgoud13
 
python_avw - Unit-03.pdf
python_avw - Unit-03.pdfpython_avw - Unit-03.pdf
python_avw - Unit-03.pdfAshaWankar1
 

Similar to Python PCEP Tuples and Dictionaries (20)

Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
 
Learn python - for beginners - part-2
Learn python - for beginners - part-2Learn python - for beginners - part-2
Learn python - for beginners - part-2
 
Java script objects 1
Java script objects 1Java script objects 1
Java script objects 1
 
Class 5: If, while & lists
Class 5: If, while & listsClass 5: If, while & lists
Class 5: If, while & lists
 
Dictionary
DictionaryDictionary
Dictionary
 
Tuples-and-Dictionaries.pptx
Tuples-and-Dictionaries.pptxTuples-and-Dictionaries.pptx
Tuples-and-Dictionaries.pptx
 
Python-Tuples
Python-TuplesPython-Tuples
Python-Tuples
 
Class 6: Lists & dictionaries
Class 6: Lists & dictionariesClass 6: Lists & dictionaries
Class 6: Lists & dictionaries
 
ITS-16163: Module 5 Using Lists and Dictionaries
ITS-16163: Module 5 Using Lists and DictionariesITS-16163: Module 5 Using Lists and Dictionaries
ITS-16163: Module 5 Using Lists and Dictionaries
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
 
Python quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung FuPython quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung Fu
 
Beginners python cheat sheet - Basic knowledge
Beginners python cheat sheet - Basic knowledge Beginners python cheat sheet - Basic knowledge
Beginners python cheat sheet - Basic knowledge
 
Python cheatsheet for beginners
Python cheatsheet for beginnersPython cheatsheet for beginners
Python cheatsheet for beginners
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 
Python for Beginners(v3)
Python for Beginners(v3)Python for Beginners(v3)
Python for Beginners(v3)
 
Python review2
Python review2Python review2
Python review2
 
The Ring programming language version 1.10 book - Part 30 of 212
The Ring programming language version 1.10 book - Part 30 of 212The Ring programming language version 1.10 book - Part 30 of 212
The Ring programming language version 1.10 book - Part 30 of 212
 
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptxLecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
 
python_avw - Unit-03.pdf
python_avw - Unit-03.pdfpython_avw - Unit-03.pdf
python_avw - Unit-03.pdf
 

More from IHTMINSTITUTE

Python PCEP Tuples and Dictionaries
Python PCEP Tuples and DictionariesPython PCEP Tuples and Dictionaries
Python PCEP Tuples and DictionariesIHTMINSTITUTE
 
Python PCEP Creating Simple Functions
Python PCEP Creating Simple FunctionsPython PCEP Creating Simple Functions
Python PCEP Creating Simple FunctionsIHTMINSTITUTE
 
Python PCEP Functions And Scopes
Python PCEP Functions And ScopesPython PCEP Functions And Scopes
Python PCEP Functions And ScopesIHTMINSTITUTE
 
Python PCEP Function Parameters
Python PCEP Function ParametersPython PCEP Function Parameters
Python PCEP Function ParametersIHTMINSTITUTE
 
Python PCEP Functions
Python PCEP FunctionsPython PCEP Functions
Python PCEP FunctionsIHTMINSTITUTE
 
Python PCEP Multidemensional Arrays
Python PCEP Multidemensional ArraysPython PCEP Multidemensional Arrays
Python PCEP Multidemensional ArraysIHTMINSTITUTE
 
Python PCEP Operations On Lists
Python PCEP Operations On ListsPython PCEP Operations On Lists
Python PCEP Operations On ListsIHTMINSTITUTE
 
Python PCEP Sorting Simple Lists
Python PCEP Sorting Simple ListsPython PCEP Sorting Simple Lists
Python PCEP Sorting Simple ListsIHTMINSTITUTE
 
Python PCEP Lists Collections of Data
Python PCEP Lists Collections of DataPython PCEP Lists Collections of Data
Python PCEP Lists Collections of DataIHTMINSTITUTE
 
Python PCEP Logic Bit Operations
Python PCEP Logic Bit OperationsPython PCEP Logic Bit Operations
Python PCEP Logic Bit OperationsIHTMINSTITUTE
 
Python PCEP Comparison Operators And Conditional Execution
Python PCEP Comparison Operators And Conditional ExecutionPython PCEP Comparison Operators And Conditional Execution
Python PCEP Comparison Operators And Conditional ExecutionIHTMINSTITUTE
 
Python PCEP How To Talk To Computer
Python PCEP How To Talk To ComputerPython PCEP How To Talk To Computer
Python PCEP How To Talk To ComputerIHTMINSTITUTE
 
Python PCEP Variables
Python PCEP VariablesPython PCEP Variables
Python PCEP VariablesIHTMINSTITUTE
 
Python PCEP Operators
Python PCEP OperatorsPython PCEP Operators
Python PCEP OperatorsIHTMINSTITUTE
 
Python PCEP Literals
Python PCEP LiteralsPython PCEP Literals
Python PCEP LiteralsIHTMINSTITUTE
 
IHTM Python PCEP Hello World
IHTM Python PCEP Hello WorldIHTM Python PCEP Hello World
IHTM Python PCEP Hello WorldIHTMINSTITUTE
 
IHTM Python PCEP Introduction to Python
IHTM Python PCEP Introduction to PythonIHTM Python PCEP Introduction to Python
IHTM Python PCEP Introduction to PythonIHTMINSTITUTE
 
Python PCEP Welcome Opening
Python PCEP Welcome OpeningPython PCEP Welcome Opening
Python PCEP Welcome OpeningIHTMINSTITUTE
 

More from IHTMINSTITUTE (19)

Python PCEP Tuples and Dictionaries
Python PCEP Tuples and DictionariesPython PCEP Tuples and Dictionaries
Python PCEP Tuples and Dictionaries
 
Python PCEP Creating Simple Functions
Python PCEP Creating Simple FunctionsPython PCEP Creating Simple Functions
Python PCEP Creating Simple Functions
 
Python PCEP Functions And Scopes
Python PCEP Functions And ScopesPython PCEP Functions And Scopes
Python PCEP Functions And Scopes
 
Python PCEP Function Parameters
Python PCEP Function ParametersPython PCEP Function Parameters
Python PCEP Function Parameters
 
Python PCEP Functions
Python PCEP FunctionsPython PCEP Functions
Python PCEP Functions
 
Python PCEP Multidemensional Arrays
Python PCEP Multidemensional ArraysPython PCEP Multidemensional Arrays
Python PCEP Multidemensional Arrays
 
Python PCEP Operations On Lists
Python PCEP Operations On ListsPython PCEP Operations On Lists
Python PCEP Operations On Lists
 
Python PCEP Sorting Simple Lists
Python PCEP Sorting Simple ListsPython PCEP Sorting Simple Lists
Python PCEP Sorting Simple Lists
 
Python PCEP Lists Collections of Data
Python PCEP Lists Collections of DataPython PCEP Lists Collections of Data
Python PCEP Lists Collections of Data
 
Python PCEP Logic Bit Operations
Python PCEP Logic Bit OperationsPython PCEP Logic Bit Operations
Python PCEP Logic Bit Operations
 
Python PCEP Loops
Python PCEP LoopsPython PCEP Loops
Python PCEP Loops
 
Python PCEP Comparison Operators And Conditional Execution
Python PCEP Comparison Operators And Conditional ExecutionPython PCEP Comparison Operators And Conditional Execution
Python PCEP Comparison Operators And Conditional Execution
 
Python PCEP How To Talk To Computer
Python PCEP How To Talk To ComputerPython PCEP How To Talk To Computer
Python PCEP How To Talk To Computer
 
Python PCEP Variables
Python PCEP VariablesPython PCEP Variables
Python PCEP Variables
 
Python PCEP Operators
Python PCEP OperatorsPython PCEP Operators
Python PCEP Operators
 
Python PCEP Literals
Python PCEP LiteralsPython PCEP Literals
Python PCEP Literals
 
IHTM Python PCEP Hello World
IHTM Python PCEP Hello WorldIHTM Python PCEP Hello World
IHTM Python PCEP Hello World
 
IHTM Python PCEP Introduction to Python
IHTM Python PCEP Introduction to PythonIHTM Python PCEP Introduction to Python
IHTM Python PCEP Introduction to Python
 
Python PCEP Welcome Opening
Python PCEP Welcome OpeningPython PCEP Welcome Opening
Python PCEP Welcome Opening
 

Recently uploaded

Call Girls Service Adil Nagar 7001305949 Need escorts Service Pooja Vip
Call Girls Service Adil Nagar 7001305949 Need escorts Service Pooja VipCall Girls Service Adil Nagar 7001305949 Need escorts Service Pooja Vip
Call Girls Service Adil Nagar 7001305949 Need escorts Service Pooja VipCall Girls Lucknow
 
10.pdfMature Call girls in Dubai +971563133746 Dubai Call girls
10.pdfMature Call girls in Dubai +971563133746 Dubai Call girls10.pdfMature Call girls in Dubai +971563133746 Dubai Call girls
10.pdfMature Call girls in Dubai +971563133746 Dubai Call girlsstephieert
 
定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一
定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一
定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一Fs
 
VIP 7001035870 Find & Meet Hyderabad Call Girls Dilsukhnagar high-profile Cal...
VIP 7001035870 Find & Meet Hyderabad Call Girls Dilsukhnagar high-profile Cal...VIP 7001035870 Find & Meet Hyderabad Call Girls Dilsukhnagar high-profile Cal...
VIP 7001035870 Find & Meet Hyderabad Call Girls Dilsukhnagar high-profile Cal...aditipandeya
 
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts serviceChennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts servicevipmodelshub1
 
VIP Kolkata Call Girl Salt Lake 👉 8250192130 Available With Room
VIP Kolkata Call Girl Salt Lake 👉 8250192130  Available With RoomVIP Kolkata Call Girl Salt Lake 👉 8250192130  Available With Room
VIP Kolkata Call Girl Salt Lake 👉 8250192130 Available With Roomishabajaj13
 
Russian Call girls in Dubai +971563133746 Dubai Call girls
Russian  Call girls in Dubai +971563133746 Dubai  Call girlsRussian  Call girls in Dubai +971563133746 Dubai  Call girls
Russian Call girls in Dubai +971563133746 Dubai Call girlsstephieert
 
Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
A Good Girl's Guide to Murder (A Good Girl's Guide to Murder, #1)
A Good Girl's Guide to Murder (A Good Girl's Guide to Murder, #1)A Good Girl's Guide to Murder (A Good Girl's Guide to Murder, #1)
A Good Girl's Guide to Murder (A Good Girl's Guide to Murder, #1)Christopher H Felton
 
Low Rate Call Girls Kolkata Avani 🤌 8250192130 🚀 Vip Call Girls Kolkata
Low Rate Call Girls Kolkata Avani 🤌  8250192130 🚀 Vip Call Girls KolkataLow Rate Call Girls Kolkata Avani 🤌  8250192130 🚀 Vip Call Girls Kolkata
Low Rate Call Girls Kolkata Avani 🤌 8250192130 🚀 Vip Call Girls Kolkataanamikaraghav4
 
VIP Call Girls Kolkata Ananya 🤌 8250192130 🚀 Vip Call Girls Kolkata
VIP Call Girls Kolkata Ananya 🤌  8250192130 🚀 Vip Call Girls KolkataVIP Call Girls Kolkata Ananya 🤌  8250192130 🚀 Vip Call Girls Kolkata
VIP Call Girls Kolkata Ananya 🤌 8250192130 🚀 Vip Call Girls Kolkataanamikaraghav4
 
Git and Github workshop GDSC MLRITM
Git and Github  workshop GDSC MLRITMGit and Github  workshop GDSC MLRITM
Git and Github workshop GDSC MLRITMgdsc13
 
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一Fs
 
Complet Documnetation for Smart Assistant Application for Disabled Person
Complet Documnetation   for Smart Assistant Application for Disabled PersonComplet Documnetation   for Smart Assistant Application for Disabled Person
Complet Documnetation for Smart Assistant Application for Disabled Personfurqan222004
 
Chennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts serviceChennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts servicesonalikaur4
 

Recently uploaded (20)

young call girls in Uttam Nagar🔝 9953056974 🔝 Delhi escort Service
young call girls in Uttam Nagar🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Uttam Nagar🔝 9953056974 🔝 Delhi escort Service
young call girls in Uttam Nagar🔝 9953056974 🔝 Delhi escort Service
 
Call Girls Service Adil Nagar 7001305949 Need escorts Service Pooja Vip
Call Girls Service Adil Nagar 7001305949 Need escorts Service Pooja VipCall Girls Service Adil Nagar 7001305949 Need escorts Service Pooja Vip
Call Girls Service Adil Nagar 7001305949 Need escorts Service Pooja Vip
 
10.pdfMature Call girls in Dubai +971563133746 Dubai Call girls
10.pdfMature Call girls in Dubai +971563133746 Dubai Call girls10.pdfMature Call girls in Dubai +971563133746 Dubai Call girls
10.pdfMature Call girls in Dubai +971563133746 Dubai Call girls
 
定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一
定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一
定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一
 
VIP 7001035870 Find & Meet Hyderabad Call Girls Dilsukhnagar high-profile Cal...
VIP 7001035870 Find & Meet Hyderabad Call Girls Dilsukhnagar high-profile Cal...VIP 7001035870 Find & Meet Hyderabad Call Girls Dilsukhnagar high-profile Cal...
VIP 7001035870 Find & Meet Hyderabad Call Girls Dilsukhnagar high-profile Cal...
 
Rohini Sector 26 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
Rohini Sector 26 Call Girls Delhi 9999965857 @Sabina Saikh No AdvanceRohini Sector 26 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
Rohini Sector 26 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
 
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts serviceChennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
 
VIP Kolkata Call Girl Salt Lake 👉 8250192130 Available With Room
VIP Kolkata Call Girl Salt Lake 👉 8250192130  Available With RoomVIP Kolkata Call Girl Salt Lake 👉 8250192130  Available With Room
VIP Kolkata Call Girl Salt Lake 👉 8250192130 Available With Room
 
Russian Call girls in Dubai +971563133746 Dubai Call girls
Russian  Call girls in Dubai +971563133746 Dubai  Call girlsRussian  Call girls in Dubai +971563133746 Dubai  Call girls
Russian Call girls in Dubai +971563133746 Dubai Call girls
 
Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Uttam Nagar Delhi 💯Call Us 🔝8264348440🔝
 
A Good Girl's Guide to Murder (A Good Girl's Guide to Murder, #1)
A Good Girl's Guide to Murder (A Good Girl's Guide to Murder, #1)A Good Girl's Guide to Murder (A Good Girl's Guide to Murder, #1)
A Good Girl's Guide to Murder (A Good Girl's Guide to Murder, #1)
 
Low Rate Call Girls Kolkata Avani 🤌 8250192130 🚀 Vip Call Girls Kolkata
Low Rate Call Girls Kolkata Avani 🤌  8250192130 🚀 Vip Call Girls KolkataLow Rate Call Girls Kolkata Avani 🤌  8250192130 🚀 Vip Call Girls Kolkata
Low Rate Call Girls Kolkata Avani 🤌 8250192130 🚀 Vip Call Girls Kolkata
 
Call Girls Service Dwarka @9999965857 Delhi 🫦 No Advance VVIP 🍎 SERVICE
Call Girls Service Dwarka @9999965857 Delhi 🫦 No Advance  VVIP 🍎 SERVICECall Girls Service Dwarka @9999965857 Delhi 🫦 No Advance  VVIP 🍎 SERVICE
Call Girls Service Dwarka @9999965857 Delhi 🫦 No Advance VVIP 🍎 SERVICE
 
Model Call Girl in Jamuna Vihar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in  Jamuna Vihar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in  Jamuna Vihar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Jamuna Vihar Delhi reach out to us at 🔝9953056974🔝
 
VIP Call Girls Kolkata Ananya 🤌 8250192130 🚀 Vip Call Girls Kolkata
VIP Call Girls Kolkata Ananya 🤌  8250192130 🚀 Vip Call Girls KolkataVIP Call Girls Kolkata Ananya 🤌  8250192130 🚀 Vip Call Girls Kolkata
VIP Call Girls Kolkata Ananya 🤌 8250192130 🚀 Vip Call Girls Kolkata
 
Git and Github workshop GDSC MLRITM
Git and Github  workshop GDSC MLRITMGit and Github  workshop GDSC MLRITM
Git and Github workshop GDSC MLRITM
 
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一
 
sasti delhi Call Girls in munirka 🔝 9953056974 🔝 escort Service-
sasti delhi Call Girls in munirka 🔝 9953056974 🔝 escort Service-sasti delhi Call Girls in munirka 🔝 9953056974 🔝 escort Service-
sasti delhi Call Girls in munirka 🔝 9953056974 🔝 escort Service-
 
Complet Documnetation for Smart Assistant Application for Disabled Person
Complet Documnetation   for Smart Assistant Application for Disabled PersonComplet Documnetation   for Smart Assistant Application for Disabled Person
Complet Documnetation for Smart Assistant Application for Disabled Person
 
Chennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts serviceChennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts service
 

Python PCEP Tuples and Dictionaries

  • 1. Tuples and dictionaries Module 3 Functions, Tuples, Dictionaries, Data processing
  • 2. Module 3 Tuples and dictionaries Sequence types & mutability sequence types • is a type of data • is data which can be scanned by the for loop 2 kinds of Python data • Mutable data can be freely updated at any time • Immutable data cannot be modified in this way
  • 3. Module 3 Tuples and dictionaries What is a tuple? tuple_1 = (1, 2, 4, 8) tuple_2 = 1., .5, .25, .125 print(tuple_1) print(tuple_2) (1, 2, 4, 8) (1.0, 0.5, 0.25, 0.125) create a tuple empty_tuple = () one_element_tuple_1 = (1, )
  • 4. Module 3 Tuples and dictionaries Do not modify tuple's contents! my_tuple = (1, 10, 100, 1000) print(my_tuple[0]) print(my_tuple[-1]) print(my_tuple[1:]) print(my_tuple[:-2]) for elem in my_tuple: print(elem) 1 1000 (10, 100, 1000) (1, 10) 1 10 100 1000 my_tuple = (1, 10, 100, 1000) my_tuple.append(10000) del my_tuple[0] my_tuple[1] = -10 print(my_tuple) AttributeError: 'tuple' object has no attribute 'append'
  • 5. Module 3 Tuples and dictionaries How to use a tuple my_tuple = (1, 10, 100) t1 = my_tuple + (1000, 10000) t2 = my_tuple * 3 print(len(t2)) print(t1) print(t2) print(10 in my_tuple) print(-10 not in my_tuple) 9 (1, 10, 100, 1000, 10000) (1, 10, 100, 1, 10, 100, 1, 10, 100) True True var = 123 t1 = (1, ) t2 = (2, ) t3 = (3, var) t1, t2, t3 = t2, t3, t1 print(t1, t2, t3) (2,) (3, 123) (1,)
  • 6. Module 3 Tuples and dictionaries What is a dictionary? each key must be unique a key may be any immutable type of object a dictionary holds pairs of values the len() function works for dictionaries a dictionary is a one-way tool
  • 7. Module 3 Tuples and dictionaries How to make a dictionary? dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"} phone_numbers = {'boss': 5551234567, 'Suzy': 22657854310} empty_dictionary = {} print(dictionary['cat']) print(phone_numbers['Suzy']) chat 22657854310 dictionary = { "cat": "chat", "dog": "chien", "horse": "cheval" } words = ['cat', 'lion', 'horse'] for word in words: if word in dictionary: print(word, "->", dictionary[word]) else: print(word, "is not in dictionary") cat -> chat lion is not in dictionary horse -> cheval
  • 8. Module 3 Tuples and dictionaries keys(), sorted() dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"} for key in dictionary.keys(): print(key, "->", dictionary[key]) horse -> cheval dog -> chien cat -> chat dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"} for key in sorted(dictionary.keys()): print(key, "->", dictionary[key]) cat -> chat dog -> chien horse -> cheval
  • 9. Module 3 Tuples and dictionaries items() & values() methods dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"} for english, french in dictionary.items(): print(english, "->", french) cat -> chat dog -> chien horse -> cheval dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"} for french in dictionary.values(): print(french) cheval chien chat
  • 10. Module 3 Tuples and dictionaries Modifying and adding values dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"} dictionary['cat'] = 'minou' print(dictionary) {'cat': 'minou', 'dog': 'chien', 'horse': 'cheval'} dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"} dictionary['swan'] = 'cygne' print(dictionary) {'cat': 'chat', 'dog': 'chien', 'horse': 'cheval', 'swan': 'cygne'}
  • 11. Module 3 Tuples and dictionaries Tuples and dictionaries • you need a program to evaluate the students' average scores; • the program should ask for the student's name, followed by her/his single score; • the names may be entered in any order; • entering an empty name finishes the inputting of the data; • a list of all names, together with the evaluated average score, should be then emitted. school_class = {} while True: name = input("Enter the student's name: ") if name == '': break score = int(input("Enter the student's score (0-10): ")) if score not in range(0, 11): break if name in school_class: school_class[name] += (score,) else: school_class[name] = (score,) for name in sorted(school_class.keys()): adding = 0 counter = 0 for score in school_class[name]: adding += score counter += 1 print(name, ":", adding / counter)
  • 12. Module 3 Tuples and dictionaries Key takeaways: tuples Tuples are ordered and unchangeable (immutable) collections of data. You can create an empty tuple, one-element tuple. You can access tuple elements by indexing them. Tuples are immutable, which means you cannot change their elements You can loop through a tuple elements .
  • 13. Module 3 Tuples and dictionaries EXTRA: tuple(), list() my_tuple = tuple((1, 2, "string")) print(my_tuple) my_list = [2, 4, 6] print(my_list) # outputs: [2, 4, 6] print(type(my_list)) # outputs: <class 'list'> tup = tuple(my_list) print(tup) # outputs: (2, 4, 6) print(type(tup)) # outputs: <class 'tuple'> tup = 1, 2, 3, my_list = list(tup) print(type(my_list)) # outputs: <class 'list'>
  • 14. Module 3 Tuples and dictionaries Key takeaways: dictionaries 1 Dictionaries are unordered*, changeable (mutable), and indexed collections of data. If you want to access a dictionary item: pol_eng_dictionary = {"kwiat": "flower","woda": "water","gleba": "soil"} item_1 = pol_eng_dictionary["gleba"] # ex. 1 print(item_1) # outputs: soil item_2 = pol_eng_dictionary.get("woda") print(item_2) # outputs: water
  • 15. Module 3 Tuples and dictionaries Key takeaways: dictionaries 2 If you want to change the value associated with a specific key: To add or remove a key (and the associated value): pol_eng_dictionary = {"kwiat": "flower","woda": "water","gleba": "soil"} pol_eng_dictionary["zamek"] = "lock" item = pol_eng_dictionary["zamek"] print(item) # outputs: lock phonebook = {} # an empty dictionary phonebook["Adam"] = 3456783958 # create/add a key-value pair print(phonebook) # outputs: {'Adam': 3456783958} del phonebook["Adam"] print(phonebook) # outputs: {}
  • 16. Module 3 Tuples and dictionaries Key takeaways: dictionaries 3 You can use the for loop to loop through a dictionary: pol_eng_dictionary = {"kwiat": "flower"} pol_eng_dictionary.update({"gleba": "soil"}) print(pol_eng_dictionary) # outputs: {'kwiat': 'flower', 'gleba': 'soil'} pol_eng_dictionary.popitem() print(pol_eng_dictionary) # outputs: {'kwiat': 'flower'} pol_eng_dictionary = {"kwiat": "flower","woda": "water","gleba": "soil"} for item in pol_eng_dictionary: print(item) # outputs: zamek # woda # gleba
  • 17. Module 3 Tuples and dictionaries Key takeaways: dictionaries 4 If you want to loop through a dictionary's keys and values: pol_eng_dictionary = {"kwiat": "flower","woda": "water","gleba": "soil"} for key, value in pol_eng_dictionary.items(): print("Pol/Eng ->", key, ":", value) To check if a given key exists in a dictionary: pol_eng_dictionary = {"kwiat": "flower","woda": "water","gleba": "soil"} if "zamek" in pol_eng_dictionary: print("Yes") else: print("No")
  • 18. Module 3 Tuples and dictionaries Key takeaways: dictionaries 5 To remove a specific item: pol_eng_dictionary = {"kwiat": "flower","woda": "water","gleba": "soil"} print(len(pol_eng_dictionary)) # outputs: 3 del pol_eng_dictionary["zamek"] # remove an item print(len(pol_eng_dictionary)) # outputs: 2 pol_eng_dictionary.clear() # removes all the items print(len(pol_eng_dictionary)) # outputs: 0 del pol_eng_dictionary # removes the dictionary To copy a dictionary: pol_eng_dictionary = {"kwiat": "flower","woda": "water","gleba": "soil"} copy_dictionary = pol_eng_dictionary.copy()
  • 19. Module 3 Tuples and dictionaries LAB Practice 29. Tic-Tac-Toe
  • 20. Congratulations! You have completed Module 3 the defining and using of functions the concept of passing arguments in different ways name scope issues tuples and dictionaries