20160131 [Codecademy] Python(1)

Lesson:Python SyntaxTip Calculator

[ 筆記內容 ] Python SyntaxTip Calculator
  • (一)Variables and Data Types
  • 1.  Variables
  • 2.  Booleans
  • 3.  Reassigned
  • (二)Whitespace and Statements
  • 1.  Whitespace
  • (三)Comments
  • 1.  Single Line Comments
  • 2.  Multi-Line Comments
  • (四)Math Operations
  • 1.  Math
  • 2.  Exponentiation
  • 3.  Modulo
  • (五)Review
  • Tip Calculator
  • 1.  The Meal
  • 2.  The Tax
  • 3.  The Tip
  • 4.  Reassign in a Single Line
  • 5.  The Total
 
 
 
 

(一)Variables and Data Types

1.  Variables
  • A variable stores a piece of data, and gives it a specific name.
  • [ 範例 1 ] Variables
  • Set the variable my_variable equal to the value 10.
# Write your code below!
my_variable = 10
 
 
2.  Booleans
  • A boolean is like a light switch. It can only have two values. Just like a light switch can only be on or off, a boolean can only be True or False.
  • [ 範例 2 ] Booleans
  • Set the following variables to the corresponding values:
  1. my_int to the value 7
  1. my_float to the value 1.23
  1. my_bool to the value True
# Set the variables to the values listed in the instructions!
 
my_int = 7        # interger
my_float = 1.23   # float
my_bool = True    # boolean
 
 
3.  Reassigned
  • You can change the value of a variable by "reassigning" it.
  • [ 範例 3 ] Reassigned
  • Try it and see! Change the value ofmy_int from 7 to 3 in the editor:
# my_int is set to 7 below. What do you think
# will happen if we reset it to 3 and print the result?
 
my_int = 7
 
# Change the value of my_int to 3 on line 8!
 
my_int = 3    # Reassigned
 
# Here's some code that will print my_int to the console:
# The print keyword will be covered in detail soon!
 
print my_int
 
 
 
 

(二)Whitespace and Statements

1.  Whitespace
  • In Python, whitespace is used to structure code. Whitespace is important, so you have to be careful with how you use it.
  • [ 範例 4 ] Whitespace Means Right Space
  • Properly indent the code with four spaces before eggs on line 2 and another four before return on line 3.
  • You should indent your code with four spaces.
def spam():
    eggs = 12
    return eggs
        
print spam()
 
 
 
 

(三)Comments

1.  Single Line Comments
  • The '#' sign is for comments. A comment is a line of text that Python won't try to run as code. It's just for humans to read.
  • [ 範例 5 ] Single Line Comments
  • Write a comment on line 1. Make sure it starts with '#'. It can say anything you like.
# I like coding!!
 
mysterious_variable = 42
 
 
2.  Multi-Line Comments
  • While you could write a multi-line comment, starting each line with #, that can be a pain.
  • Instead, for multi-line comments, you can include the whole block in a set of triple quotation marks:
  • [ 範例 6 ] Multi-Line Comments
  • Write a multi-line comment in the editor. It can be any text you'd like!
'''
我喜歡coding
'''
 
 
 
 

(四)Math Operations

1.  Math
  • You can add, subtract, multiply, divide numbers like this:
  • [ 範例 7 ] Math
  • Set the variable count_to equal to the sum of two big numbers.
# Set count_to equal to the sum of two big numbers
count_to = 123 + 456
 
print count_to
 
 
2.  Exponentiation
  • We create a new variable called eight and set it to 8, or the result of 2 to the power to 3 (2^3).
  • Notice that we use ** instead of * or the multiplication operator.
  • [ 範例 8 ] Exponentiation
  • Create a new variable called eggs and use exponents to set eggs equal 100.
  • Try raising 10 to the power of 2.
#Set eggs equal to 100 using exponentiation on line 3!
eggs = 10 ** 2
 
print eggs
 
 
3.  Modulo
  • Modulo returns the remainder from a division. So, if you type 3 % 2, it will return 1, because 2 goes into 3 evenly once, with 1 left over.
  • [ 範例 9 ] Modulo
  • Use modulo to set spam equal to 1. You can use any two numbers that will leave a remainder of 1 to do this.
#Set spam equal to 1 using modulo on line 3!
 
spam = 5 % 4
 
print spam
 
 
 
 

(五)Review

  • Variables
  • which store values for later use.
  • Data types
  • such as numbers and booleans.
  • Whitespace
  • which separates statements.
  • Comments
  • which make your code easier to read.
  • Arithmetic operations
  • including +, -, *, /, **, and %.
 
  • [ 範例 10 ] Bringing It All Together
  • Write a single-line comment on line 1. It can be anything! (Make sure it starts with #)
  • Set the variable monty equal toTrue.
  • Set another variable python equal to 1.234.
  • Set a third variable monty_pythonequal to python squared.
# This is a test
monty = True
python = 1.234
monty_python = 1.234 ** 2
 
 
 
 

Tip Calculator

1.  The Meal
  • You've finished eating at a restaurant, and received this bill:
  • Cost of meal: $44.50
  • Restaurant tax: 6.75%
  • Tip: 15%
  • You'll apply the tip to the overall cost of the meal (including tax).
  • [ STEP_1 ] The Meal
  • First, let's declare the variable mealand assign it the value 44.50.
# Assign the variable meal the value 44.50 on line 3!
 
meal = 44.50
 
 
2.  The Tax
  • The tax on your receipt is 6.75%. You'll have to divide 6.75 by 100 in order to get the decimal form of the percentage. (See the Hint if you would like further explanation.)
  • [ STEP_2 ] The Tax
  • Create the variable tax and set it equal to the decimal value of 6.75%.
meal = 44.50
tax = meal / 1000 + 0.023
 
 
3.  The Tip
  • Nice work! You received good service, so you'd like to leave a 15% tip on top of the cost of the meal, including tax.
  • Before we compute the tip for your bill, let's set a variable for the tip. Again, we need to get the decimal form of the tip, so we divide 15.0 by 100.
  • [ STEP_3 ] The Tip
  • Set the variable tip to decimal value of 15% on line 5.
# You're almost there! Assign the tip variable on line 5.
 
meal = 44.50
tax = 0.0675
tip = 15.0 / 100
 
 
4.  Reassign in a Single Line
  • Okay! We've got the three variables we need to perform our calculation, and we know some arithmetic operators that can help us out.
  • We saw in Lesson 1 that we can reassign variables. For example, we could say spam = 7, then later change our minds and say spam = 3.
  • [ STEP_4 ] Reassign in a Single Line
  • On line 7, reassign meal to the value of itself + itself * tax. And yes, you're allowed to reassign a variable in terms of itself!
# Reassign meal on line 7!
 
meal = 44.50
tax = 0.0675
tip = 0.15
 
meal = meal + meal * tax
 
 
5.  The Total
  • Now that meal has the cost of the food plus tax, let's introduce on line 8 a new variable, total, equal to the new meal + meal * tip.
  • The code on line 10 formats and prints to the console the value of total with exactly two numbers after the decimal. (We'll learn about string formatting, the console, and print in Unit 2!)
  • [ STEP_5 ] The Total
  • Assign the variable total to the sum of meal + meal * tip on line 8. Now you have the total cost of your meal!
# Assign the variable total on line 8!
 
meal = 44.50
tax = 0.0675
tip = 0.15
 
meal = meal + meal * tax
total = meal + meal * tip
 
print("%.2f" % total)