Advertisement
tamsenmckerley

python quiz #1 review

Jun 3rd, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.24 KB | None | 0 0
  1. import turtle
  2.  
  3. #Use of main function
  4. def main():
  5.     #Strings
  6.     x = "hello"
  7.     x = 'hello' #' and " do the same thing
  8.     x = """hello my\nname is\ntamsen""" #"""creates multiple line string, \n is a new line
  9.     #https://www.programiz.com/python-programming/methods/string <-- methods
  10.    
  11.     #Creating Variables
  12.     x = True #bool
  13.     x = False
  14.     x = 1 #int
  15.     x = 1.1 #float
  16.     x = "hello" #string
  17.     x = [1, 2, 3] #list
  18.     #variables' types can change
  19.     #http://developer.rhino3d.com/guides/rhinopython/python-datatypes/ <-- more in depth
  20.    
  21.     #user input
  22.     name = input("what is your name?")
  23.     number = input("type a number")
  24.     number = int(number) #input() takes in everything as a string so you have to convert it if you want a dif. type
  25.     #you can do things like int(var), bool(var), float(var), etc to convert
  26.  
  27.     #For loops (various ways to do it), each prints 3 times
  28.     for i in range(3):
  29.         print("hello")
  30.     for i in range(1,4):
  31.         print("bonjour")
  32.     for i in range(1,7,2):
  33.         print("hallo")
  34.     #range explanation: https://www.pythoncentral.io/pythons-range-function-explained/
  35.     for i in [1, 2, 3]:
  36.         print("hola")  
  37.    
  38.     #Lists
  39.     x = [1, 2, 3]
  40.     y = x[0] #indexes are the same as they are in java
  41.     y = x[-1] #-1 is the end of a list (going left from 0)
  42.    
  43.     #Turtle Class (import statement at the top of the module)
  44.     #https://docs.python.org/3.3/library/turtle.html?highlight=turtle <-- documentation w/ methods
  45.     wn = turtle.Screen()
  46.     t = turtle.Turtle()
  47.     #^^ always need these two methods at the beginning 3(names are changeable tho)
  48.     wn.exitonclick() #end turtle programs with this
  49.    
  50.     #Converting from integer to binary to List  
  51.     x = 5
  52.     print(bin(x)) #x goes from 5 to 0b101 (which is a string). the 0b represent that it's in binary, and the 101 is 5 in binary
  53.     print(bin(x)[2:]) #to get rid of the 0b
  54.     y = bin(x)[2:]
  55.     y = list(y) #turns 101 into a list where each number is one index - ['1', '0', '1']
  56.     print(y)
  57.    
  58.     #creating functions
  59.     def hello():
  60.         print("hello")
  61.     hello()
  62.      
  63. if __name__ == "__main__": #runs the main function. fun fact idk how it works so don't ask me
  64.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement