SlideShare a Scribd company logo
Exercise: simple list creation
1 Ask the user to enter an integer, n
Create a list of integers from 0 to n-1
Print this list
2
3
Solution
n =int(input())
x =list(range(n))
print(x)
Exercise: more list creation
1 Ask the user to enter an integer, n
Store the square of all the odd numbers less than
n in a list
Print this list
2
3
Solution
n =int(input())
result = []
for i in range(1, n, 2):
result.append(i*i)
print(result)
Exercise: tuple creation
1 Ask the user to enter an integer, n
Store the square of all the odd numbers less than
n
Print a tuple of this list
2
3
Solution
n =int(input())
result = []
for i in range(1, n, 2):
result.append(i*i)
print(tuple(result))
Hint: string to list/tuple
Here is an easy way to convert a string to a list of
characters.
Try this:
In []: x =’hello’
In []:print(list(x))
In []:print(tuple(x))
Exercise: list of Fibonacci
1 Ask the user to enter an integer, n(≥ 1)
2 Store the first n numbers of the Fibonnaci series in
a list
Print this list
3
Solution
n =int(input()) a, b = 0,
1
result = [0]
for I in range(n-1):
result.append(b)
a, b = b, a+b
print(result)
Exercise: square a list of integers
1 Ask the user to enter a list of integers separated by
spaces
Convert this into a list of integers but square each
element
Print this list
2
3
For example, if the user enters 1 2 3 4, print:
[1, 4, 9, 16]
Solution
text =input()
result = []
for item in text.split():
x =int(item)
result.append(x*x)
print(result)
Exercise: list of tuples
1 Ask the user to enter a list of integers separated by
spaces
Convert this into a list of integers but square each
element
Store the integer and its square in a tuple, put this
into a list
Print this list
2
3
4
For example, if the user enters 1 2 3 4, print:
[(1, 1), (2, 4), (3, 9), (4, 16)]
Solution
text =input()
result = []
for item in text.split():
x =int(item)
result.append((x, x*x))
print(result)
Hint: iterating over a list of tuples
Consider the following code:
In []: data = [(1, 1), (2, 4), (3, 9), (4,
16)]
We can iterate over this as follows:
In []:for x, y in data:
.....:print(x, y)
Exercise: list methods
1 Ask the user to enter a string
Convert this into a list of characters
Sort this list in ascending order
Now eliminate any repeated values in this list
Print this list
2
3
4
5
For example, if the user enters hello, print:
[’e’,’h’,’l’,’o’]
Solution
text =input()
chars =list(text)
chars.sort()
result = []
for c in chars:
if c not in result:
result.append(c)
print(result)
Another solution
text =input()
chars =set(text)
chars =list(chars)
chars.sort()
print(chars)
Another solution
chars =set(input())
print(sorted(chars))
Exercise: simple dictionaries
1 Ask the user for a list of integers separated by
spaces
For each integer, store the string version as the
key and the square of the integer value as the
value in a dictionary.
Print the resulting dictionary
2
3
For example if the user enters "1 3 5", print:
{’1’: 1,’3’: 9,’5’: 25}
Hint: simply print the resulting dictionary
Solution
text =input()
d = {}
for item in text.split():
x =int(item)
d[item] = x*x
print(d)
Exercise: mapping using dicts
1 Create a mapping from 3 character month name to
month number
2 Hint: for example {’jan’: 1, ’dec’: 12}
Ask the user for a 3 character month code
lower/upper mixed
Print the month number corresponding to the
month the user entered.
3
4
For example if the user enters "Jul", print:
7
Solution
months = (’janfebmaraprmayjunjul’+
’augsepoctnovdec’).split()
month2mm = {}
for i in range(len(months)):
month2mm[months[i]] = i + 1
text =input()
mon = text[:3].lower()
print(month2mm[mon])
Aside: using enumerate example
for i, char in enumerate(’hello’):
print(i, char)
Aside: using enumerate
months = (’janfebmaraprmayjunjul’+
’augsepoctnovdec’).split()
month2mm = {}
for i, month in enumerate(months):
month2mm[month] = i + 1
#Is easier/nicer than this:
for I inrange(len(months)):
month2mm[months[i]] = i + 1
Exercise: dictionaries
1 Ask the user to enter a string
Convert this to lower case
Count the number of occurrences of each
character in the string
Hint: use a dict
Print the result in sorted order of the characters
2
3
4
5
For example, if the user enters helloo, print:
e 1
h 1
l 2
o 2
Solution
text =input().lower()
result = {}
for char in text:
if char in result:
result[char] += 1
else:
result[char] = 1
for char in sorted(result):
print(char, result[char])
Problem: datestring to date tuple
You are given date strings of the form “29 Jul, 2009”, or
“4 January 2008”. In other words a number, a string
and another number, with a comma sometimes
separating the items.
Write a program that takes such a string as input and
prints a tuple (yyyy, mm, dd) where all three elements
are ints.
Solution
months = (’janfebmaraprmayjunjul’+
’augsepoctnovdec’).split()
month2mm = {}
for i, month in enumerate(months):
month2mm[month] = i + 1
date =input()
date = date.replace(’,’,’’) day, month,
year = date.split()
dd, yyyy =int(day),int(year)
mon = month[:3].lower()
mm = month2mm[mon] print((yyyy,
mm, dd))
FOSSEE Team (FOSSEE – IITB) Basic Python 30 / 31
That’s all folks!
FOSSEE Team (FOSSEE – IITB) Basic Python 31 / 31

