Skip to content
This repository has been archived by the owner on Feb 27, 2021. It is now read-only.

Latest commit

 

History

History

Week 1 Exercises

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Week 1 Exercises

Week 1: Python Basics / Finger Exercises

Exercise: hello world

Write a piece of Python code that prints out the string hello world


My solution: hello_world.py

Exercise: happy

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

Exercise: vara varb

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 either varA or varB are strings
  • "bigger" if varA is larger than varB
  • "equal" if varA is equal to varB
  • "smaller" if varA is smaller than varB

My solution: vara_varb.py

Exercise: while

In this problem you'll be given a chance to practice writing some while loops.

  1. Convert the following into code that uses a while loop.

    prints 2

    prints 4

    prints 6

    prints 8

    prints 10

    prints Goodbye!

  2. Convert the following into code that uses a while loop.

    prints Hello!

    prints 10

    prints 8

    prints 6

    prints 4

    prints 2

  3. 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 define end to be 6, your code should print out the result:

21

which is 1 + 2 + 3 + 4 + 5 + 6.


My solution: while.py

Exercise: for

In this problem you'll be given a chance to practice writing some for loops.

  1. Convert the following code into code that uses a for loop.

    prints 2

    prints 4

    prints 6

    prints 8

    prints 10

    prints Goodbye!

  2. Convert the following code into code that uses a for loop.

    prints Hello!

    prints 10

    prints 8

    prints 6

    prints 4

    prints 2

  3. 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 define end to be 6, your code should print out the result:

    21

    which is 1 + 2 + 3 + 4 + 5 + 6.


My solution: for.py