Advertisement
NWPlayer123

Python Tutorial (v0.1.1)

Dec 7th, 2015
769
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.45 KB | None | 0 0
  1. #Use a single hashtag/number sign/octothorpe for a single line comment
  2. """
  3. Use a triple quote to do multi-line comments or strings
  4. """
  5.  
  6. #There are also different kinds of strings like
  7. uni = u"unicode string"
  8. binary = b"binary string"
  9. string = "normal string"
  10. '''Python 2 is more relaxed, if you're using python 3, everything is
  11. default binary strings, need to convert normal strings into them using
  12. bytes(), 2 just uses string as default and allows concat with binary'''
  13. print(bytes("normal", "UTF-8") + b" + binary")
  14.  
  15. array = ["This", "is", "a", "multi", "element", "array."]
  16. print(" ".join(array)) #Prettyprints with spaces in between
  17. [array.append(i) for i in ["You", "can", "append", "values", "to",
  18.               "the", "end,", "too!"]]
  19. print(" ".join(array)) #Prettyprints with spaces in between
  20.  
  21. '''This is called list comprehension, very useful to condense things
  22. This does n*n(n^2) for all numbers in range(13) which is an array with
  23. 0-12 in it, and stores it as an array. Basically a one-liner'''
  24. squares = [n * n for n in range(13)]
  25. print(squares)
  26. #You can get individual values from this too!
  27. print("The square root of 12 is " + str(squares[-1]))
  28. '''There are also generators that do calculations when called'''
  29. squares = (n * n for n in range(13))
  30. print(squares[3]) #Just gets the square root of 3, nothing else
  31. '''Side note, when doing debug printing, just use strings and str() to
  32. convert ints and such into printable formats. Also, you can slice
  33. backwards too, -1 is the last element of the array, and you can do -12.'''
  34.  
  35. '''You can also "slice" an array, to get certain chunks within. This
  36. prints squares of 0-3, saying "end before the element in position 4"'''
  37. print(squares[0:4])
  38.  
  39. #You can import "modules" like this
  40. import sys, struct
  41. '''sys.argv lets you get input from the command prompt, seperated by
  42. spaces. It is an array and you can append to it, too. First variable
  43. is /always/ the script, so a file'd be sys.argv[1]'''
  44. sys.argv.append("file.bflim")
  45. sys.argv.append("256")
  46. print(sys.argv)
  47.  
  48. i = 0 #Makes a new variable
  49. while True: #Infinite loop
  50.     if i == 10: break #but we can break it!
  51.     else: pass #This will just not do anything, easy for debug
  52.     i+= 1 #Increment one, can use -= and a bunch more, no ++ tho :<
  53. #Also, you can use Ctrl+C to stop a program when it's infinite looping
  54. print(i) #Should be 10
  55.  
  56.  
  57. with open("test.txt", "w") as f: #Opens fileas writable
  58.     f.write("TEST\x00\x00\x00\x08")
  59. with open("test.txt", "rb") as f: #Opens file as read binary
  60.     print(f.read(4)) #"TEST"
  61.     print(struct.unpack("!I", f.read(4))[0])
  62. '''This unpacks 4 bytes of binary as an int (unpack is a "tuple", use [0])
  63. There's several modes, < is little endian, > is big, @ is native, wouldn't
  64. recommend. There's a whole list of values you can convert to, check
  65. https://docs.python.org/2/library/struct.html#format-characters'''
  66.  
  67.  
  68. '''There's also a bunch of bit manipulation you can do'''
  69. print(bin(0xBF)) #prints 0b<value>, this is 0b10111111
  70. '''& compares if both are 1, 0xF is all 1s, lets you get a digit'''
  71. print(hex(0x42 & 0x0F)) #prints 0x2
  72. print(hex(0x42 & 0xF0)) #prints 0x40
  73. '''| is OR, if either is 1 but not both. Lets you combine values'''
  74. print(hex(0x24 << 8 | 0x68)) #Combines 0x2400 and 0x68, 0x2468
  75. '''You can "shift" bits left or right for how you need them, << and >>'''
  76. '''Another useful compare tool is %, lets you get the remainder'''
  77. 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