Basic Python
Last updated
Last updated
Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Learn & practice GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
list(xrange()) == range() --> Katika python3 range ni xrange ya python2 (siyo orodha bali ni jenereta) Tofauti kati ya Tuple na List ni kwamba nafasi ya thamani katika tuple inampa maana lakini orodha ni thamani zilizopangwa tu. Tuples zina muundo lakini orodha zina mpangilio.
Ili kuinua nambari unatumia: 3**2 (siyo 3^2) Ikiwa unafanya 2/3 inarudisha 1 kwa sababu unagawa ints mbili (nambari nzima). Ikiwa unataka desimali unapaswa kugawa floats (2.0/3.0). i >= j i <= j i == j i != j a na b a au b siyo a float(a) int(a) str(d) ord("A") = 65 chr(65) = 'A' hex(100) = '0x64' hex(100)[2:] = '64' isinstance(1, int) = True "a b".split(" ") = ['a', 'b'] " ".join(['a', 'b']) = "a b" "abcdef".startswith("ab") = True "abcdef".contains("abc") = True "abc\n".strip() = "abc" "apbc".replace("p","") = "abc" dir(str) = Orodha ya mbinu zote zinazopatikana help(str) = Maelezo ya darasa str "a".upper() = "A" "A".lower() = "a" "abc".capitalize() = "Abc" sum([1,2,3]) = 6 sorted([1,43,5,3,21,4])
Join chars 3 * ’a’ = ‘aaa’ ‘a’ + ‘b’ = ‘ab’ ‘a’ + str(3) = ‘a3’ [1,2,3]+[4,5]=[1,2,3,4,5]
Parts of a list ‘abc’[0] = ‘a’ 'abc’[-1] = ‘c’ 'abc’[1:3] = ‘bc’ kutoka [1] hadi [2] "qwertyuiop"[:-1] = 'qwertyuio'
Comments # Maoni ya mstari mmoja """ Maoni ya mistari kadhaa Nyingine """
Loops
t1 = (1,'2,'three') t2 = (5,6) t3 = t1 + t2 = (1, '2', 'three', 5, 6) (4,) = Singelton d = () tuple tupu d += (4,) --> Kuongeza kwenye tuple CANT! --> t1[1] == 'New value' list(t2) = [5,6] --> Kutoka tuple hadi orodha
d = [] tupu a = [1,2,3] b = [4,5] a + b = [1,2,3,4,5] b.append(6) = [4,5,6] tuple(a) = (1,2,3) --> Kutoka orodha hadi tuple
d = {} tupu monthNumbers={1:’Jan’, 2: ‘feb’,’feb’:2}—> monthNumbers ->{1:’Jan’, 2: ‘feb’,’feb’:2} monthNumbers[1] = ‘Jan’ monthNumbers[‘feb’] = 2 list(monthNumbers) = [1,2,’feb’] monthNumbers.values() = [‘Jan’,’feb’,2] keys = [k for k in monthNumbers] a={'9':9} monthNumbers.update(a) = {'9':9, 1:’Jan’, 2: ‘feb’,’feb’:2} mN = monthNumbers.copy() #Nakala huru monthNumbers.get('key',0) #Angalia kama ufunguo upo, Rudisha thamani ya monthNumbers["key"] au 0 kama haipo
Katika set hakuna kurudiwa myset = set(['a', 'b']) = {'a', 'b'} myset.add('c') = {'a', 'b', 'c'} myset.add('a') = {'a', 'b', 'c'} #Hakuna kurudiwa myset.update([1,2,3]) = set(['a', 1, 2, 'b', 'c', 3]) myset.discard(10) #Kama ipo, iondoe, kama sio, hakuna myset.remove(10) #Kama ipo iondoe, kama sio, panda exception myset2 = set([1, 2, 3, 4]) myset.union(myset2) #Thamani ni myset AU myset2 myset.intersection(myset2) #Thamani katika myset NA myset2 myset.difference(myset2) #Thamani katika myset lakini sio katika myset2 myset.symmetric_difference(myset2) #Thamani ambazo hazipo katika myset NA myset2 (sio katika zote) myset.pop() #Pata kipengele cha kwanza cha set na uondoe myset.intersection_update(myset2) #myset = Vipengele katika myset na myset2 myset.difference_update(myset2) #myset = Vipengele katika myset lakini sio katika myset2 myset.symmetric_difference_update(myset2) #myset = Vipengele ambavyo havipo katika zote
Njia katika __It__ itakuwa ile itakayotumika na sort kulinganisha kama kitu cha darasa hili ni kikubwa kuliko kingine
Map ni kama: [f(x) for x in iterable] --> map(tutple,[a,b]) = [(1,2,3),(4,5)] m = map(lambda x: x % 3 == 0, [1, 2, 3, 4, 5, 6, 7, 8, 9]) --> [False, False, True, False, False, True, False, False, True]
zip inasimama wakati mfupi wa foo au bar inasimama:
Lambda inatumika kufafanua kazi (lambda x,y: x+y)(5,3) = 8 --> Tumia lambda kama kazi sorted(range(-5,6), key=lambda x: x** 2) = [0, -1, 1, -2, 2, -3, 3, -4, 4, -5, 5] --> Tumia lambda kupanga orodha m = filter(lambda x: x % 3 == 0, [1, 2, 3, 4, 5, 6, 7, 8, 9]) = [3, 6, 9] --> Tumia lambda kuchuja reduce (lambda x,y: x*y, [1,2,3,4]) = 24
mult1 = [x for x in [1, 2, 3, 4, 5, 6, 7, 8, 9] if x%3 == 0 ]
Ikiwa hali si ya kweli, maandiko yataonyeshwa kwenye skrini.
Generator, badala ya kurudisha kitu, "hutoa" kitu. Unapokifikia, kitarejesha thamani ya kwanza iliyozalishwa, kisha, unaweza kukifikia tena na kitarejesha thamani inayofuata iliyozalishwa. Hivyo, thamani zote hazizalishwi kwa wakati mmoja na kumbukumbu nyingi zinaweza kuokolewa kwa kutumia hii badala ya orodha yenye thamani zote.
g = myGen(6) --> 6 next(g) --> 7 next(g) --> Hitilafu
import re re.search("\w","hola").group() = "h" re.findall("\w","hola") = ['h', 'o', 'l', 'a'] re.findall("\w+(la)","hola caracola") = ['la', 'la']
Maana Maalum: . --> Kila kitu \w --> [a-zA-Z0-9_] \d --> Nambari \s --> Karakteri ya Nafasi[ \n\r\t\f] \S --> Karakteri isiyo na nafasi ^ --> Anza na $ --> Maliza na + --> Moja au zaidi * --> 0 au zaidi ? --> Matukio 0 au 1
Chaguo: re.search(pat,str,re.IGNORECASE) IGNORECASE DOTALL --> Ruhusu nukta kuendana na newline MULTILINE --> Ruhusu ^ na $ kuendana katika mistari tofauti
re.findall("<.*>", "<b>foo</b>and<i>so on</i>") = ['<b>foo</b>and<i>so on</i>'] re.findall("<.*?>", "<b>foo</b>and<i>so on</i>") = ['<b>', '</b>', '<i>', '</i>']
IterTools product from itertools import product --> Inazalisha mchanganyiko kati ya orodha 1 au zaidi, labda ikirudia thamani, bidhaa ya cartesian (sifa ya usambazaji) print list(product([1,2,3],[3,4])) = [(1, 3), (1, 4), (2, 3), (2, 4), (3, 3), (3, 4)] print list(product([1,2,3],repeat = 2)) = [(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]
permutations from itertools import permutations --> Inazalisha mchanganyiko wa wahusika wote katika kila nafasi print list(permutations(['1','2','3'])) = [('1', '2', '3'), ('1', '3', '2'), ('2', '1', '3'),... Mchanganyiko wote unaowezekana print(list(permutations('123',2))) = [('1', '2'), ('1', '3'), ('2', '1'), ('2', '3'), ('3', '1'), ('3', '2')] Mchanganyiko wote unaowezekana wa urefu 2
combinations from itertools import combinations --> Inazalisha mchanganyiko wote unaowezekana bila kurudia wahusika (ikiwa "ab" ipo, haiwezi kuunda "ba") print(list(combinations('123',2))) --> [('1', '2'), ('1', '3'), ('2', '3')]
combinations_with_replacement from itertools import combinations_with_replacement --> Inazalisha mchanganyiko wote unaowezekana kuanzia wahusika (kwa mfano, ya tatu inachanganywa kuanzia ya tatu lakini si na ya pili au ya kwanza) print(list(combinations_with_replacement('1133',2))) = [('1', '1'), ('1', '1'), ('1', '3'), ('1', '3'), ('1', '1'), ('1', '3'), ('1', '3'), ('3', '3'), ('3', '3'), ('3', '3')]
Mwapambo unaopima muda ambao kazi inahitaji kutekelezwa (kutoka hapa):
Ikiwa utaikimbia, utaona kitu kama ifuatavyo:
Jifunze na fanya mazoezi ya AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Jifunze na fanya mazoezi ya GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)