در این جلسه از کلاس به ساختار های داده
Set, Tuple, Dictionary
پرداختیم
PySec101 Fall 2013 J3E1 By Mohammad Reza Kamalifard
Talk About :
Sets,Tuples and Dictionary Data Types in Python
Seal of Good Local Governance (SGLG) 2024Final.pptx
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
1. Python for ethical hackers
Mohammad reza Kamalifard
kamalifard@datasec.ir
2. Python language essentials
Module 1:
Introduction to Python and Setting up an Environment for Programing
Part 3 :
Lists,Tuple, Sets, Dictionaries
3. Lists
• Collection of objects which can be heterogeneous
• Lists are like Arrays in C/C++ and Java, but Python lists are by far
more flexible than classical arrays.
>>> my_list = ['English', 'Farsi', 'Arabic', 'German',]
>>> my_list[0]
'English'
>>> my_list[1]
'Farsi'
>>> my_list[-1]
'German'
>>> my_list[2:3]
['Arabic']
>>> my_list[::-1]
['German', 'Arabic', 'Farsi', 'English']
8. Tuples
• Tuples are similar to list but immutable
• Can Convert from list to tuple and vice versa
• tuple(list)
• list(tuple)
>>>my_tuple = ('reza', 1362, 22, 'aban')
>>>my_tuple
('reza', 1362, 22, 'aban')
>>> my_tuple[2]
22
>>> my_tuple[2] = 14
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
my_tuple[2] = 14
TypeError: 'tuple' object does not support item assignment
>>>
10. Sets
• Unordered collection of unique objects
• List to set : b = set(a)
• Set to list : a = list(b)
• Set Operations
• Union: a | b
• Intersection: a & b
• Difference: a - b
13. Dictionaries
• Unordered key-value pairs
• Keys are unique and immutable objects
• Value can change
• Check if a given key is present
• dict.has_key(key)
• key in dict
{ 'key' : 'value' }
>>>my_dic1 = {}
>>>my_dic1['name'] = 'reza'
{'name': 'reza'}
>>> my_dic2 = {'name' : 'reza', 'age' : 23}
>>> my_dic2
{'age': 23, 'name': 'reza'}
14. Dictionaries
>>> user = {'name': 'reza', 'age': 23, 'from': 'Iran'}
>>> user
{'age': 23, 'from': 'Iran', 'name': 'reza'}
>>>
>>> user.has_key('name')
True
>>> user.has_key('hobby')
False
>>> 'name' in user
True
>>> 'Iran' in user
False
15. Dictionaries
• Get tuple of items : dict.items()
• Get list of keys : dict.keys()
• Get list of values : dict.values()
• Get a particular item : dict.get(key)
• Item deletion:
• All item : dict.clear()
• one item: del dict[key]
20. help()
• Help on built-in function replace:
• help(name.replace)
replace(...)
S.replace(old, new[, count]) -> string
Return a copy of string S with all occurrences of substring
old replaced by new. If the optional argument count is
given, only the first count occurrences are replaced.
(END)
21. References
SPSE securitytube training by Vivek Ramachandran
SANS Python for Pentesters (SEC573)
Violent python
Security Power Tools
python-course.eu
----------------------------http://www.tutorialspoint.com/python/python_lists.htm
http://www.tutorialspoint.com/python/python_tuples.htm
http://www.tutorialspoint.com/python/python_dictionary.htm
http://www.python-course.eu/sequential_data_types.php
http://www.python-course.eu/sets_frozensets.php
22. This work is licensed under the Creative Commons Attribution-NoDerivs 3.0 Unported License.
To view a copy of this license, visit
http://creativecommons.org/licenses/by-nd/3.0/
Copyright 2013 Mohammad Reza Kamalifard
All rights reserved.
Go to Kamalifard.ir/pysec101 to Download Slides and Course martials .