Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

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

در این جلسه از کلاس به ساختار های داده
Set, Tuple, Dictionary
پرداختیم

Mohammad reza Kamalifard

November 18, 2013
Tweet

More Decks by Mohammad reza Kamalifard

Other Decks in Education

Transcript

  1. Python language essentials Module 1: Introduction to Python and Setting

    up an Environment for Programing Part 3 : Lists,Tuple, Sets, Dictionaries
  2. 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']
  3. 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 >>>
  4. 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]
  5. >>> my_list1 = [1, 2] >>> my_list2 = [3, 4]

    >>> my_list = my_list1 + my_list2 >>> my_list [1, 2, 3, 4] >>> my_list.append(5) >>> my_list [1, 2, 3, 4, 5] >>> my_list.append([6, 7]) >>> my_list [1, 2, 3, 4, 5, [6, 7]] >>> my_list.extend([8, 9]) >>> my_list [1, 2, 3, 4, 5, [6, 7], 8, 9] >>>
  6. >>> my_list [1, 2, 3, 4, 5, [6, 7], 8,

    9] >>> my_list.pop() 9 >>> my_list [1, 2, 3, 4, 5, [6, 7], 8] >>> my_list.insert(2, 12) >>> my_list [1, 2, 12, 3, 4, 5, [6, 7], 8] >>> del my_list[2] >>> my_list [1, 2, 3, 4, 5, [6, 7], 8] >>>
  7. 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 >>>
  8. >>> 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
  9. 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
  10. Sets >>> set1 = set([1, 2, 3, 3, 2]) >>>

    set1 set([1, 2, 3]) >>> >>> set2 = set(['Hamid', 'Ali', 'Reza', 'Hamid', 'Reza', 2, 5, 2, 1, 3, 5]) >>> set2 set([1, 2, 3, 5, 'Ali', 'Reza', 'Hamid']) >>> >>> set3 = set([3, 4, 5]) >>> set3 set([3, 4, 5])
  11. Sets >>> set1 | set3 set([1, 2, 3, 4, 5])

    >>> >>> set1 & set3 set([3]) >>> >>> set1 - set3 set([1, 2]) >>> >>> set3 - set1 set([4, 5])
  12. 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'}
  13. 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
  14. 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]
  15. 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
  16. 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 {}
  17. Getting Help • Getting Help on Methods and etc. •

    dir() : lists all attributes • help(string.replace) – list method help
  18. 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'] >>>
  19. 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)
  20. 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
  21. 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 .