PYTHON FOR ETHICAL HACKERS
Mohammad reza Kamalifard
Python Language Essentials
Part 3 :
Data Types: Tuple, Sets, Dictionaries
Tuples
• Tuples are similar to list but immutable
• Can Convert from list to tuple and vice versa
• tuple(list)
• list(tup...
Tuples
>>> post_data = tuple(['PYSEC101', 25, 100, 3])
>>> video_data
('PYSEC101', 25, 100, 3)
>>>
sequence unpacking
>>> ...
Sets
• Unordered collection of unique objects
• List to set : b = set(a)
• Set to list : a = list(b)
• Set Operations
• Un...
Sets
>>> set1 = set([1, 2, 3, 3, 2])
>>> set1
set([1, 2, 3])
>>>
>>> set2 = set(['Hamid', 'Ali', 'Reza', 'Hamid', 'Reza', ...
Sets
>>> set1 | set3
set([1, 2, 3, 4, 5])
>>>
>>> set1 & set3
set([3])
>>>
>>> set1 - set3
set([1, 2])
>>>
>>> set3 - set1...
Dictionaries
• Unordered key-value pairs
• Keys are unique and immutable objects
• Value can change
• Check if a given key...
Dictionaries
>>> user = {'name': 'reza', 'age': 23, 'from': 'Iran'}
>>> user
{'age': 23, 'from': 'Iran', 'name': 'reza'}
>...
Dictionaries
• Get tuple of items : dict.items()
• Get list of keys : dict.keys()
• Get list of values : dict.values()
• G...
Dictionaries
>>> user.keys()
['age', 'from', 'name']
>>> user.values()
[23, 'Iran', 'reza']
>>> user.items()
[('age', 23),...
Dictionaries
Add new Value
>>> user['Fav_Site'] = 'Twitter.com'
>>> user
{'age': 23, 'from': 'Iran', 'name': 'reza', 'Fav_...
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__', '__form...
help()
• Help on built-in function replace:
• help(name.replace)
replace(...)
S.replace(old, new[, count]) -> string
Retur...
References
• Data model — Python v2.7.5 documentation
• http://docs.python.org/2/reference/datamodel.html#objects-values-a...
‫قانونی‬ ‫هکرهای‬ ‫برای‬ ‫پایتون‬ ‫دوم‬ ‫دوره‬
•‫از‬ ‫قانونی‬ ‫های‬ ‫هکر‬ ‫برای‬ ‫پایتون‬ ‫کالس‬ ‫دوم‬ ‫دوره‬28‫شود‬ ‫می‬ ...
Upcoming SlideShare
Loading in...5
×

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

209

Published on

در این جلسه در مورد
Tuples , Sets , Dictionaries
صحبت می شود و مثال های ارائه می شوند
در این جلسه بخش سوم از کلاس به پایان می رسد

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
209
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

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

  1. 1. PYTHON FOR ETHICAL HACKERS Mohammad reza Kamalifard
  2. 2. Python Language Essentials Part 3 : Data Types: Tuple, Sets, Dictionaries
  3. 3. 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 >>>
  4. 4. Tuples >>> post_data = tuple(['PYSEC101', 25, 100, 3]) >>> video_data ('PYSEC101', 25, 100, 3) >>> sequence unpacking >>> post_name, view, comments, upvotes = post_data >>> post_name 'PYSEC101' >>> >>> view 25 >>> comments 100 >>> upvotes 3
  5. 5. 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
  6. 6. 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])
  7. 7. Sets >>> set1 | set3 set([1, 2, 3, 4, 5]) >>> >>> set1 & set3 set([3]) >>> >>> set1 - set3 set([1, 2]) >>> >>> set3 - set1 set([4, 5])
  8. 8. 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'}
  9. 9. 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
  10. 10. 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)
  11. 11. 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 • Item deletion: • All item : dict.clear() • one item: del dict[key]
  12. 12. 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 {} >>>
  13. 13. Getting Help • Getting Help on Methods and etc. • dir() : lists all attributes • help(string.replace) – list method help
  14. 14. 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'] >>>
  15. 15. 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)
  16. 16. References • Data model — Python v2.7.5 documentation • http://docs.python.org/2/reference/datamodel.html#objects-values-and-types • PEP 8 -- Style Guide for Python Code • http://www.python.org/dev/peps/pep-0008/ THIS IS A COPYRIGHTED MATERIAL ©2013 Mohammad Reza Kamalifard. ALL RIGHTS RESERVED.
  17. 17. ‫قانونی‬ ‫هکرهای‬ ‫برای‬ ‫پایتون‬ ‫دوم‬ ‫دوره‬ •‫از‬ ‫قانونی‬ ‫های‬ ‫هکر‬ ‫برای‬ ‫پایتون‬ ‫کالس‬ ‫دوم‬ ‫دوره‬28‫شود‬ ‫می‬ ‫برگسار‬ ‫ماه‬ ‫مرداد‬. •‫کنید‬ ‫معرفی‬ ‫رو‬ ‫دوره‬ ‫هم‬ ‫دوستانتون‬ ‫به‬ ‫لطفا‬ ‫دونین‬ ‫می‬ ‫مفید‬ ‫رو‬ ‫دوره‬ ‫این‬ ‫اگر‬. -ethical-for-http://www.webamooz.ir/home/?courses=python 1-hackers

×