Pathwise

Intro to Python · Lesson 4 of 12 · 12 min

Input and types

input() lets your program ask a question, but the answer always comes back as text. Learn the main types and how to convert between them, so "7" + "3" stops giving you 73.

ASKING THE USER

input() asks a question and waits

name = input("Your name? ") shows the prompt, waits for the user to type something and press Enter, then hands back what they typed, which is stored in name. The space at the end of the prompt keeps the answer from sticking to the question mark.

If Sara types her name at the prompt, name now holds "Sara" and the program carries on to the next line.

name = input("Your name? ")
print("Hello, " + name)

Output

Your name? Sara
Hello, Sara

Here the user typed Sara and pressed Enter. The first output line is the prompt with Sara's typing after it; the second is printed by the program. In the app this example is fixed, so the typed answer is shown for you.

FOUR BASIC TYPES

Every value has a type

Python keeps track of what kind of value it's holding. The four you'll use most: int, a whole number like 7; float, a number with a decimal point like 7.5; str, text in quotes like "7"; and bool, which is just True or False. The type decides what operators do: + adds two numbers but joins two strings. type(x) tells you which one x is.

type(7.5) shows <class 'float'>, and type("7") shows <class 'str'>.

Check yourself

Match each value to its type

Show the answer
  • 7 → int
  • 7.5 → float
  • "7" → str
  • True → bool

THE CATCH

input() always gives back text

Whatever the user types arrives as a string, even when it's only digits. Type 7 and you get "7", the character, not the number. That's harmless for a name, but for maths it bites: + on two strings joins them instead of adding.

a = "7" and b = "3", just as two input() calls would give them: a + b is "73".

Check yourself

a = "7"
b = "3"
print(a + b)
  1. 73
  2. 10
  3. 7 3
  4. 7 + 3
Show the answer
73

Step through it

  1. input() hands over "7" and "3" as text

    The user types 7, then 3. input() sends each one into its own box, a and b, and the values are drawn with quotes and a small str tag. Digits typed at the keyboard still arrive as text.

  2. a + b glues them into "73"

    Under a line, a + b gives one box holding "73", still tagged str. With two strings, + doesn't add: it joins the characters end to end, 7 then 3.

  3. int() turns "7" and "3" into 7 and 3

    Each string goes along an int() arrow and comes out on the right as a plain number, without quotes and tagged int. The boxes on the left still hold "7" and "3": int() hands back a new value and leaves the original alone.

  4. 7 + 3 is 10, and int("7.5") fails

    Now 7 + 3 really adds: its box shows 10, tagged int, next to the "73" from before. Below, int("7.5") has an orange cross and a ValueError tag: text that isn't a whole number can't become an int, so use float() for decimals.

Check yourself

a = int("7")
b = int("3")
print(a + b)
  1. 10
  2. 73
  3. 7 + 3
  4. Error
Show the answer
10
print(int("7") + 1)
print(float("7.5"))
print(str(7) + "3")
print(int(7.9))

Output

8
7.5
73
7

int() turns text into a whole number and float() into a decimal. str() goes the other way, number to text, so line 3 joins "7" and "3". The last line surprises people: int(7.9) is 7, because int() cuts off the decimal part; it doesn't round.

bill = float(input("Bill? "))
tip = bill * 0.1
print(f"Tip: {tip}")

Output

Bill? 40
Tip: 4.0

A tip calculator. The user typed 40. Wrapping input() in float() converts the answer straight away, so bill holds the number 40.0 and the maths works. The same pattern with int() is the usual way to ask for a whole number: age = int(input("Age? ")).

Check yourself

Negar's program runs int("7.5"). What happens?

  1. It gives 7
  2. It gives 8
  3. It stops with a ValueError
  4. It gives 7.5
Show the answer

It stops with a ValueError

Right. int() only accepts text that is a whole number, so "7.5" stops the program with ValueError: invalid literal for int() with base 10: '7.5'. float("7.5") would work.

Check yourself

age = ____(input("Age? "))
print(age + 1)

int · str · input · print

Show the answer

int

Right. int() turns the typed text into a whole number, so age + 1 does real maths: typing 30 prints 31.

Lesson recap

  • input("prompt") shows the prompt, waits for Enter, and returns what the user typed.
  • The four basic types are int (7), float (7.5), str ("7") and bool (True/False); type() tells you which.
  • input() always returns a str, so two typed numbers joined with + give "73", not 10.
  • int(), float() and str() convert; the usual pattern is age = int(input("Age? ")).
  • int("7.5") is a ValueError, while int(7.9) cuts to 7 rather than rounding.

Keep it, don't just read it

Pathwise brings each idea back just before you'd forget it, with a quick question. Free on Android and on the web, in English and Persian.

Cafe Bazaar Myket Open the web app

All lessons in this course

  1. Your first program
  2. Variables: names for values
  3. Strings: working with text
  4. Input and types
  5. Making decisions with if
  6. Lists: many values in one place
  7. Loops: doing it again
  8. Functions: name a job, reuse it
  9. Dictionaries: look it up by name
  10. Reading error messages
  11. Saving and reading files
  12. Final project: an expense tracker