Practice: The Basics#
With variables, conditionals and operators under your belt, this chapter is meant to give you additional practice. Note that each question will include a handful of assert
statements. After you write and execute the code to each question, each assert
should “pass silently” (meaning: should give no output upon execution) indicating that you are on the right track. After you’ve written your code and run the assert
s, you can always check your answers, as the answers to these questions are included in the “Answers” chapter of this book.
Variables#
Variables Q1. Write five lines of code that define five different variables named var_float
, var_str
, and var_bool
, var_tuple
, and var_dict
, storing a float, a string, and a boolean, a tuple, and a dictionary, respectively. (You get to choose the specific float, string, boolean, tuple, or dictionary each variable stores.)
Checks you can use to see if you’re on the right track:
assert isinstance(var_float, float)
assert isinstance(var_str, str)
assert isinstance(var_bool, bool)
assert isinstance(var_tuple, tuple)
assert isinstance(var_dict, dict)
Operators#
Operators Q1 Use the variables provided below (var_low
, var_mid
and var_high
) to generate the variables: var_even
and var_odd
according to the following specifications:
var_even
| use the 3 variables provided once each and at least two of the math operators (+
,-
,*
,/
,**
,//
,%
) to store the value 12var_odd
| use the 3 variables provided once each and at least two of the math operators (+
,-
,*
,/
,**
,//
,%
) to store the value 13
Variables provided:
var_low = 2
var_mid = 20
var_high = 30
Checks you can use to see if you’re on the right track:
assert int(var_even % 2) == 0
assert var_even == 12
assert var_odd % 2 != 0
assert var_odd == 13
Operators Q2. First, define two variables below: my_age
and pres_age
. Store your age (as an integer) in my_age
and store the current US President’s age (as an integer) in pres_age
.
Then, use (refer to) my_age
and pres_age
to define define two variables: true_compar
and false_compar
.
These variables must satisfy the following requirements:
Each variable must be defined using one comparison operator (
<
,>
,<=
,>=
,!=
,==
).Each variable must use and refer to
my_age
andpres_age
(and no other values or variables)true_compar
should evaluate as the booleanTrue
;false_compar
should evaluate as the booleanFalse
Checks you can use to see if you’re on the right track:
assert isinstance(my_age, int)
assert my_age < 150
assert pres_age <150
assert true_compar
assert not false_compar
Operators Q3. Create three variables: var_even
, var_odd
, and var_operator
.
Across these three variables:
all seven math operators (
+
,-
,/
,*
,**
,%
,//
) must be used at least oncevar_even
must store an even numbervar_odd
must store an odd numbervar_operator
can include other variables/numbers but must use (refer to) the variablesvar_even
andvar_odd
during assignment and must store (have the value) 6 (or 6.0)
Checks you can use to see if you’re on the right track:
assert var_odd % 2 != 0
assert int(var_even % 2) == 0
assert isinstance(var_operator, float) or isinstance(var_operator, int)
assert var_operator == 6
Operators Q4. Your answer will define two variables: true_var
and false_var
.
These variables must satisfy the following requirements:
Each variable must be defined using at least one comparison operator (
<
,>
,<=
,>=
,!=
,==
).Each variable must use and refer to at least one variables defined below (
var_low
,var_mid
,var_high
)Additional values and/or operators may also be used, but
true_var
must use theor
operator;false_var
must use theand
operator.true_var
should evaluate as the booleanTrue
;false_var
should evaluate as the booleanFalse
Variables provided:
var_low = 35 % 11
var_mid = 5 ** 2
var_high = (14 + 2) * 6
Checks you can use to see if you’re on the right track:
assert true_var
assert not false_var
Operators Q5 Create three variables: comp_a
, comp_b
, and comp_c
.
These variables must satisfy the following requirements:
Each variable must store (aka return) the Boolean
True
Additional operators may also be used, but
comp_a
must use theand
operator;comp_b
must use theor
operator, andcomp_c
must use thenot
operator.Each variable must be created using at least two of the comparison operators (
<
,>
,<=
,>=
,!=
,==
).All six comparison operators must show up in the cell below at least once.
Checks you can use to see if you’re on the right track:
assert comp_a
assert comp_b
assert comp_c
Conditionals#
Conditionals Q1. Store your first name as a string in the variable my_name
.
Write a conditional that will determine if there are more letters in your name (my_name
) relative to the number of letters in the comparison_name
(provided below, and storing the string ‘Shannon’).
If your name is:
shorter than the
comparison_name
, store the string ‘shorter’ in the variableoutput
longer than the
comparison_name
, store the string ‘longer’ in the variableoutput
the same length as
comparison_name
, store the string ‘same length’ in the variableoutput
Note: Do not hard-code. This means your code should work (store the correct string in output
) regardless of the specific strings stored in comparison_name
or my_name
.
Variable provided:
comparison_name = 'Shannon'
Checks you can use to see if you’re on the right track:
assert my_name
assert output
assert output in ['shorter', 'longer', 'same length']
Topics Synthesis#
Synthesis Q1. Create two variables: var_cat
and var_sum
.
var_cat
should store a string created (assigned) by concatenating two strings togethervar_sum
should store a numeric variable (float or int) created (assigned) by adding two numeric values together
The specific values you use to generate var_cat
and var_sum
are up to you, as long as they fulfull the criteria specified.
Checks you can use to see if you’re on the right track:
assert isinstance(var_cat, str)
assert isinstance(var_sum, float) or isinstance(var_sum, int)
Synthesis Q2. Create three variables: var_int
, var_float
, and var_combined
.
var_int
should store an integervar_float
should store a floatvar_combined
should use a math operator to addvar_int
andvar_float
together
The specific values you use to generate var_int
and var_float
are up to you, as long as they fulfull the criteria specified.
Checks you can use to see if you’re on the right track:
assert isinstance(var_int, int)
assert isinstance(var_float, float)
assert var_combined == var_int + var_float
Synthesis Q3. create three variables: var_a
, var_b
, and var_c
.
These variables must satisfy the following requirements:
var_a
must store a float (you get to choose the specific float)var_b
must use at least two of the comparison operators (<
,>
,<=
,>=
,!=
,==
) and store (return) the valueTrue
.var_c
must use at least two of the math operators (+
,-
,/
,*
,**
,%
,//
) and store the value 36
Checks you can use to see if you’re on the right track:
assert isinstance(var_a, float)
assert var_b
assert var_c == 36
Synthesis Q4. create three variables: var_a
, var_b
, and var_c
.
var_a
should store four characters in an immutable variable type that is only able to store one piece of informationvar_b
should be a single value that, when added to itself returns a whole numbervar_c
should be an immutable variable storing three different values
The specific values you use to generate the variables are up to you, as long as they fulfull the criteria specified.
Checks you can use to see if you’re on the right track:
assert isinstance(var_a, str)
assert len(var_a) == 4
assert var_b % 1 == 0
assert isinstance(var_c, tuple)
assert len(var_c) == 3