User Tools

Site Tools

tools:python

This is an old revision of the document!


This article was imported from the 2011 Insurgency Wiki backup, and sits here in its original crude format. It is in dire need of modernization and cleanup.

Python

Below applies to Python 2.6 or lower, Python 3 is a good bit different.

Python is an object oriented, interpreted language. It is unique in that it requires a user to indent in place of using curly brackets ({..}).

Note: that most *nix distributions come with Python by default.

Basics

Basic python syntax

Hello World!

>>> print "HELLO WORLD!"
HELLO WORLD!

Variables

Variable names DO NOT need to begin with a special character, unlike PHP. Variable names CAN, however, be any combination, as long as the word isn't reserved (Ex: and, or, print), of letters and numbers.

>>> #Numbers
>>> a = 1 #Declare A as 1
>>> b = a+5 #b becomes 6, while a remains 1
>>> a += 5 #a is overwritten, and becomes 6
>>> c = b = a #c and b are overwritten and become 6
>>> #Strings
>>> a = "Hello "
>>> b = "There"
>>> c = a+b #JOIN STRINGS
>>> a = "Blue Yellow Green Red".split(" ") #Split the string at every " "
>>> a #Output 'a' to the screen, same as print or print()
['Blue', 'Yellow', 'Green', 'Red'] #List object, another type of variable
>>> .join(a) #Join all the list objects as one
'BlueYellowGreenRed'
>>> a = 'Blue Green Yellow Red'[0:4] #This returns the sub-string 'Blue'
>>> a = 'Blue Green Yellow Red'[5:] #This returns the sub-string 'Green Yellow Red'
>>> a = 'a = 'Blue Green Yellow Red'[::2] #This returns the sub-string Bu re elwRd'
>>> #Float 
>>> a = 88.2
>>> b = 88
>>> print a/25
3.528
>>> print b/25
3
>>> #Other types
>>> a = (" ", 355, 256.7, ["Hello", "World"])
>>> type(a)
<type 'tuple'>
>>> a = {"Hello":"World", "Key":"Value"}
>>> type(a)
<type 'dict'>
>>> a = "Anhero"
>>> del a #Its a good idea, if you're making a 'hidden' script, to delete variables after you use them...

Statements

Comparison

<       Less than
>       Greater than
==      Equal to
!=      Not equal
is      Identical
is not  Not identical
<=      Less than or equal to
>=      Greater than or equal to

If

>>> a = 1
>>> b = 2
>>> if a != b:
	print "No!"

No!

>>> a = 1
>>> b = 1
>>> if a == b:
	print "Yes!"

	
Yes!
>>> a = 1
>>> b = 5
>>> if (a == b-len("....")/1) and "a" == "a" or "b" == "b":
	print "Yes!"

Yes!

Loops

While

>>> a = 1
>>> while a < 5: #Note, replacing '<' with '<=' allows it to reach 5, instead of stopping at 4
print a
a += 1;

1
2
3
4
>>> a = False;
>>> while a == False:
print "False"
a = True;
 
	
False

For

>>> for x in range(0, 10):
	print x

	
0
1
2
3
4
5
6
7
8
9

consider using xrange when dealing with larger lists, as it creates an iterator, rather than constructing the full list in memory

>>> for x in ['Hello', 'world', ',this', 'is', 'a', 'list']:
print x

 	
Hello
world
,this
is
a
list
tools/python.1717394033.txt.gz ยท Last modified: 2024/08/06 05:52 (external edit)

Find this page online at: https://bestpoint.institute/tools/python