Showing posts with label Python Shell. Show all posts
Showing posts with label Python Shell. Show all posts

Lesson 4: Input, Output and Assignments

Learn python programming in 12 lessons


A python program can also accept input from the user and this is used by the input() function. The python program AddThreeNums.py can be updated to accept input from a user and store them in the three variables as follow


num1 = input(“Enter the first number”)
num2 = input(“Enter the second number”)
num3 = input(“Enter the third number”)

total = eval(num1) + eval(num1) + eval(num4)
average = total/3

print(“Total”, total)
print(“Average”, average)

The eval function is used to convert a string that is a number into an integer.

The print function used to provide the output to the screen is an interesting function. In Python 3, you are able to change a number of things in the printing pattern of any expression. When printing(1,2,3,4,5), The print command automatically inserts a space between the numbers separated by the commas. Those spaces can be replaced by any character sequence of your choice such as print(1,2,3,4,5,sep=‘’) which would print 12345 with no spaces.

>>> print(1,2,3,4,5)

1 2 3 4 5

>>> print(1,2,3,4,5,sep='')

12345

>>> print(1,2,3,4,5,sep='=')

1=2=3=4=5

>>> print(1,2,3,4,5,sep='&')

1&2&3&4&5

>>> print(1,2,3,4,5,sep='*')

1*2*3*4*5

>>> print(1,2,3,4,5,sep='no')

1no2no3no4no5

>>> print(1,2,3,4,5,sep='!')

1!2!3!4!5

Also take note that the print function automatically prints a new line once it has run out of things to print. This newline brings the cursor to the next line to print a different sequence on a new line. To change the end character from a newline to any character of your choice, the end keyword is used. The end keyword can be used as follows


>>> print(1,2,3,4,5,end=’STOP')

1 2 3 4 5STOP>>>

>>> print(1,2,3,4,5,sep=‘no’,end=‘*')

1no2no3no4no5*>>>

>>> print(1,2,sep=‘no’,end=‘\n')

1no2

>>> print(1,sep=‘no’,end=‘*')

1*>>>

You should take note that the sep keyword is only invoked if there is another item in the printing queue. The end keyword is only invoked once the printing queue is empty.

There are a number of predefined characters that can be used with the print function such as a tab denoted by \t and a single quote denoted by \’. The use of a backslash is called escaping the character sequence and to print the backslash, you have to escape it as \\. The first backslash encountered in the printing sequence notifies the interpreter that the next character is not there by mistake but you actually wish to print it as it appear. Here are a list of other character sequence escaped in Python.
\t --> tab
\n --> new line
\b --> back space
\s --> space
\" --> quotation mark (")
\' --> apostrophe (')

You have already encountered assignments in Python in the program AddThreeNums.py program above and one such example is average=total/3. Python can also do multiple assignments on the same line like


w,x,y,z = 1,2,3,4

The above assignment is equivalent to


w = 1

x = 2

y = 3

z = 4

Python is also able to handle double swapping assignments in one line without the need to have a temporary variable to facilitate the process. Say you wanted the numbers in x and y above to be swapped, you could introduce a new variable and do the swap like this


temp = x

x = y

y = temp

The three lines above are equivalent to the single line  
x,y = y,x
read more

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.
read more