More Related Content

Similar to Practice_Exercises_Data_Structures.pptx

The Ring programming language version 1.7 book - Part 26 of 196
The Ring programming language version 1.7 book - Part 26 of 196The Ring programming language version 1.7 book - Part 26 of 196
The Ring programming language version 1.7 book - Part 26 of 196
Mahmoud Samir Fayed
 
Revision Tour 1 and 2 complete.doc
Revision Tour 1 and 2 complete.docRevision Tour 1 and 2 complete.doc
Revision Tour 1 and 2 complete.doc
SrikrishnaVundavalli
 
Data Handling
Data Handling Data Handling
Data Handling
bharath916489
 
Python Lecture 8
Python Lecture 8Python Lecture 8
Python Lecture 8
Inzamam Baig
 
GE3151_PSPP_UNIT_4_Notes
GE3151_PSPP_UNIT_4_NotesGE3151_PSPP_UNIT_4_Notes
GE3151_PSPP_UNIT_4_Notes
Asst.prof M.Gokilavani
 
TENSOR DECOMPOSITION WITH PYTHON
TENSOR DECOMPOSITION WITH PYTHONTENSOR DECOMPOSITION WITH PYTHON
TENSOR DECOMPOSITION WITH PYTHON
André Panisson
 
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdfGE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
Asst.prof M.Gokilavani
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
Soba Arjun
 
List_tuple_dictionary.pptx
List_tuple_dictionary.pptxList_tuple_dictionary.pptx
List_tuple_dictionary.pptx
ChandanVatsa2
 
NumPy_Broadcasting Data Science - Python.pptx
NumPy_Broadcasting Data Science - Python.pptxNumPy_Broadcasting Data Science - Python.pptx
NumPy_Broadcasting Data Science - Python.pptx
JohnWilliam111370
 
Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptx
MihirDatir1
 
‏‏chap6 list tuples.pptx
‏‏chap6 list tuples.pptx‏‏chap6 list tuples.pptx
‏‏chap6 list tuples.pptx
RamiHarrathi1
 
Python Lecture 11
Python Lecture 11Python Lecture 11
Python Lecture 11
Inzamam Baig
 
Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptx
MihirDatir
 
Python programming workshop session 3
Python programming workshop session 3Python programming workshop session 3
Python programming workshop session 3
Abdul Haseeb
 
Python - Data Collection
Python - Data CollectionPython - Data Collection
Python - Data Collection
JoseTanJr
 
Function Analysis v.2
Function Analysis v.2Function Analysis v.2
Function Analysis v.2
Arun Umrao
 
