Python

From Network Security Wiki
Revision as of 18:51, 16 July 2017 by Amanjosan2008 (talk | contribs) (page created)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


list=[1,2,3] string="Hello" int=23


Here are some basic argument specifiers you should know:

%s - String (or any object with a string representation, like numbers) %d - Integers %f - Floating point numbers %.<number of digits>f - Floating point numbers with a fixed amount of digits to the right of the dot. %x/%X - Integers in hex representation (lowercase/uppercase)


Indentation is Mandatory: num=12 if num>5:

print("Number is greater than 5")
if num<20:
  print("Number is lesser than 20")

print("Program Ended")


Operator Precedence:



num=9 if num==5:

print("Number is 5")

elif num ==11:

print("Number is 11")

elif num==7:

print("Number is 7")

else:

print("Number isn't 5,11,7")


AND = returns true only if both are true

>>> 1 == 1 and 2==2 True >>> 1 == 1 and 2==3 False >>> 1 != 1 and 2==2 False >>> 1 == 1 and 2==2 True

OR = returns true if either of both arguments are true; false if both are false.


>>> 1 != 1 or 2==2 True >>> 1 == 1 or 2==2 True >>> 1 == 1 or 2==3 True


While Loop: runs more than once, till condition is true, once condition is false, next section of code is executed:

i=1 while i<=500:

print(i)
i=i+1

print("Finished !")


Infinity while loop: condition always remain True:

while 1==1:

print('In a loop..!!')


Break the infinity Loop:

i=0 while 1==1:

print(i)
i=i+1
if i>=25:
 print('Breaking!')
 break

print("Finished!")


Continue: this jumps back to top of the loop, rather than stopping it

i=0 while True:

i=i+1
if i==10:
 print('Skip 10')
 continue
if i==25:
 print("Breaking")
 break
print(i)

print("Finished!")


Lists:

words=["Hello","World","!"] print(words[0]) print(words[1]) print(words[2])


Mixed List:

number=3 things=['string',0,[1,2,number],4,56] print(things[1]) print(things[2]) print(things[2][2])


List Operators:

nums = [1,2,3] print(nums+[4,5,6]) print(nums*3)


in operator:

words=['spam',"egg"] print("spam" in words) > True print("tomato" in words) > False print (not "tomato" in words) > True print ("tomato" not in words) > True


Append to the list:

nums=[1,2,3] nums.append(4) print(nums)


Len function:

nums=[1,3,5,2,4] print(len(nums))

unlike append, len is a normal function rather than a method. Therefore it is written before the list it is being called on, without a dot.


Insert Method: similar to append, but allows to add at any position in the list

words=["Python","fun"] index=1 words.insert(index,"is") print(words)