• Save
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
Upcoming SlideShare
Loading in...5
×
 

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

on

  • 202 views

در این جلسه از کلاس به ساختار های داده ...

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

Statistics

Views

Total Views
202
Views on SlideShare
202
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

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

  • Python for ethical hackers Mohammad reza Kamalifard kamalifard@datasec.ir
  • Python language essentials Module 1: Introduction to Python and Setting up an Environment for Programing Part 3 : Lists,Tuple, Sets, Dictionaries
  • 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']
  • 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 >>>
  • 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]
  • >>> >>> >>> >>> [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]
  • >>> [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]
  • 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 >>>
  • >>> 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
  • 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
  • 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])
  • Sets >>> set1 | set3 set([1, 2, 3, 4, 5]) >>> >>> set1 & set3 set([3]) >>> >>> set1 - set3 set([1, 2]) >>> >>> set3 - set1 set([4, 5])
  • 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'}
  • 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
  • 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]
  • 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
  • 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 {}
  • Getting Help • Getting Help on Methods and etc. • dir() : lists all attributes • help(string.replace) – list method help
  • 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'] >>>
  • 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)
  • 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
  • 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 .