Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Use a single hashtag/number sign/octothorpe for a single line comment
- """
- Use a triple quote to do multi-line comments or strings
- """
- #There are also different kinds of strings like
- uni = u"unicode string"
- binary = b"binary string"
- string = "normal string"
- '''Python 2 is more relaxed, if you're using python 3, everything is
- default binary strings, need to convert normal strings into them using
- bytes(), 2 just uses string as default and allows concat with binary'''
- print(bytes("normal", "UTF-8") + b" + binary")
- array = ["This", "is", "a", "multi", "element", "array."]
- print(" ".join(array)) #Prettyprints with spaces in between
- [array.append(i) for i in ["You", "can", "append", "values", "to",
- "the", "end,", "too!"]]
- print(" ".join(array)) #Prettyprints with spaces in between
- '''This is called list comprehension, very useful to condense things
- This does n*n(n^2) for all numbers in range(13) which is an array with
- 0-12 in it, and stores it as an array. Basically a one-liner'''
- squares = [n * n for n in range(13)]
- print(squares)
- #You can get individual values from this too!
- print("The square root of 12 is " + str(squares[-1]))
- '''There are also generators that do calculations when called'''
- squares = (n * n for n in range(13))
- print(squares[3]) #Just gets the square root of 3, nothing else
- '''Side note, when doing debug printing, just use strings and str() to
- convert ints and such into printable formats. Also, you can slice
- backwards too, -1 is the last element of the array, and you can do -12.'''
- '''You can also "slice" an array, to get certain chunks within. This
- prints squares of 0-3, saying "end before the element in position 4"'''
- print(squares[0:4])
- #You can import "modules" like this
- import sys, struct
- '''sys.argv lets you get input from the command prompt, seperated by
- spaces. It is an array and you can append to it, too. First variable
- is /always/ the script, so a file'd be sys.argv[1]'''
- sys.argv.append("file.bflim")
- sys.argv.append("256")
- print(sys.argv)
- i = 0 #Makes a new variable
- while True: #Infinite loop
- if i == 10: break #but we can break it!
- else: pass #This will just not do anything, easy for debug
- i+= 1 #Increment one, can use -= and a bunch more, no ++ tho :<
- #Also, you can use Ctrl+C to stop a program when it's infinite looping
- print(i) #Should be 10
- with open("test.txt", "w") as f: #Opens fileas writable
- f.write("TEST\x00\x00\x00\x08")
- with open("test.txt", "rb") as f: #Opens file as read binary
- print(f.read(4)) #"TEST"
- print(struct.unpack("!I", f.read(4))[0])
- '''This unpacks 4 bytes of binary as an int (unpack is a "tuple", use [0])
- There's several modes, < is little endian, > is big, @ is native, wouldn't
- recommend. There's a whole list of values you can convert to, check
- https://docs.python.org/2/library/struct.html#format-characters'''
- '''There's also a bunch of bit manipulation you can do'''
- print(bin(0xBF)) #prints 0b<value>, this is 0b10111111
- '''& compares if both are 1, 0xF is all 1s, lets you get a digit'''
- print(hex(0x42 & 0x0F)) #prints 0x2
- print(hex(0x42 & 0xF0)) #prints 0x40
- '''| is OR, if either is 1 but not both. Lets you combine values'''
- print(hex(0x24 << 8 | 0x68)) #Combines 0x2400 and 0x68, 0x2468
- '''You can "shift" bits left or right for how you need them, << and >>'''
- '''Another useful compare tool is %, lets you get the remainder'''
- print("53 is " + (["even", "odd"][53 % 2])) #Remainder of 0 or 1, 1 is odd
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement