1.22k likes | 1.33k Views
>>> print 'Hello, World!'. Hello, World!. >>>. >>> print 'Welcome to PYSEC101 Course'. Welcome to PYSEC101 Course. >>>. >>> 'Hello World'. 'Hello World'. >>> 3. 3. >>>. >>> 12 / 7. 1. >>>. >>> 12.0 / 7. 1.7142857142857142. >>>. >>> float ( 12 ) / 7.
E N D
>>> print 'Hello, World!' Hello, World! >>> >>> print 'Welcome to PYSEC101 Course' Welcome to PYSEC101 Course >>> >>> 'Hello World' 'Hello World' >>> 3 3 >>>
>>> 12 / 7 1 >>> >>> 12.0 / 7 1.7142857142857142 >>> >>> float(12) / 7 1.7142857142857142 >>>
>>> 3 + 2 * 4 11 >>> ( 3 + 2 ) * 4 20 >>> >>> _ 20 >>>
$ vim Hello.py print 'Hello, World!' ~ ~ ~ ~ ~ ~ ~ $ python Hello.py Hello, World!
>>> name = 'reza' >>> print name reza >>> >>>name = 10 >>>name 10
>>> name = 10 >>> type (name) <type 'int'> >>> name = 'Reza' >>> type (name) <type 'str'> >>> name = 10.42 >>> type (name) <type 'float'> >>>
>>> name = 'reza' >>> id(name) 3072831328L >>> hex(id(name)) '0xb727af60L' >>> >>> name.__repr__ <method-wrapper '__repr__' of str object at 0xb727af60> >>> 0xb727af60
>>> a = 010 >>> print a 8 >>> hex_number = 0xA0F >>> print hex_number 2575
>>>int('10') 10 >>>int('0011', 2) 3 >>>int('1111', 2) 15 >>>int('0xff', 16) 255
>>>name = 'reza' >>>name = reza >>>name = rez'a >>>name = 'Mohammd\nreza' >>>print name Mohammad reza
>>>name = r'Mohammd\nreza' print name Mohammd\nreza >>>
>>> name = ''' ... Hello Dear Students ... Welcome to PYSEC101 Course! ... Python Scripting Course for Ethical Hackers ... ''' >>> name '\nHello Dear Students\nWelcome to PYSEC101 Course!\nPython Scripting Course for Ethical Hackers\n' >>> print name Hello Dear Students Welcome to PYSEC101 Course! Python Scripting Course for Ethical Hackers
>>>name = u'Mohammad' >>> name u'Mohammad' >>> >>> str(name) 'Mohammad' >>> >>> unicode(name) u'Mohammad' >>>
>>>s1 + s2 >>> s1 = 'Hamid' >>> s2 = 'rezaie' >>> s1 + s2 'Hamidrezaie' >>> s1 + ' ' + s2 'Hamid rezaie' >>>
>>> buffer = 'A' * 20 >>> buffer 'AAAAAAAAAAAAAAAAAAAA' >>>
>>> name = 'Mohammad reza' >>> name[5:10] 'mad r' >>> name[0:10] 'Mohammad r' >>> name[0:-1] 'Mohammad rez' >>> name[0:-5] 'Mohammad' >>> name[:] 'Mohammad reza' >>> name[::-1] 'azer dammahoM' >>> name[::2] 'Mhma ea' >>>
>>>name = 'reza' >>>name[ 0] r 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 = 'reza' name = 'Mohammad'
>>> name = 'mohammad reza' >>> name.find('PYSEC101') -1 >>> name.find('mma') 4 >>> >>> name.split() ['mohammad', 'reza'] >>> name.split('a') ['moh', 'mm', 'd rez', ''] >>> >>> name.replace('m', 'H') 'HohaHHad reza' >>>
>>> 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 '
>>> 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']
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 >>>
>>> 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] >>>
>>> 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] >>>
>>>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
>>> 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])
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_keyUnordered key-value pairs • Keys are unique and immutable objects • Value can change • Check if a given key is present • dict.has_keyy(key • keyindict { 'key' : 'value' } >>>my_dic1 = {} >>>my_dic1['name'] = 'reza' {'name': 'reza'} >>> my_dic2 = {'name' : 'reza', 'age' : 23} >>> my_dic2 {'age': 23, 'name': 'reza'}
>>> 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 >>> 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
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 {}
Help
() >>> 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'] >>>
()