Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Lesson2
- # Go over practice problems!
- # Printing tricks:
- # Concationation: using the "+" to print out multiple things that have the same data type
- # Printing multiple things with different data types, seperate the pieces of data with commas
- # EXAMPLE
- print "A week has", 7, "days."
- # Basic Arithmetic Functions
- # + addition
- # - subtraction
- # * multiplication
- # / integer division (gives the whole number division)
- # % modulo (gives the remainder of divison)
- # ** exponent/power
- # CONCEPT 1: What is a program?
- # A program is a list of instructions for the
- # computer to execute in sequential order.
- # The instructions are written line by line in
- # the source code file. In python, our source
- # code files have the extension .py
- # CONCEPT 2: How do I solve a problem computationally?
- # 1. Break down problem into smaller problems
- # 2. Make series of steps (e.g. algorithm)
- # to solve smaller problems.
- # 3. Write algorithm steps in English (pseudo-code)
- # 4. Write algorithm steps in real code
- # 5. Run code - your problem is (hopefully) solved!
- # EXAMPLE 1:
- # Problem - introduce yourself to someone
- # Breakdown problem into series of smaller (algorithm)
- # steps written in pseudo-code
- # 1) print a greeting
- # 2) print a name
- # 3) print an age
- # Write algorithm is real code
- #print "Hello!"
- #print "My name is Gabrielle."
- #print "I am 19 years old."
- # EXAMPLE 2:
- # Problem - Write a program which translates celsius
- # temperatures to fahrenheit.
- # Algorithm in pseudo-code
- # 1) Get a temperature input in degrees celsius from the user.
- # 2) Use our formula to calculate fahrenheit temperature.
- # 3) Tell the user the temperature in degrees fahrenheit.
- # Algorithm broken down into even simpler steps!
- # 1a) Get input from user
- # 1b) Evaluate input as number
- # 1c) Save number into a variable
- # 2a) Calculate fahrenheit temperature with given formula
- # 2b) Save fahrenheit temperature into a variable
- # 3a) Print message explaining our calculation
- # 3b) Print value of fahrenheit variable
- # ACTIVITY 1
- # Write an algorithm for making your favorite meal.
- # Remember to be specific!
- # ACTIVITY 2
- # Write the definition for an algorithm in your own words.
- # ACTIVITY 3
- # Try to break down this problem into smaller subproblems:
- # PROBLEM - Drinking a glass of milk.
- # The first step could be
- # 1. Get the milk out of the refrigerator.
- # CHECK IN
- # 1. True or False: The output of an algorithm can change even
- # if you give it the same inputs every time.
- # 2. True or False: Pseudo code is runnable code.
- # CONCEPT 3: Input, Process, Output (IPO)
- # 1. Get input from user
- # 2. Process input / do some calculations to it
- # 3. Output final calculations or information to the user
- # EXAMPLE
- #celsius = eval(input("Enter a temperature in degrees celsius: "))
- #fahrenheit = 9/5 * celsius + 32
- #print("The temperature is", fahrenheit, "degrees Fahrenheit.")
- # VARIABLES
- # Variables are simply names created by you that are used to keep
- # track of information in your program.
- # Each variable has some value associated to it.
- # Variables are created when they are first assigned values.
- # The value associated to a variable can be updated to a new value
- # when used in an expression.
- # Variables must be declared before they can be used in expressions.
- # EXAMPLE 1
- # The "=" is used to assign vales to variables.
- counter = 100 # An integer (non decimal)
- miles = 1000.0 # A float (decimal)
- name = "John" # A string
- print counter
- print miles
- print name
- # EXAMPLE 2
- # Changing what is inside a variable.
- changing = 3
- print changing
- changing = 9
- print changing
- # EXAMPLE 3
- # Assigning the value of a variable to be the value of another variable
- red = 5
- blue = 10
- print (red, blue)
- yellow = red
- print (yellow, red, blue)
- # EXAMPLE 4
- # Python allows you to assign a single value to several variables simultaneously.
- a = b = c = 1
- # Here, the number 1 is assigned to the variables a, b, and c.
- # EXAMPLE 5
- # You can also assign multiple objects to multiple variables.
- a, b, c = 1, 2, "John"
- # Here, a = 1, b = 2, and c = “John”.
- # Rules for variable names
- # 1. Names can not start with a number.
- # 2. There can be no spaces in the name, use _ instead.
- # 3. Can't use any of these symbols :'",<>/?|\()!@#$%^&*~-+
- # 4. It's considered best practice (PEP8) that the names are lowercase.
- # 5. Variable names should not be Python reserved words like with or not.
Add Comment
Please, Sign In to add comment