Unit 4 python -list methods
Unit 4   python -list methodsUnit 4   python -list methods
Unit 4 python -list methods
narmadhakin
 
R Cheat Sheet for Data Analysts and Statisticians.pdf
R Cheat Sheet for Data Analysts and Statisticians.pdfR Cheat Sheet for Data Analysts and Statisticians.pdf
R Cheat Sheet for Data Analysts and Statisticians.pdf
Timothy McBush Hiele
 

Similar to Practice_Exercises_Data_Structures.pptx (20)

Pytho lists
Pytho listsPytho lists
Pytho lists
 
The Ring programming language version 1.7 book - Part 26 of 196
The Ring programming language version 1.7 book - Part 26 of 196The Ring programming language version 1.7 book - Part 26 of 196
The Ring programming language version 1.7 book - Part 26 of 196
 
Revision Tour 1 and 2 complete.doc
Revision Tour 1 and 2 complete.docRevision Tour 1 and 2 complete.doc
Revision Tour 1 and 2 complete.doc
 
Data Handling
Data Handling Data Handling
Data Handling
 
Python Lecture 8
Python Lecture 8Python Lecture 8
Python Lecture 8
 
GE3151_PSPP_UNIT_4_Notes
GE3151_PSPP_UNIT_4_NotesGE3151_PSPP_UNIT_4_Notes
GE3151_PSPP_UNIT_4_Notes
 
TENSOR DECOMPOSITION WITH PYTHON
TENSOR DECOMPOSITION WITH PYTHONTENSOR DECOMPOSITION WITH PYTHON
TENSOR DECOMPOSITION WITH PYTHON
 
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdfGE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
 
List_tuple_dictionary.pptx
List_tuple_dictionary.pptxList_tuple_dictionary.pptx
List_tuple_dictionary.pptx
 
NumPy_Broadcasting Data Science - Python.pptx
NumPy_Broadcasting Data Science - Python.pptxNumPy_Broadcasting Data Science - Python.pptx
NumPy_Broadcasting Data Science - Python.pptx
 
Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptx
 
‏‏chap6 list tuples.pptx
‏‏chap6 list tuples.pptx‏‏chap6 list tuples.pptx
‏‏chap6 list tuples.pptx
 
Python Lecture 11
Python Lecture 11Python Lecture 11
Python Lecture 11
 
Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptx
 
Python programming workshop session 3
Python programming workshop session 3Python programming workshop session 3
Python programming workshop session 3
 
Python - Data Collection
Python - Data CollectionPython - Data Collection
Python - Data Collection
 
Function Analysis v.2
Function Analysis v.2Function Analysis v.2
Function Analysis v.2
 
Unit 4 python -list methods
Unit 4   python -list methodsUnit 4   python -list methods
Unit 4 python -list methods
 
R Cheat Sheet for Data Analysts and Statisticians.pdf
R Cheat Sheet for Data Analysts and Statisticians.pdfR Cheat Sheet for Data Analysts and Statisticians.pdf
R Cheat Sheet for Data Analysts and Statisticians.pdf
 

More from Rahul Borate

PigHive presentation and hive impor.pptx
PigHive presentation and hive impor.pptxPigHive presentation and hive impor.pptx
PigHive presentation and hive impor.pptx
Rahul Borate
 
Unit 4_Introduction to Server Farms.pptx
Unit 4_Introduction to Server Farms.pptxUnit 4_Introduction to Server Farms.pptx
Unit 4_Introduction to Server Farms.pptx
Rahul Borate
 
Unit 3_Data Center Design in storage.pptx
Unit  3_Data Center Design in storage.pptxUnit  3_Data Center Design in storage.pptx
Unit 3_Data Center Design in storage.pptx
Rahul Borate
 
Fundamentals of storage Unit III Backup and Recovery.ppt
Fundamentals of storage Unit III Backup and Recovery.pptFundamentals of storage Unit III Backup and Recovery.ppt
Fundamentals of storage Unit III Backup and Recovery.ppt
Rahul Borate
 
