← Back to course
Code Crew G6+ / Week 02 / Tuesday
2/6
Week 02 Β· Variables and input

Tuesday

Kinds of values
// Programs that remember and ask
⏱ about 30 min

Tuesday: Kinds of values

Rocket is adding up the crew list. He types "5" + "5" into the shell, expecting a team of ten. The shell answers with 55.

"Fifty-five crew members?" he says. "We only have chairs for ten!"

Raven points at the quote marks on the screen. "What do you notice?" she asks. "Those fives are inside quotes. Python thinks they are text, not numbers."

Nova hums and draws two boxes in the air: one labeled numbers, one labeled text. "Would you like a hint?" she asks. "Every value has a type. The type decides what plus means."

Rocket deletes the quote marks, presses Enter, and gets 10. Then he asks the shell what type each value is.

Four types

TypeName in PythonExamples
Whole numberint42, 0, -7
Decimal numberfloat3.5, 0.25, 100.0
Text (a string)str"Nova", "42", ""
True or falseboolTrue, False

The type() function tells you the type of any value. Plus adds numbers, but it joins strings end to end.

>>> type(42)
<class 'int'>
>>> type(3.5)
<class 'float'>
>>> type("42")
<class 'str'>
>>> type(True)
<class 'bool'>
>>> "5" + "5"
'55'
>>> 5 + 5
10

Two more ways to divide

You already know / divides and keeps the decimal. Two more signs are useful. // divides and drops the decimal part. % gives the remainder.

For example, 130 seconds is 130 // 60 minutes, which is 2, and 130 % 60 seconds left over, which is 10.

>>> 7 / 2
3.5
>>> 7 // 2
3
>>> 7 % 2
1
NAME THE TYPE
  • Read the question.
  • Tap your answer.
What type is 98.6?
What type is "98.6" (with quotes)?
What does 17 // 5 give?
Predict: what does "3" + "4" give in the shell?
WHY THIS EXERCISEWith strings, + joins them, so "3" + "4" is "34", not 7.
Predict: what does 17 % 5 give?
WHY THIS EXERCISE% gives the remainder after dividing, and 17 minus 15 leaves 2.
At your computer
1. In the IDLE shell, type type(42) and press Enter.
2. Try type(3.5), type("42") and type(True).
3. Type "5" + "5" and then 5 + 5. Compare the answers.
4. Type 130 // 60 and then 130 % 60. Check you get 2 and 10.
5. Pick your own number of seconds and turn it into minutes and seconds the same way.

Practice: turning one type into another

The int() and float() functions turn strings into numbers. The str() function goes the other way, turning a number into a string.

Dividing with / always makes a float, even when the answer is a whole number.

>>> int("7") + 3
10
>>> str(7) + "3"
'73'
>>> type(10 / 2)
<class 'float'>
>>> 200 // 60
3
>>> 200 % 60
20
WHICH TYPE COMES OUT?
  • Read the question.
  • Tap your answer.
What does str(7) + "3" give?
What type is 10 / 2?
What type is True?
200 seconds is how many whole minutes? (use //)
How many seconds are left over? (use %)
What does int("12") + 8 give?
StatementTrue or false?
"8" + "2" gives 10.?
int("8") + int("2") gives 10.?
9 // 2 gives 4.?
9 % 2 gives 4.5.?
WHY THIS EXERCISEStrings join with plus, numbers add, // drops the decimal, and % gives the remainder, which is 1 here.
Try it
1. In the shell, type str(5) + str(5) and press Enter.
2. Now type int("5") + int("5"). Compare the two answers.
3. Pick a number of seconds over 500. Turn it into minutes and seconds with // and %.
4. Check your answer by multiplying the minutes by 60 and adding the seconds.

Types explain a lot of surprises. Tomorrow your programs will ask the user a question, and types will matter right away.

← Monday