The interactive interpreter
So far you have written whole programs: Python reads the file and runs it from top to bottom. Python has a second way of working too, the interactive interpreter. You type one line, press Enter, and Python runs it right away and shows the result. Programmers keep one open all the time, to try out a line or two before it goes into a real program.
On your own computer you start it by typing python3 (on Windows, python) in a terminal. It shows a >>> prompt, which means "type a line of Python". This site has no interpreter window, but lessons show sessions like this one, with the prompts dimmed:
>>> 7 * 6
42
>>> 10 / 4
2.5
>>> "ab" * 3
'ababab'Values are shown, not printed
When the line you type is an expression (something that has a value), the interpreter shows its value, without any print. Look at how it shows a string: with quotes. The interpreter shows a value the way you would write it in Python code, so you can tell a string from a number. print shows the text itself, without quotes:
>>> word = "hello"
>>> word
'hello'
>>> print(word)
hello
>>> len(word)
5
>>> "5"
'5'
>>> 5
5A line that has no value, such as word = "hello", shows nothing. The variable is remembered for the rest of the session, as it would be in a program.
The difference between '5' and 5 matters: the first is a string, the second a number. Later in this module, the checks of many challenges call your functions and show the values they give back in this same way. When a check expects 5 and shows that it got '5', your function gave a string instead of a number.
Trying out methods
The interpreter is the quickest way to find out what something does. You've met string methods such as find: commands that you write after a string, with a dot in between. Strings have many more of them. Two handy ones are upper and lower, which give the string in capital letters or in small letters:
>>> "Python Arena".upper()
'PYTHON ARENA'
>>> name = "ADA"
>>> name.lower()
'ada'
>>> name
'ADA'
>>> name.lowr()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'lowr'. Did you mean: 'lower'?A method gives you a new string: name itself is still 'ADA'. A typo in a method's name gives an AttributeError, and when the name is close to a real one, Python even suggests the right spelling. To see the names of all string methods, try dir("") in the interpreter. Python uses the names with two underscores at each end behind the scenes; the plain ones, such as upper, lower and find, are the ones you call yourself.
A common use of lower is comparing what the user typed without caring about capital letters:
answer = input("Do you want to continue? ")
if answer.lower() == "yes":
print("Continuing")
else:
print("Stopping")
YESDo you want to continue? YES
ContinuingWhether the user types yes, Yes or YES, answer.lower() is "yes".
To try something out on this site, put it inside print in a small program and run it, in any challenge's editor or in the Playground (in the menu at the top).