UNIT I Introduction to NoSQL.pptx
UNIT I Introduction to NoSQL.pptxUNIT I Introduction to NoSQL.pptx
UNIT I Introduction to NoSQL.pptx
Rahul Borate
 
Confusion Matrix.pptx
Confusion Matrix.pptxConfusion Matrix.pptx
Confusion Matrix.pptx
Rahul Borate
 
Unit 4 SVM and AVR.ppt
Unit 4 SVM and AVR.pptUnit 4 SVM and AVR.ppt
Unit 4 SVM and AVR.ppt
Rahul Borate
 
Unit I Fundamentals of Cloud Computing.pptx
Unit I Fundamentals of Cloud Computing.pptxUnit I Fundamentals of Cloud Computing.pptx
Unit I Fundamentals of Cloud Computing.pptx
Rahul Borate
 
Unit II Cloud Delivery Models.pptx
Unit II Cloud Delivery Models.pptxUnit II Cloud Delivery Models.pptx
Unit II Cloud Delivery Models.pptx
Rahul Borate
 
QQ Plot.pptx
QQ Plot.pptxQQ Plot.pptx
QQ Plot.pptx
Rahul Borate
 
EDA.pptx
EDA.pptxEDA.pptx
EDA.pptx
Rahul Borate
 
Module III MachineLearningSparkML.pptx
Module III MachineLearningSparkML.pptxModule III MachineLearningSparkML.pptx
Module III MachineLearningSparkML.pptx
Rahul Borate
 
Unit II Real Time Data Processing tools.pptx
Unit II Real Time Data Processing tools.pptxUnit II Real Time Data Processing tools.pptx
Unit II Real Time Data Processing tools.pptx
Rahul Borate
 
2.2 Logit and Probit.pptx
2.2 Logit and Probit.pptx2.2 Logit and Probit.pptx
2.2 Logit and Probit.pptx
Rahul Borate
 
UNIT I Streaming Data & Architectures.pptx
UNIT I Streaming Data & Architectures.pptxUNIT I Streaming Data & Architectures.pptx
UNIT I Streaming Data & Architectures.pptx
Rahul Borate
 
UNIT I Introduction to NoSQL.pptx
UNIT I Introduction to NoSQL.pptxUNIT I Introduction to NoSQL.pptx
UNIT I Introduction to NoSQL.pptx
Rahul Borate
 
Practice_Exercises_Files_and_Exceptions.pptx
Practice_Exercises_Files_and_Exceptions.pptxPractice_Exercises_Files_and_Exceptions.pptx
Practice_Exercises_Files_and_Exceptions.pptx
Rahul Borate
 
blog creation.pdf
blog creation.pdfblog creation.pdf
blog creation.pdf
Rahul Borate
 
Chapter I.pptx
Chapter I.pptxChapter I.pptx
Chapter I.pptx
Rahul Borate
 
Polygon Mesh.ppt
Polygon Mesh.pptPolygon Mesh.ppt
Polygon Mesh.ppt
Rahul Borate
 

More from Rahul Borate (20)

PigHive presentation and hive impor.pptx
PigHive presentation and hive impor.pptxPigHive presentation and hive impor.pptx
PigHive presentation and hive impor.pptx
 
Unit 4_Introduction to Server Farms.pptx
Unit 4_Introduction to Server Farms.pptxUnit 4_Introduction to Server Farms.pptx
Unit 4_Introduction to Server Farms.pptx
 
Unit 3_Data Center Design in storage.pptx
Unit  3_Data Center Design in storage.pptxUnit  3_Data Center Design in storage.pptx
Unit 3_Data Center Design in storage.pptx
 
Fundamentals of storage Unit III Backup and Recovery.ppt
Fundamentals of storage Unit III Backup and Recovery.pptFundamentals of storage Unit III Backup and Recovery.ppt
Fundamentals of storage Unit III Backup and Recovery.ppt
 
UNIT I Introduction to NoSQL.pptx
UNIT I Introduction to NoSQL.pptxUNIT I Introduction to NoSQL.pptx
UNIT I Introduction to NoSQL.pptx
 
