3 – Variables

“Think twice; code once”

Theory

Data types and Operators

Two of the data types that we are using are integers and strings.

  • an integer: any integer number
    • .., -3, -2, -1, 0, 1, 2, 3, …
  • a string: any sequence of 0 or more characters between quotes
    • “”
    • “5”
    • “look at me mom!”

The following are a few of their operators:

integer operators

string operators

Now we can take an integer expression and reduce it to its value:

We can also evaluate an expression of strings:

The value of a function

The value of a function is the value that the function returns, i.e., the value that follows the keyword return.

In Python, if the keyword return is not followed by anything, then the value of the function is set to the special value None. For example,

let me try it

Variables

  • A variable is a value with a name.
  • When we name a value, we say that we are assigning the value to the variable.
  • We assign values to variables using the assignment operator =
  • A variable behaves like the value that it contains

let me try it

Type casting

Expressions are usually composed of elements of the same type, e.g., all the terms are integers:

or all the terms are strings:

If we want to use a term of one type into an expression of another type we need to change its type; this is called casting.

Casting functions:

  • String to integer -> use int()
    int( “5” ) == 5
  • Integer to string -> use str(),
    str( 5 ) == “5”

let me try it

Getting input from the user

Karel can read strings using input(), which receives a string to use in a dialog, and returns as a string with the user’s reply, i.e.,

For example:

Show it to me

Function Input/Output (I/O)

output

To get information from a function, we assign its value to a variable, e.g.,

input

We pass information to a function within the parentheses; the data in the parentheses are called the arguments of the function call, e.g.,

minus_diagramFunctions receive data in variables called parameters. The parameters are paired up with the arguments so that:

1st parameter = 1st argument
2nd parameter = 2nd argument

and so on

For example:

show it to me, let me try

for loop

With for loops we can repeat a group of instructions many times. We saw that the following two pieces of code do the same thing:

Show it to me




Now we’ll see why. In Python, for loops often use the function range(a, b), which returns a list of numbers where a is the first number that we want, and b is the first number that we do not want. For example, to get a list with the numbers from 0 to 4 (we do not want 5) we use:

Hence, when we call range() inside a loop we get a list, e.g., these are equivalent pieces of code:




The for loop sets up the variable i to be each value of the list, in succession. Hence, the three following three pieces of code are equivalent:


Show it to me


show it to me


Show it to me


Training Missions

3.TM1 Life in SoCal – Football

Work it out

It is no surprise that a football player as skilled as Karel is being courted to play in different leagues. The canadian football fields are larger than the american ones. Help Karel run some reconnaissance on the perimeter of each field. Most of the program is already written; we need to write the functions that find the perimeter and announce the result.

American football field

American football field

The perimeter of a rectangle is the sum of the length of all its sides, e.g., if its width it 5 ft. and its length is 10 ft. then its perimeter is:

Answer: Show it to me

3.TM2 Olympic dreams – Mountain training

Work it out

Karel has seen the benefits of mountain training. Now that he is in the downhill team, he is helping to prepare the course for his teammates. Karel needs to climb the mountain and place a marker at each level so that his teammates get oriented as to where they are: he needs to place 1 marker at elevation 1, 2 at elevation 2, and so on. After Karel is done, he needs to go home and ask for a replenishment of the markers that he used, so he can mark another course some other day. It goes without saying that Karel cannot fly; he has to go up and down the steps.

Jenny Hadfield - Image from runnersworld.com

Jenny Hadfield – Image from runnersworld.com

We have been using the variable i as the index of the loop. Here we will need variables that take two other roles.
The first one is a counter, which is a variable that keeps track of how many times we have iterated over a loop:

let me try

In a for loop, there is usually a close relationship between the index of the loop i and the counter, but there does not need to be.

The second structure that we need is an accumulator, which is a variable that accumulates some value:

let me try

Often we accumulate some value that depends on i, but this does not need to be the case.

Answer: Show it to me

Before...

Before…

...and after.

…and after.

3.1 Strawberries – Fertilizer tryout

Work it out

Karel is trying out a new fertilizer. The manufacturer left 50 bags for Karel but we only need a few of them. Use input() to tell Karel a number between 0 and 50; he needs to move these many bags to the shed, then go home, and then use say() to confirm the number of bags that he moved to the shed.

fertilizer tryout

Answer: Show it to me

We ask Karel to take 12 bags to the shed

We ask Karel to take 12 bags to the shed


Job well done

Job well done

3.2 Strawberries – Single file

★★ Work it out

Karel wants to plant strawberry plants in each of the 7 cells in front of his house. This time, though, he has a strategy for planting them: he will place 1 plant in the farthest cell from his house and then, as he approaches the house, he will place one more plant in each cell than he placed in the preceding cell, i.e., he will plant, 1, 2, 3, 4, 5, 6, and 7 plants in the 7 cells in front of his house. Keep track of how many strawberries he has planted and announce the total when he gets home.

Notice the pattern: if he deposited i plants in a cell, Karel will deposit i+1 plants in the next cell, i.e.,
1, 2, 3, 4, 5, etc.

Strawberry, ''gariguette'' variety Copyright © 2005 David Monniaux

Strawberry, ”gariguette” variety Copyright © 2005 David Monniaux

Answer: Show it to me

Plant the strawberries in front of the house

Plant the strawberries in front of the house

Ha!  Done.

Ha! Done.

3.3 Strawberries – single file harvest

★★★ Work it out

The strategy of Karel worked out: it turns out that the more plants you put together, the more strawberries you get; in each grid we got the square of the number of plants that we initially planted, i.e., if we planted 5 plants, we got 5 x 5 = 25 strawberries; if we planted 6, we got 6 x 6 = 36 strawberries, and so on.

Notice the pattern: if he picked up i plants from a cell, Karel will picked up i*i plants from the next cell, i.e., 1, 4, 9, 16, 25, …

Have Karel pick up all the strawberries and put 120 in the fridge, in the shed. The rest he takes home to bake a pie.

"Strawberry Cheese Pie" by Own work - Own work. Licensed under Creative Commons Attribution 2.5 via Wikimedia Commons

“Strawberry Cheese Pie” by Own work – Own work. Licensed under Creative Commons Attribution 2.5 via Wikimedia Commons

Answer: Show it to me

Collect the strawberries, put 120 in the shed and take the rest home to bake a pie.

Collect the strawberries, put 120 in the shed and take the rest home to bake a pie.

Final scenario

Final scenario

Optional Mission – Does not give a star

3.4 Life in SoCal – Binary harvest

★★★★ Work it out

Karel is encouraged by his success with the single line crop and increasing crop sizes. He has bought fertilizer to follow this strategy further. He bought lots of fertilizer of increasing size, this time each twice as big as the previous one, i.e., lots of 1, 2, 4, 8, 16, … etc. Please, move the bags to the sheds and go home to announce how many bags we got in total.

Notice the pattern: if he move i bags to a shed, Karel will move i+i bags to the next shed, i.e., 1, 2, 4, 8, 16, …

Image from vegetable-garden-basics.com

Image from vegetable-garden-basics.com

Answer: Show it to me

Before

Before

..and after

..and after