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

Share your thoughts