Confusion Matrix.pptx
Confusion Matrix.pptxConfusion Matrix.pptx
Confusion Matrix.pptx
 
Unit 4 SVM and AVR.ppt
Unit 4 SVM and AVR.pptUnit 4 SVM and AVR.ppt
Unit 4 SVM and AVR.ppt
 
Unit I Fundamentals of Cloud Computing.pptx
Unit I Fundamentals of Cloud Computing.pptxUnit I Fundamentals of Cloud Computing.pptx
Unit I Fundamentals of Cloud Computing.pptx
 
Unit II Cloud Delivery Models.pptx
Unit II Cloud Delivery Models.pptxUnit II Cloud Delivery Models.pptx
Unit II Cloud Delivery Models.pptx
 
QQ Plot.pptx
QQ Plot.pptxQQ Plot.pptx
QQ Plot.pptx
 
EDA.pptx
EDA.pptxEDA.pptx
EDA.pptx
 
Module III MachineLearningSparkML.pptx
Module III MachineLearningSparkML.pptxModule III MachineLearningSparkML.pptx
Module III MachineLearningSparkML.pptx
 
Unit II Real Time Data Processing tools.pptx
Unit II Real Time Data Processing tools.pptxUnit II Real Time Data Processing tools.pptx
Unit II Real Time Data Processing tools.pptx
 
2.2 Logit and Probit.pptx
2.2 Logit and Probit.pptx2.2 Logit and Probit.pptx
2.2 Logit and Probit.pptx
 
UNIT I Streaming Data & Architectures.pptx
UNIT I Streaming Data & Architectures.pptxUNIT I Streaming Data & Architectures.pptx
UNIT I Streaming Data & Architectures.pptx
 
UNIT I Introduction to NoSQL.pptx
UNIT I Introduction to NoSQL.pptxUNIT I Introduction to NoSQL.pptx
UNIT I Introduction to NoSQL.pptx
 
Practice_Exercises_Files_and_Exceptions.pptx
Practice_Exercises_Files_and_Exceptions.pptxPractice_Exercises_Files_and_Exceptions.pptx
Practice_Exercises_Files_and_Exceptions.pptx
 
blog creation.pdf
blog creation.pdfblog creation.pdf
blog creation.pdf
 
Chapter I.pptx
Chapter I.pptxChapter I.pptx
Chapter I.pptx
 
Polygon Mesh.ppt
Polygon Mesh.pptPolygon Mesh.ppt
Polygon Mesh.ppt
 

Recently uploaded

Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
Vivekanand Anglo Vedic Academy
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 

Recently uploaded (20)

Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 

