- 20160131 [Codecademy] Python(1)
- Lesson:Python Syntax、Tip Calculator
- [ 筆記內容 ] Python Syntax、Tip 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
Lesson:Python Syntax、Tip Calculator
(一)Variables and Data Types
# Write your code below!
my_variable = 10
# Set the variables to the values listed in the instructions!
my_int = 7 # interger
my_float = 1.23 # float
my_bool = True # boolean
# 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
def spam():
eggs = 12
return eggs
print spam()
(三)Comments
# I like coding!!
mysterious_variable = 42
'''
我喜歡coding
'''
(四)Math Operations
# Set count_to equal to the sum of two big numbers
count_to = 123 + 456
print count_to
#Set eggs equal to 100 using exponentiation on line 3!
eggs = 10 ** 2
print eggs
#Set spam equal to 1 using modulo on line 3!
spam = 5 % 4
print spam
(五)Review
# This is a test
monty = True
python = 1.234
monty_python = 1.234 ** 2
Tip Calculator
# Assign the variable meal the value 44.50 on line 3!
meal = 44.50
meal = 44.50
tax = meal / 1000 + 0.023
# You're almost there! Assign the tip variable on line 5.
meal = 44.50
tax = 0.0675
tip = 15.0 / 100
# Reassign meal on line 7!
meal = 44.50
tax = 0.0675
tip = 0.15
meal = meal + meal * tax
# 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)