Guest User

Untitled

a guest
Jun 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. # Lesson2
  2.  
  3. # Go over practice problems!
  4.  
  5. # Printing tricks:
  6. # Concationation: using the "+" to print out multiple things that have the same data type
  7. # Printing multiple things with different data types, seperate the pieces of data with commas
  8.  
  9. # EXAMPLE
  10. print "A week has", 7, "days."
  11.  
  12.  
  13. # Basic Arithmetic Functions
  14. # + addition
  15. # - subtraction
  16. # * multiplication
  17. # / integer division (gives the whole number division)
  18. # % modulo (gives the remainder of divison)
  19. # ** exponent/power
  20.  
  21.  
  22. # CONCEPT 1: What is a program?
  23. # A program is a list of instructions for the
  24. # computer to execute in sequential order.
  25. # The instructions are written line by line in
  26. # the source code file. In python, our source
  27. # code files have the extension .py
  28.  
  29. # CONCEPT 2: How do I solve a problem computationally?
  30. # 1. Break down problem into smaller problems
  31. # 2. Make series of steps (e.g. algorithm)
  32. # to solve smaller problems.
  33. # 3. Write algorithm steps in English (pseudo-code)
  34. # 4. Write algorithm steps in real code
  35. # 5. Run code - your problem is (hopefully) solved!
  36.  
  37. # EXAMPLE 1:
  38.  
  39. # Problem - introduce yourself to someone
  40.  
  41. # Breakdown problem into series of smaller (algorithm)
  42. # steps written in pseudo-code
  43. # 1) print a greeting
  44. # 2) print a name
  45. # 3) print an age
  46.  
  47. # Write algorithm is real code
  48. #print "Hello!"
  49. #print "My name is Gabrielle."
  50. #print "I am 19 years old."
  51.  
  52. # EXAMPLE 2:
  53.  
  54. # Problem - Write a program which translates celsius
  55. # temperatures to fahrenheit.
  56.  
  57. # Algorithm in pseudo-code
  58. # 1) Get a temperature input in degrees celsius from the user.
  59. # 2) Use our formula to calculate fahrenheit temperature.
  60. # 3) Tell the user the temperature in degrees fahrenheit.
  61.  
  62. # Algorithm broken down into even simpler steps!
  63. # 1a) Get input from user
  64. # 1b) Evaluate input as number
  65. # 1c) Save number into a variable
  66. # 2a) Calculate fahrenheit temperature with given formula
  67. # 2b) Save fahrenheit temperature into a variable
  68. # 3a) Print message explaining our calculation
  69. # 3b) Print value of fahrenheit variable
  70.  
  71.  
  72.  
  73.  
  74. # ACTIVITY 1
  75. # Write an algorithm for making your favorite meal.
  76. # Remember to be specific!
  77.  
  78. # ACTIVITY 2
  79. # Write the definition for an algorithm in your own words.
  80.  
  81. # ACTIVITY 3
  82. # Try to break down this problem into smaller subproblems:
  83. # PROBLEM - Drinking a glass of milk.
  84. # The first step could be
  85. # 1. Get the milk out of the refrigerator.
  86.  
  87.  
  88. # CHECK IN
  89. # 1. True or False: The output of an algorithm can change even
  90. # if you give it the same inputs every time.
  91. # 2. True or False: Pseudo code is runnable code.
  92.  
  93. # CONCEPT 3: Input, Process, Output (IPO)
  94.  
  95. # 1. Get input from user
  96. # 2. Process input / do some calculations to it
  97. # 3. Output final calculations or information to the user
  98.  
  99. # EXAMPLE
  100. #celsius = eval(input("Enter a temperature in degrees celsius: "))
  101. #fahrenheit = 9/5 * celsius + 32
  102. #print("The temperature is", fahrenheit, "degrees Fahrenheit.")
  103.  
  104.  
  105.  
  106.  
  107.  
  108. # VARIABLES
  109. # Variables are simply names created by you that are used to keep
  110. # track of information in your program.
  111. # Each variable has some value associated to it.
  112. # Variables are created when they are first assigned values.
  113. # The value associated to a variable can be updated to a new value
  114. # when used in an expression.
  115. # Variables must be declared before they can be used in expressions.
  116.  
  117. # EXAMPLE 1
  118. # The "=" is used to assign vales to variables.
  119. counter = 100 # An integer (non decimal)
  120. miles = 1000.0 # A float (decimal)
  121. name = "John" # A string
  122.  
  123. print counter
  124. print miles
  125. print name
  126.  
  127. # EXAMPLE 2
  128. # Changing what is inside a variable.
  129. changing = 3
  130. print changing
  131.  
  132. changing = 9
  133. print changing
  134.  
  135. # EXAMPLE 3
  136. # Assigning the value of a variable to be the value of another variable
  137. red = 5
  138. blue = 10
  139. print (red, blue)
  140.  
  141. yellow = red
  142. print (yellow, red, blue)
  143.  
  144. # EXAMPLE 4
  145. # Python allows you to assign a single value to several variables simultaneously.
  146. a = b = c = 1
  147. # Here, the number 1 is assigned to the variables a, b, and c.
  148.  
  149. # EXAMPLE 5
  150. # You can also assign multiple objects to multiple variables.
  151. a, b, c = 1, 2, "John"
  152. # Here, a = 1, b = 2, and c = “John”.
  153.  
  154.  
  155.  
  156. # Rules for variable names
  157. # 1. Names can not start with a number.
  158. # 2. There can be no spaces in the name, use _ instead.
  159. # 3. Can't use any of these symbols :'",<>/?|\()!@#$%^&*~-+
  160. # 4. It's considered best practice (PEP8) that the names are lowercase.
  161. # 5. Variable names should not be Python reserved words like with or not.
Add Comment
Please, Sign In to add comment