Practice_Exercises_Data_Structures.pptx

  • 1. Exercise: simple list creation 1 Ask the user to enter an integer, n Create a list of integers from 0 to n-1 Print this list 2 3
  • 3. Exercise: more list creation 1 Ask the user to enter an integer, n Store the square of all the odd numbers less than n in a list Print this list 2 3
  • 4. Solution n =int(input()) result = [] for i in range(1, n, 2): result.append(i*i) print(result)
  • 5. Exercise: tuple creation 1 Ask the user to enter an integer, n Store the square of all the odd numbers less than n Print a tuple of this list 2 3
  • 6. Solution n =int(input()) result = [] for i in range(1, n, 2): result.append(i*i) print(tuple(result))
  • 7. Hint: string to list/tuple Here is an easy way to convert a string to a list of characters. Try this: In []: x =’hello’ In []:print(list(x)) In []:print(tuple(x))
  • 8. Exercise: list of Fibonacci 1 Ask the user to enter an integer, n(≥ 1) 2 Store the first n numbers of the Fibonnaci series in a list Print this list 3
  • 9. Solution n =int(input()) a, b = 0, 1 result = [0] for I in range(n-1): result.append(b) a, b = b, a+b print(result)
  • 10. Exercise: square a list of integers 1 Ask the user to enter a list of integers separated by spaces Convert this into a list of integers but square each element Print this list 2 3 For example, if the user enters 1 2 3 4, print: [1, 4, 9, 16]
  • 11. Solution text =input() result = [] for item in text.split(): x =int(item) result.append(x*x) print(result)
  • 12. Exercise: list of tuples 1 Ask the user to enter a list of integers separated by spaces Convert this into a list of integers but square each element Store the integer and its square in a tuple, put this into a list Print this list 2 3 4 For example, if the user enters 1 2 3 4, print: [(1, 1), (2, 4), (3, 9), (4, 16)]
  • 13. Solution text =input() result = [] for item in text.split(): x =int(item) result.append((x, x*x)) print(result)
  • 14. Hint: iterating over a list of tuples Consider the following code: In []: data = [(1, 1), (2, 4), (3, 9), (4, 16)] We can iterate over this as follows: In []:for x, y in data: .....:print(x, y)
  • 15. Exercise: list methods 1 Ask the user to enter a string Convert this into a list of characters Sort this list in ascending order Now eliminate any repeated values in this list Print this list 2 3 4 5 For example, if the user enters hello, print: [’e’,’h’,’l’,’o’]
  • 16. Solution text =input() chars =list(text) chars.sort() result = [] for c in chars: if c not in result: result.append(c) print(result)
  • 17. Another solution text =input() chars =set(text) chars =list(chars) chars.sort() print(chars)
  • 19. Exercise: simple dictionaries 1 Ask the user for a list of integers separated by spaces For each integer, store the string version as the key and the square of the integer value as the value in a dictionary. Print the resulting dictionary 2 3 For example if the user enters "1 3 5", print: {’1’: 1,’3’: 9,’5’: 25} Hint: simply print the resulting dictionary
  • 20. Solution text =input() d = {} for item in text.split(): x =int(item) d[item] = x*x print(d)
  • 21. Exercise: mapping using dicts 1 Create a mapping from 3 character month name to month number 2 Hint: for example {’jan’: 1, ’dec’: 12} Ask the user for a 3 character month code lower/upper mixed Print the month number corresponding to the month the user entered. 3 4 For example if the user enters "Jul", print: 7
  • 22. Solution months = (’janfebmaraprmayjunjul’+ ’augsepoctnovdec’).split() month2mm = {} for i in range(len(months)): month2mm[months[i]] = i + 1 text =input() mon = text[:3].lower() print(month2mm[mon])
  • 23. Aside: using enumerate example for i, char in enumerate(’hello’): print(i, char)
  • 24. Aside: using enumerate months = (’janfebmaraprmayjunjul’+ ’augsepoctnovdec’).split() month2mm = {} for i, month in enumerate(months): month2mm[month] = i + 1 #Is easier/nicer than this: for I inrange(len(months)): month2mm[months[i]] = i + 1
  • 25. Exercise: dictionaries 1 Ask the user to enter a string Convert this to lower case Count the number of occurrences of each character in the string Hint: use a dict Print the result in sorted order of the characters 2 3 4 5 For example, if the user enters helloo, print: e 1 h 1 l 2 o 2
  • 26. Solution text =input().lower() result = {} for char in text: if char in result: result[char] += 1 else: result[char] = 1 for char in sorted(result): print(char, result[char])
  • 27. Problem: datestring to date tuple You are given date strings of the form “29 Jul, 2009”, or “4 January 2008”. In other words a number, a string and another number, with a comma sometimes separating the items. Write a program that takes such a string as input and prints a tuple (yyyy, mm, dd) where all three elements are ints.
  • 28. Solution months = (’janfebmaraprmayjunjul’+ ’augsepoctnovdec’).split() month2mm = {} for i, month in enumerate(months): month2mm[month] = i + 1 date =input() date = date.replace(’,’,’’) day, month, year = date.split() dd, yyyy =int(day),int(year) mon = month[:3].lower() mm = month2mm[mon] print((yyyy, mm, dd)) FOSSEE Team (FOSSEE – IITB) Basic Python 30 / 31
  • 29. That’s all folks! FOSSEE Team (FOSSEE – IITB) Basic Python 31 / 31