Robert Blake - Codeacademy -Python Syntax Answer Sheet

advertisement
Python Syntax. Answer sheet
Task 2
Variables
1. # Write your code below!
2. my_variable=10
Task 3
Data Types
1.
2.
3.
4.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
1.
2.
3.
4.
5.
1.
2.
Task 4
Reassigned
Task 6
White space
means right
space
Task 7
A matter of
Interpretation
Task 8
Single line
Comments
Task 9
Multi-line
Comments
Task 10
Arithmetic
Operators
Task 11
Subtraction
Task 12
Multiplication
Task 13
Division
# Set the variables to the values listed in the instructions!
my_int=7
my_float=1.23
my_bool=True
# 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
# Here's some code that will print my_int to the console:
# The print keyword will be covered in detail soon!
print my_int
def spam():
eggs = 12
return eggs
print spam()
spam=True
eggs=False
1. # You write your comment here after the # tag
2.
3. mysterious_variable = 42
1. """ If you want to write a comment that is going to be on more than one line you must use 3
speech marks to open the comment and 3 speech marks to close the comment"""
2.
1. # Set count_to equal to 1 plus 2 on line 3!
2.
3. count_to=1+2
4.
5. print count_to
1. count_to = 5
2. """Four shalt thou not count, neither count thou two,
3. excepting that thou then proceed to three. Five is right out.
4. Change count_to to 5 minus 2!"""
5.
6. count_to = 5 – 2
7.
8. print count_to
1. # Set ni to 2 times 10 on line 3!
2.
3. ni = 2 * 10
4.
5. print ni
1. # Set ni to 20 divided by 4 on line 3!
2.
3.
4.
5.
1.
2.
3.
4.
5.
1.
2.
3.
4.
5.
1.
2.
3.
4.
5.
Task 14
Exponentiation
Task 15
Modulo
Task 16
Review
ni = 20 / 4
print ni
#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 = 10 % 9
print spam
#This is bringing it all together
monty = True
monty - 10
python = 1.234
monty_python = python **2
Remember…
Python is case sensitive.
1. Variables must be in lowercase and Boolean must be titlecase
2. Whitespace is important and must be used correctly. Do not use tab, use 4 spaces to indent code.
3. # key for a single line comment.
4. 3 speech marks to open and close a multi-line comment
Python Tip Calculator. Answer sheet
Task 1
Favourite
meal
Task 2
The Tax
Task 3
The Tip
Task 4
Reassign in a
single line
Task 5
Calculate it
3.
4.
5.
5.
6.
14.
15.
16.
17.
18.
6.
7.
8.
9.
10.
11.
12.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
# Assign the variable meal the value 44.50 on line 3!
meal = 44.50
meal = 44.50
tax = 0.0675
# You're almost there! Assign the tip variable on line 5.
meal = 44.50
tax = 0.0675
tip = 0.15
# 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)
Download