Data types, pt. 1

[vocab] integer

M. Lim
Intro to Programming
3 min readFeb 4, 2018

--

→ Lab Assignment: “Integers + Strings” on repl.it

1. Let’s run the following in IDLE shell

Remember:

  • Lines that start with # are comments (don’t type them)
  • >>> ← this is the shell’s prompt for a new command. Press ‘Enter’ to run each command.

2. Now let’s try this:

.

.

.

What happens? We get this error:

We can’t add an integer to a string, because they are different data types!

So we need to convert the integer → string, using str():

🤖 str() is a function that turns any value into a string

3. Try calling str() on the integer variable:

4. Now let’s use it to print the whole statement:

Great! Now let’s look at an example where we need to convert the other way, from string → integer

5. Define the following input variable:

Because this is the shell, it will immediately ask you for the input afterwards. Type your age and press ‘Enter’.

6. To check the value of your variable, just type its name + press ‘Enter’:

It should return the value immediately after.

As you can see, it returns a string.

🤖 * An input is always a string *

Remember this ^

7. Let’s try to find out how old you will be in 25 years:

.

.

.

What happens? We get this error:

We can’t add an integer to a string, because they are different data types!

So we need to convert the string → integer, using int():

🤖 int() is a function that turns a value into an integer

8. Try calling int() on the age variable:

9. Now let’s add 25 years:

No error this time! 😎

--

--