Basic Python
Last updated
Last updated
Jifunze na zoea AWS Hacking:Mafunzo ya HackTricks AWS Timu Nyekundu Mtaalam (ARTE) Jifunze na zoea GCP Hacking: Mafunzo ya HackTricks GCP Timu Nyekundu Mtaalam (GRTE)
list(xrange()) == range() --> Katika python3 range ni xrange ya python2 (siyo orodha bali jenereta) Tofauti kati ya Tuple na Orodha ni kwamba nafasi ya thamani katika tuple inampa maana lakini orodha ni thamani zilizopangwa tu. Tuples zina miundo lakini orodha zina utaratibu.
Kuongeza nambari unatumia: 3**2 (siyo 3^2) Ikiwa unafanya 2/3 inarudisha 1 kwa sababu unagawanya ints (namba kamili). Ikiwa unataka desimali unapaswa kugawanya floats (2.0/3.0). i >= j i <= j i == j i != j a na b a au b si a float(a) int(a) str(d) ord("A") = 65 chr(65) = 'A' hex(100) = '0x64' hex(100)[2:] = '64' isinstance(1, int) = Kweli "a b".split(" ") = ['a', 'b'] " ".join(['a', 'b']) = "a b" "abcdef".startswith("ab") = Kweli "abcdef".contains("abc") = Kweli "abc\n".strip() = "abc" "apbc".replace("p","") = "abc" dir(str) = Orodha ya njia zote zilizopo help(str) = Maelezo ya darasa la str "a".upper() = "A" "A".lower() = "a" "abc".capitalize() = "Abc" sum([1,2,3]) = 6 sorted([1,43,5,3,21,4])
Jumuisha herufi 3 * ’a’ = ‘aaa’ ‘a’ + ‘b’ = ‘ab’ ‘a’ + str(3) = ‘a3’ [1,2,3]+[4,5]=[1,2,3,4,5]
Sehemu za orodha ‘abc’[0] = ‘a’ 'abc’[-1] = ‘c’ 'abc’[1:3] = ‘bc’ kutoka [1] hadi [2] "qwertyuiop"[:-1] = 'qwertyuio'
Maoni # Maoni ya mstari mmoja """ Maoni ya mistari kadhaa Nyingine """
Mizunguko
t1 = (1,'2,'three') t2 = (5,6) t3 = t1 + t2 = (1, '2', 'three', 5, 6) (4,) = Singelton d = () tupu tuple d += (4,) --> Kuongeza kwenye tuple CANT! --> t1[1] == 'Thamani mpya' list(t2) = [5,6] --> Kutoka kwenye tuple kwenda kwenye 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 kwenye orodha kwenda kwenye 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 ikiwa funguo ipo, Rudi thamani ya monthNumbers["funguo"] au 0 ikiwa haipo
Kwenye seti hakuna kurudia myset = set(['a', 'b']) = {'a', 'b'} myset.add('c') = {'a', 'b', 'c'} myset.add('a') = {'a', 'b', 'c'} #Hakuna kurudia myset.update([1,2,3]) = set(['a', 1, 2, 'b', 'c', 3]) myset.discard(10) #Ikiwepo, iondoe, ikiwa la, usifanye kitu myset.remove(10) #Ikiwepo iondoe, ikiwa la, italeta kosa myset2 = set([1, 2, 3, 4]) myset.union(myset2) #Thamani za myset AU myset2 myset.intersection(myset2) #Thamani zilizo kwenye myset NA myset2 myset.difference(myset2) #Thamani zilizo kwenye myset lakini sio kwenye myset2 myset.symmetric_difference(myset2) #Thamani ambazo sio kwenye myset NA myset2 (sio kwenye zote mbili) myset.pop() #Pata kipengele cha kwanza cha seti na iondoe myset.intersection_update(myset2) #myset = Elementi zilizo kwenye myset na myset2 myset.difference_update(myset2) #myset = Elementi zilizo kwenye myset lakini sio kwenye myset2 myset.symmetric_difference_update(myset2) #myset = Elementi ambazo sio kwenye zote mbili
Mbinu katika __It__ itatumika na sort kulinganisha ikiwa kitu cha darasa hili ni kikubwa kuliko kingine
Ramani ni kama: [f(x) kwa x katika iterable] --> ramani(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 inakoma wakati mfupi kati ya foo au bar inapokoma:
Lambda hutumika kufafanua kazi (lambda x,y: x+y)(5,3) = 8 --> Tumia lambda kama kazi rahisi 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 kwa x katika [1, 2, 3, 4, 5, 6, 7, 8, 9] kama x%3 == 0 ]
Ikiwa hali ni ya uwongo, kamba itachapishwa kwenye skrini
Mjenzi, badala ya kurudisha kitu, inatoa kitu. Unapopata, ita "rudisha" thamani ya kwanza iliyozalishwa, kisha, unaweza kuipata tena na itarudisha thamani inayofuata iliyozalishwa. Kwa hivyo, thamani zote hazizalishwi wakati mmoja na kiasi kikubwa cha kumbukumbu kinaweza kuokolewa kwa kutumia hii badala ya orodha na thamani zote.
Ukiikimbia, utaona kitu kama hiki:
Jifunze na zoea AWS Hacking:Mafunzo ya HackTricks AWS Timu Nyekundu Mtaalam (ARTE) Jifunze na zoea GCP Hacking: Mafunzo ya HackTricks GCP Timu Nyekundu Mtaalam (GRTE)