Upcoming SlideShare
×

# جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

308 views

Published on

در این جلسه از کلاس به ساختار های داده
Set, Tuple, Dictionary
پرداختیم
PySec101 Fall 2013 J3E1 By Mohammad Reza Kamalifard
Sets,Tuples and Dictionary Data Types in Python

Published in: Education, Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
308
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
0
0
Likes
0
Embeds 0
No embeds

No notes for slide

### جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

1. 1. Python for ethical hackers Mohammad reza Kamalifard kamalifard@datasec.ir
2. 2. Python language essentials Module 1: Introduction to Python and Setting up an Environment for Programing Part 3 : Lists,Tuple, Sets, Dictionaries
3. 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']
4. 4. Lists my_list = [1,2,3,4] my_list= [1, 'reza', 'PYSEC101', 2.5] >>>my_list= [1, [3,4, 'Hello'], [3,4], 2, 3] >>>len(my_list) 5 >>>len(my_list[1]) 3 >>>
5. 5. List Operations • Concatenate [1,2] + [3,4] = [1,2,3,4] • Append – list.append() • Extend – list.extend() • Reverse – list.reverse() • Pop – list.pop() • Insert – list.insert(index,item) • Delete – del list[index]
6. 6. >>> >>> >>> >>> [1, >>> >>> [1, >>> >>> [1, >>> >>> [1, >>> my_list1 = [1, 2] my_list2 = [3, 4] my_list = my_list1 + my_list2 my_list 2, 3, 4] my_list.append(5) my_list 2, 3, 4, 5] my_list.append([6, 7]) my_list 2, 3, 4, 5, [6, 7]] my_list.extend([8, 9]) my_list 2, 3, 4, 5, [6, 7], 8, 9]
7. 7. >>> [1, >>> 9 >>> [1, >>> >>> [1, >>> >>> [1, >>> my_list 2, 3, 4, 5, [6, 7], 8, 9] my_list.pop() my_list 2, 3, 4, 5, [6, 7], 8] my_list.insert(2, 12) my_list 2, 12, 3, 4, 5, [6, 7], 8] del my_list[2] my_list 2, 3, 4, 5, [6, 7], 8]
8. 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 >>>
9. 9. >>> post_data = tuple(['PYSEC101', 25, 100, 3]) >>> post_data ('PYSEC101', 25, 100, 3) >>> sequence unpacking >>> post_name, view, comments, upvotes = post_data >>> post_name 'PYSEC101' >>> >>> view 25 >>> comments 100 >>> upvotes 3
10. 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
11. 11. Sets >>> set1 = >>> set1 set([1, 2, >>> >>> set2 = 'Reza', 2, >>> set2 set([1, 2, >>> >>> set3 = >>> set3 set([3, 4, set([1, 2, 3, 3, 2]) 3]) set(['Hamid', 'Ali', 'Reza', 'Hamid', 5, 2, 1, 3, 5]) 3, 5, 'Ali', 'Reza', 'Hamid']) set([3, 4, 5]) 5])
12. 12. Sets >>> set1 | set3 set([1, 2, 3, 4, 5]) >>> >>> set1 & set3 set([3]) >>> >>> set1 - set3 set([1, 2]) >>> >>> set3 - set1 set([4, 5])
13. 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. 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. 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]
16. 16. Dictionaries >>> user.keys() ['age', 'from', 'name'] >>> user.values() [23, 'Iran', 'reza'] >>> user.items() [('age', 23), ('from', 'Iran'), ('name', 'reza')] >>> >>> user.get('age') 23 >>> user['age'] 23
17. 17. Dictionaries Add new Value >>> user['Fav_Site'] = 'Twitter.com' >>> user {'age': 23, 'from': 'Iran', 'name': 'reza', 'Fav_Site': 'Twitter.com'} >>> Delete an item >>> del user['Fav_Site'] >>> user {'age': 23, 'from': 'Iran', 'name': 'reza'} >>> >>> user.clear() >>> user {}
18. 18. Getting Help • Getting Help on Methods and etc. • dir() : lists all attributes • help(string.replace) – list method help
19. 19. dir() >>> name = 'reza' >>> dir(name) ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'] >>>
20. 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. 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