Introduction to the Runpython Console - Runpython-IntroProgramming/Course-Syllabus GitHub Wiki

The Python console gives you direct, interactive access to the Python language. The console is a great place to test out short snippets of Python code, or investigate the behavior of built-in Python behavior without actually creating full programs. We will use the console to introduce some basic Python concepts.

To open the console, visit the runpython console.

There will be a (relatively) short pause while the Python system loads, then:

Brython 3.7.4 on Netscape 5.0 (X11; CrOS x86_64 12607.58.0) 
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.86 Safari/537.36
>>>

Click to the right of the bottom set of ">>>" brackets. This is where you will do your typing today.

By the numbers

Start by typing some numbers. Press return or enter when you are done with each number. Try typing numbers with and without decimal points. You can even enter complex numbers! Each time you type a number, Python creates an object in the computer's memory that represents the number, then it shows you the value. Once Python has shown you the value, it throws the number away and forgets all about it!

Your window might look something like this:

>>> 3
3
>>> 3.0
3.0
>>> 55532235
55532235
>>> 234234.23423525
234234.23423525
>>> 32j
32j

That seems pretty tame. So this Python terminal looks like a web site where if you type something, it just gives it back to you. Notice where you typed 32j? Try typing some other combinations.

>>> 32d
Traceback (most recent call last):
  module exec_46 line 1
    32d
    ^
SyntaxError: invalid syntax
>>> 32a
Traceback (most recent call last):
  module exec_46 line 1
    32a
    ^
SyntaxError: invalid syntax

OK, so it turns out that j is special. Does it matter where you put it?

>>> j32
Traceback (most recent call last):
NameError: j32
>>> 3j2
Traceback (most recent call last):
  module exec_46 line 1
    3j2
    ^
SyntaxError: invalid syntax

Apparently, it does.

So it turns out that j has a special meaning, but only when it appears at the end of a number. In Python, the j signifies the square root of -1, or what math type people refer to as i.

Saving Numbers

Typing numbers and getting them back isn't all that interesting!

If you want to keep your numbers around, you can assign them to names using the assignment operator: = .

>>> a = 1
>>> b = 23.5
>>> c = 0.1
>>> d = 0.2
>>> pi = 3.14159
>>> num77 = 77

A couple of things to notice about this example

  1. Python isn't just giving the numbers back to you.
  2. The = isn't exactly like the equals sign that you may be familiar with from algebra. In algebra the equals sign means that whatever is on the left is exactly the same as whatever is on the right. In Python, the equal sign means that whatever is on the right (a number in our examples) can be referred to, or recalled, by using the name given on the left. In programming, a name is usually referred to as a variable.

Again, the meaning of variable in Python is very different from the meaning in algebra. In Algebra, the variable symbolizes a number that is unknown and the equal sign is used to create an equation. In Python, the variable is just a name that we can use to refer to a number, or some other piece of data in the computer.

Variable names must begin with a letter (upper or lower case) and can be as long as you like, and may contain underscore characters or numerals (but not as the first character).

Again, in Python, the = character means that you are creating a name that will be used to refer to an object in computer memory (like a number). The name always appears on the left hand side of the = sign and the object is on the right. Always.


Questions

  1. Would 3 = val be considered valid Python?
  2. Would val1 = val2 = 33 be considered valid Python? (Note: you will need to try it out!)
  3. Suppose you type a = 5 then b = a. Is this valid Python?
  4. What is the final value of b in the previous question?
  5. What is the final value of a in the previous question?

Math (yay!)

First of all, notice that you can recover a number that you have saved in a variable by simply typing the variable name (and pressing enter):

>>> a
1
>>> b
23.5

As you might expect, Python can actually do useful things with those numbers.

>>> a
1
>>> b
23.5
>>> a*b # multiplication
23.5
>>> b/a # division
23.5
>>> b//a # integer division
23.0
>>> (3+5)*pi/43.6 # parentheses are good too
0.5764385321100917
>>> c+d
0.30000000000000004

As you can see in the last example (go back and refer to the values assigned to c and d), simple arithmetic with floating point (decimal) numbers can lead to some interesting surprises. This is because many decimal fractions (numbers with a decimal part) can not be perfectly represented in binary. And since binary is the only way computers can really do math (under the hood), they sometimes will show you values that are very, very close to correct, but not exactly.


Questions

  1. You want to divide $5.00 between four people. How much does each person get? Write a Python expression to calculate this. (answer is $1.25)
  2. An elevator has a capacity of 1000 lb. If an average person weighs 180 lb, how many people can safely ride the elevator. Write an expression in Python to calculate this. (answer is 5)
  3. What does the ** operator do in Python? For example, 2**3.
  4. What does the % operator do in Python? For example, 8%3.

Strings

Python isn't just an arithmetic calculator. You can also create objects that represent written text. For example.

>>> "Python is named after Monty Python"
'Python is named after Monty Python'
>>> 'This is a string with single quotes'
'This is a string with single quotes'
>>> '''This is a string with triple quotes'''
'This is a string with triple quotes'

This is the text equivalent to typing numbers and getting them back again. Notice that simply typing naked text does not do the same thing:

>>> Python is named after Monty Python
Traceback (most recent call last):
  module exec_46 line 1
    Python is named after Monty Python
                  ^
SyntaxError: invalid syntax

In order to create a text object in Python, you have to enclose the text in quotation marks. It doesn't matter what kind of quotation marks you use, but you are required to use the same style at the beginning and end of each text object.

Errors

By the way, you should get used to seeing errors like this. Errors like this just mean that the Python system can't make any sense of what you have typed. Don't give up when you get an error. Read the error carefully and try to figure out what the problem is.

In Python, and most programming languages, text objects like this are called strings.

Pro tip: if you want to create a string object that contains a " character, use the single quotes at the start and end of the string. If you use " characters to define the string, then any " characters inside the string will confuse Python, because it will assume they mark the end of the string.

Strings can be assigned to variables just like numbers can:

>>> myname = "Monty"
>>> myname
'Monty'

You can even do some limited "math" operations on them:

>>> verb = "likes"
>>> verb = " likes"
>>> object = " programming!"
>>> myname + verb + object
'Monty likes programming!'

Converting Strings and Numbers

If you try doing math with combinations of strings and numbers, hilarity ensues:

>>> myname
'Monty'
>>> a
1
>>> myname + 1
Traceback (most recent call last):
TypeError: Can't convert int to str implicitely

Just as in science, you can't add numbers together if they have different type (a Python term) or units (a science term). So we can convert the number to a string and try again:

>>> myname + str(a)
'Monty1'

The bit that says str(a) means we are calling a function. The name of the function is str, and the input to the function is the variable name a (which, you may remember, actually refers to the number 1). The str function takes any object as an input, and attempts to produce a text representation of it.

If you have a string with numbers inside of it, you can convert the other way:

>>> nums = "111"
>>> nums + 3
Traceback (most recent call last):
TypeError: Can't convert int to str implicitely
>>> int(nums) + 3
114

In this example, the function is int and it attempts to convert an object into a number. It is not very smart...


Questions

In each case, make your own prediction, then try it out. Were you right? If not, make sure you understand why!

  1. What will int('one') + 2 do?
  2. What will str(1) + "2" do?
  3. What will str('1') + str(2) do?
  4. What will int('1') + int(2) do?

Now you are ready to write your first programs.