Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

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

440 views

Published on

Python for Ethical Hacker Week2

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

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

  1. 1. Python for Ethical Hackers Mohammad reza Kamalifard
  2. 2. Python Language Essentials Part 2 : Variables and Data Types © Mohammad reza Kamalifard
  3. 3. Unicode String Regular String = ASCII Unicode Used for internationalization Contains most of the world's writing systems. >>>name = u'Mohammad' >>> name u'Mohammad' >>> >>>type(name) <type 'unicode'> unicode to regular string conversion >>> str(name) 'Mohammad' >>> regular string to unicode conversion >>> unicode(name) u'Mohammad' >>> © Mohammad reza Kamalifard
  4. 4. Concatenating Strings String1 + String2 + String3 + ... >>> s1 = 'Hamid' >>> s2 = 'rezaie' >>> s1 + s2 'Hamidrezaie' >>> s1 + ' ' + s2 'Hamid rezaie' >>> © Mohammad reza Kamalifard
  5. 5. >>> buffer = 'A' * 20 >>> buffer 'AAAAAAAAAAAAAAAAAAAA' >>> Int to string >>> 'a' + 42 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: cannot concatenate 'str' and 'int' objects >>> str(42) '42' >>>>'a' + str(42) 'a42' © Mohammad reza Kamalifard
  6. 6. Stirngs are immutable objects >>>name = 'reza' >>>name[0] r >>>name[3] A name [0] = 'a' TypeError: 'str' object does not support item assignment You can not change String Object directly in memory because they are immutable name = 'Mohammad' 'reza' object still does exists now name is referenced to 'Mohammad' object © Mohammad reza Kamalifard
  7. 7. Slicing – breaking up the string string[start:end:steps](from start to end and contains end) >>> name = 'Mohammad reza' >>> name[5:10] 'mad r' >>> name[:10] 'Mohammad r' >>> name[:-1] 'Mohammad rez' >>> name[:-5] 'Mohammad' >>> name[:] 'Mohammad reza' >>> name[::-1] 'azer dammahoM' >>> name[::2] 'Mhma ea' >>> © Mohammad reza Kamalifard
  8. 8. >>> reply = 'ip address of your system is 192.168.1.1' >>> reply 'ip address of your system is 192.168.1.1' >>> reply[3:10] + ' ' + reply[-11:] 'address 192.168.1.1' © Mohammad reza Kamalifard
  9. 9. >>>foo = “reza” ( foo is a reference to string object in memory address which contains “reza”) >>>bar = foo (bar is a reference to string object in memory address which contains “reza” and not related to foo) >>>bar “reza” >>>foo “reza” >>>foo = 'Mohammad' >>>foo 'Mohammad' >>>bar “reza” © Mohammad reza Kamalifard
  10. 10. Concatenating Strings >>> first_name = 'Mohammadreza' >>> last_name = 'Kamalifard' >>> first_name + last_name 'MohammadrezaKamalifard' >>> >>> first_name + ' ' + last_name 'Mohammadreza Kamalifard' Garbage Collection: Once any object is no longer referenced by a variable it is typically automatically freed up in language like C you have to manually use free to free up... © Mohammad reza Kamalifard
  11. 11. String Methods string.find() string.replace() string.split() string.upper() string.lower() © Mohammad reza Kamalifard
  12. 12. String Methods >>> name = 'mohammad reza' >>> name.find('PYSEC101') -1 >>> name.find('mma') 4 >>> >>> name.split() ['mohammad', 'reza'] by default split on white spaces and return list of strings. split on 'a' >>> name.split('a') ['moh', 'mm', 'd rez', ''] >>> >>> name.replace('m' , 'H') 'HohaHHad reza' >>> © Mohammad reza Kamalifard
  13. 13. String Formatting >>> ip = '192.168.1.252' >>> line = 'Crack this IP :%s' % ip >>> line 'Crack this IP :192.168.1.252' >>> >>> line = 'Crack this IP : %s and name %s ' % (ip, 'Reza-PC') >>> line 'Crack this IP : 192.168.1.252 and name Reza-PC ' © Mohammad reza Kamalifard
  14. 14. Numbers Integers,Floats etc Operators +,-,*,/ x**y(x to the power y) (>,=,<,>=,<=,==) x|y(OR) , x^y(XOR),x&y(AND) (bitwise operators) x and y , x or y , not x (logical operators) © Mohammad reza Kamalifard
  15. 15. Numbers >>>2+2 4 Number is base 2 >>int ('0011',2) 3 >>> int('1111',2) 15 >>> int('0xff',16) 255 © Mohammad reza Kamalifard
  16. 16. Lists Lists are Collection of objects which can be heterogeneous. >>> my_list1 = [1,2,3,4] >>> my_list1 [1, 2, 3, 4] >>>len(my_list1) 4 >>> my_list2 = [1, 'reza', 'PYSEC101', 2.5] >>> my_list2[2] 'PYSEC101' >>> >>>my_list3= [1, [3,4, 'Hello'], [3,4], 2, 3] >>> len(my_list3) 5 >>> my_list3[1] [3, 4, 'Hello'] © Mohammad reza Kamalifard
  17. 17. Nested lists >>> my_list = [1, 2, [3, 4]] >>> my_list [1, 2, [3, 4]] >>> >>>len(my_list) 3 >>> Error: 'int' has no len() >>>len(my_list[2]) 2 © Mohammad reza Kamalifard
  18. 18. List Operations Concatenate [1,2] + [3,4] = [1,2,3,4] Append – list.append() Extend – list.extend() Reverse – list.reverse() Pop – list.pop(index) default index is last item Insert – list.insert(index,item) Delete – del list[index] © Mohammad reza Kamalifard
  19. 19. Lists Operation >>> my_list = [1, 2, 3] >>> my_list [1, 2, 3] >>> my_list.append(4) >>> my_list [1, 2, 3, 4] >>> my_list.pop() >>>my_list [1, 2, 3] >>> my_list.reverse() >>> my_list [3, 2, 1] >>> © Mohammad reza Kamalifard
  20. 20. Append >>> my_list = [1, 2, 3] >>> my_list [1, 2, 3] >>> my_list.append([4,5]) >>> my_list [1, 2, 3, [4, 5]] >>> © Mohammad reza Kamalifard
  21. 21. Extend >>> my_list = [1, 2, 3] >>> my_list [1, 2, 3] >>> my_list.extend([4,5]) >>> my_list [1, 2, 3, 4, 5] >>> © Mohammad reza Kamalifard
  22. 22. Pop , Insert , Delete list item >>> my_list = [1, 2, 3, 4] >>> my_list [1, 2, 3, 4] >>> my_list.pop(1) 2 >>> my_list [1, 3, 4] >>> my_list.insert(1,2) >>> my_list [1, 2, 3, 4] >>> del my_list[3] >>> my_list [1, 2, 3] >>> © Mohammad reza Kamalifard
  23. 23. References Data model — Python v2.7.5 documentation http://docs.python.org/2/reference/datamodel.html#objects-values-and-types String — Common string operations — Python v2.7.5 documentation http://docs.python.org/2/library/string.html Python Strings http://www.tutorialspoint.com/python/python_strings.htm Strings in Python http://zetcode.com/lang/python/strings/ 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.

×