Advertisement
plirof2

PYTHON tutorial, Notes

Jun 9th, 2022 (edited)
865
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.73 KB | None | 0 0
  1. ## Online tester https://www.online-python.com/
  2. #https://www.learnpython.org/en/Lists
  3. #https://docs.python.org/3/library/
  4. # https://www.learnpython.org/en/Numpy_Arrays  ??
  5. #INPUT : a = int(input('Enter 1st number: '))
  6. ===============Aligned text in columns===================
  7.     for i in range(totalitems):
  8.      line_new = '{:<12}  {:>12}  {:>12}'.format(myproductslist[i], inventory[i], currentprices[i])
  9.      #print (i,')',myproductslist[i],' | owned: ',inventory[i]," | price: ",currentprices[i])
  10.      print (i,')',line_new)              
  11. ________________________________________________                         
  12.  
  13.      for i in range(totalitems):
  14.            print (i,')',city[i])
  15.  
  16. def positive_or_negative():
  17.     return 1 if random.random() < 0.5 else -1
  18.  
  19. def random_event_happened():
  20.     return 1 if random.random() < special_event_chance else 0                                                                                
  21.  
  22. def traveling(a):
  23.        global daycount,cash,debt,currentcity
  24.  
  25. ########################################
  26. #print( "hello" + " " + "world")
  27. #print ("hello" * 10)
  28. #print([1,2,3] * 3) # lists can be multiplied (NOTE just like joining them 3 times)
  29. #LIST can be JOINED (concaterated) :all_numbers = odd_numbers + even_numbers
  30. # String formatting : https://www.learnpython.org/en/String_Formatting
  31. #%s - String (or any object with a string representation, like numbers)
  32. #%d - Integers
  33. #%f - Floating point numbers
  34. #%.<number of digits>f - Floating point numbers with a fixed amount of digits to the right of the dot.
  35. #%x/%X - Integers in hex representation (lowercase/uppercase)
  36. #print(len(astring))
  37. #astring = "Hello world!"
  38. # print(astring.index("o")) ;printe 4
  39. # print(astring[3:7]) #This prints a slice of the string, starting at index 3, and ending at index 6
  40. #print(astring[3:7:2]) # This prints the characters of string from 3 to 7 skipping one character. This is extended slice syntax. The general form is [start:stop:step].
  41. #print(astring[::-1]) #reverse string
  42. #print(astring.upper())
  43. #print(astring.lower())
  44. #String checks
  45. #print(astring.startswith("Hello")) #TRUE
  46. #print(astring.endswith("asdfasdfasdf")) #FALSE
  47. # afewwords = astring.split(" ") # Split string by space (like php explode!!!) for x in afewwords: print(x)
  48. # if name == "John" and age == 23:
  49. #    print("Your name is John, and you are also 23 years old.")
  50. #if name in ["John", "Rick"]:
  51. # --------IF---------------
  52. #statement = False
  53. #another_statement = True
  54. #if statement is True:
  55. #    # do something
  56. #    pass
  57. #elif another_statement is True: # else if
  58. #    # do something else
  59. #    pass
  60. #else:
  61. #    # do another thing
  62. #    pass
  63. #
  64. #x = [1,2,3]
  65. #y = [1,2,3]
  66. #print(x == y) # Prints out True
  67. #print(x is y) # Prints out False # Unlike the double equals operator "==", the "is" operator does not match the values of the variables, but the instances themselves. For example:
  68. # ----------LOOPS FOR
  69. ## Prints out the numbers 0,1,2,3,4
  70. for x in range(5):
  71.     print(x)
  72.  
  73. # Prints out 3,4,5
  74. for x in range(3, 6):
  75.     print(x)
  76.  
  77. # Prints out 3,5,7
  78. for x in range(3, 8, 2):
  79.     print(x)
  80. #----_WHILE LOOP
  81. ## Prints out 0,1,2,3,4
  82.  
  83. count = 0
  84. while count < 5:
  85.     print(count)
  86.     count += 1  # This is the same as count = count + 1
  87. # break is used to exit a for loop or a while loop, whereas continue is used to skip the current block, and return to the "for" or "while" statement. A few examples:
  88. #
  89. ## Prints out 0,1,2,3,4
  90.  
  91. count = 0
  92. while True:
  93.     print(count)
  94.     count += 1
  95.     if count >= 5:
  96.         break
  97.  
  98. # Prints out only odd numbers - 1,3,5,7,9
  99. for x in range(10):
  100.     # Check if x is even
  101.     if x % 2 == 0:
  102.         continue
  103.     print(x)
  104. #
  105. # FUNCTIONS:
  106. #def sum_two_numbers(a, b):
  107.     return a + b
  108. #
  109. #
  110. #
  111. #Classes ---------------------------
  112. class MyClass:
  113.     variable = "blah"
  114.  
  115.     def function(self):
  116.         print("This is a message inside the class.")
  117.  
  118. myobjectx = MyClass()
  119.  
  120. myobjectx.variable
  121. myobjectx.function()
  122. #------------------------------
  123. ## Dictionary arrays
  124. #phonebook = {
  125.     "John" : 938477566,
  126.     "Jack" : 938377264,
  127.     "Jill" : 947662781
  128. }
  129. print(phonebook)
  130. ##-----------Iterating over dictionaries--------------------
  131. #phonebook = {"John" : 938477566,"Jack" : 938377264,"Jill" : 947662781}
  132. for name, number in phonebook.items():
  133.     print("Phone number of %s is %d" % (name, number))
  134. #--------------
  135. # Del a value fron dictionary
  136. phonebook = {
  137.    "John" : 938477566,
  138.    "Jack" : 938377264,
  139.    "Jill" : 947662781
  140. }
  141. del phonebook["John"]
  142. print(phonebook)
  143. #------------------------------
  144. #import
  145. #You can use the import * command to import all the objects in a module like this:
  146. ## game.py
  147. # import the draw module
  148. from draw import *
  149.  
  150. def main():
  151.     result = play_game()
  152.     draw_game(result)
  153. #
  154. #
  155. #
  156. #
  157. #
  158. #
  159. #
  160. #
  161. #
  162. #
  163. #
  164. #
  165. #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement