Week 1: Python Basics / Finger Exercises
Write a piece of Python code that prints out the string hello world
My solution: hello_world.py
Write a piece of Python code that prints out the string 'hello world
' if the value of an integer variable, happy
, is strictly greater than 2.
My solution: happy.py
Assume that two variables, varA
and varB
, are assigned values, either numbers or strings.
Write a piece of Python code that evaluates varA
and varB
, and then prints out one of the following messages:
"string involved"
if eithervarA
orvarB
are strings"bigger"
ifvarA
is larger thanvarB
"equal"
ifvarA
is equal tovarB
"smaller"
ifvarA
is smaller thanvarB
My solution: vara_varb.py
In this problem you'll be given a chance to practice writing some while loops.
-
Convert the following into code that uses a while loop.
prints
2
prints
4
prints
6
prints
8
prints
10
prints
Goodbye!
-
Convert the following into code that uses a while loop.
prints
Hello!
prints
10
prints
8
prints
6
prints
4
prints
2
-
Write a while loop that sums the values 1 through
end
, inclusive.end
is a variable that we define for you. So, for example, if we defineend
to be 6, your code should print out the result:
21
which is 1 + 2 + 3 + 4 + 5 + 6.
My solution: while.py
In this problem you'll be given a chance to practice writing some for loops.
-
Convert the following code into code that uses a for loop.
prints
2
prints
4
prints
6
prints
8
prints
10
prints
Goodbye!
-
Convert the following code into code that uses a for loop.
prints
Hello!
prints
10
prints
8
prints
6
prints
4
prints
2
-
Write a for loop that sums the values 1 through
end
, inclusive.end
is a variable that we define for you. So, for example, if we defineend
to be 6, your code should print out the result:21
which is 1 + 2 + 3 + 4 + 5 + 6.
My solution: for.py