Lesson 2: The Python Shell

Learn python programming in 12 lessons


One of the very reason’s Python is powerful is because it has that simple interactive shell that a user can use to evaluate expressions without necessarily writing a program.

Expressions typed in the shell that cannot be evaluated are simple echoed back. For example, if you type a string like “Hello word!”, the shell will echo back ‘Hello world!’. If you type a number such as 2, the shell will echo the same number back. If an arithmetic expression like 3+2 is typed in, the shell will rather evaluate it and display 5. Boolean expression can also be evaluated in the shell. If you type 5==2, the shell will evaluate this and display false because 5 and 2 are not equal.

Python shell can also do computation on strings such as concatenations. If you type “Spider”+”man”, the shell evaluates this to ‘Spiderman’. 2*”Spider”+”man” or “Spider”*2+”man” evaluates to ‘SpiderSpiderman’. 
The shell can also evaluate expressions that makes use of functions. “Hello world!” can be enclosed into the function that displays items to the screen such as print(“Hello world!”) which would display Hello world! instead of ‘Hello world!’.

There are other functions such as min and max to display the minimum and maximum. 
For example min(1,2,3,4) will evaluate to 1 and max(1,2,3,4) will evaluate to 4. to find the square root of 4, you would type sqrt(4) but this would give an error because the library containing the sort function is not directly available, you would have to tell Python that you want to make use of functions in the math library by importing it with the line import math. 
The correct way to reference the square root function in the math library is by using the expression math.sqrt(4) which would evaluate to 2.0 which is double number.

If you ever need to read up on a function, you simply type help() into the interactive shell and you will be provided with the command prompt help>. To exit this prompt, you just hit the return(enter) key and the prompt returns to >>>. 

For example

help> min
min(...)
    min(iterable[, key=func]) -> value
    min(a, b, c, ...[, key=func]) -> value
   
    With a single iterable argument, return its smallest item.
    With two or more arguments, return the smallest argument.

help> max
Help on built-in function max in module __builtin__:

max(...)
    max(iterable[, key=func]) -> value
    max(a, b, c, ...[, key=func]) -> value
   
    With a single iterable argument, return its largest item.
    With two or more arguments, return the largest argument.

help> abs
Help on built-in function abs in module __builtin__:

abs(...)
    abs(number) -> number
   
    Return the absolute value of the argument.

The other method of getting more information on a function if you know it exist is to use access the function’s doc string using this line print(function.__doc__)

For example

>>>print(eval.__doc__)
      eval(source[, globals[, locals]]) -> value

      Evaluate the source in the context of globals and locals.
      The source may be a string representing a Python expression
      or a code object as returned by compile().
      The globals must be a dictionary and locals can be any mapping,
      defaulting to the current globals and locals.
      If only globals is given, locals defaults to it.

>>>print(abs.__doc__)
      abs(number) -> number

      Return the absolute value of the argument.

>>>print(max.__doc__)
      max(iterable[, key=func]) -> value
      max(a, b, c, ...[, key=func]) -> value

      With a single iterable argument, return its largest item.
      With two or more arguments, return the largest argument.

The python shell is very powerful and it should usually be your go to place when you are testing an expression or you simply want to find out more information on a function.

Share your thoughts