Uncategorized

9:Input & Output

Input & Output

In the previous lecture we understand the concept of variables and we find that every program has many variables to solve a problem but variables are inside the computer and have no direct interaction with the real world. In actual working software, data enter to variables from the real world and results are sent to the real world after processing. To interact with the real world, every programming language has artifacts that are called input and output statements. These statements use different input and output devices.

  1. We must have some input and output devices that enable a computer to interact with the real world.
  2. Input devices help us to insert data in computer (see Figure 1 in lecture 1 – Computer System)
  3. There are hundreds and thousands of input devices that can be used with a computer like keyboard, mouse, camera, scanner, micro phone, heat sensor, proximity sensor, pressure sensor etc.
  4. To make things simple, we only use the keyboard for this course as an input device.
  5. Similarly, there are an unlimited number of output devices that can attach to a computer or represent the results.
  6. Monitor, multimedia projector, speakers, motor, pumps, piston or other actuators.
  7. For keeping things simple, we will use a monitor as output.
  8. On the monitor, two types of display system exist.
  9. Command prompt
  10. Graphical User Interface (GUI)
  11. Command prompt is a simple and text-based system.
  12. GUI supports images, animations, videos and text.
  13. Command prompt-based systems are easy for computers to handle while GUI requires more complex methods to handle.
  14. We will use command prompt in this course.
  15. The output of the command prompt will be displayed at the Terminal window of VS Code.

Input Statement

Input statements work as a function (we read more about functions in lecture 17)

name = input ("Enter your name: ")

Code 1: Input String

Code 1 will help to enter the string. When this statement will execute, we find this output

Figure 1: Result of Code 1

  1. When Code 1 is executed, the program will print “Enter your name: ” and will halt to wait for entering the string where the cursor is blinking. It is possible that on your computer the cursor may not blink. In this case you just click on the “Enter your name: ” line.
  2. After typing the string press enter.
  3. When you press enter, the string you have typed here will store in the name variable.
  4. input has limitation that it only can take string inputs.
  5. If you want to input integers, float or bool values into variables then you have to cast the data in required data type.
num_str = input ("Enter a number: ")num = int(num_str)

Code 2: Entering Integer Value

  1. You can see that first store the input value into a string variable and then we convert this string into integer by using int method.
  2. We can also use a short-hand method that performs both operations (input and casting) in a single line.
num_str = int (input ("Enter a number: "))

Code 3: Short-hand of input and conversion

  1. No technical difference between code 2 and code 3.
  2. We will use short-hand for next.

Output Statement

  1. The output statement is used to represent the result. 
  2. There are multiple output statements in Python but we only use the print method that displays the output in Terminal.
print ("Hello World!")

Code 4: Display output with print function

  1. The value we want to show in output, we will place it between the parenthesis ()
  2. By default, the print function also works with strings but it can also work with other data types without explicit casting.
print (5)

Code 5: Output a simple integer number with print

  1. Code 5 will print 5 on the terminal.
  2. Similarly, you can place some variables here and the value of the variable will also display.
  3. The print method also has capability to display multiple values even with different data types.
print ("Hello", 5, 3.14)

Code 6: Printing multiple values with single statement

Figure 2: Output of multiple values display with different data types

String Interpolation

String interpolation is very useful when any other data type is required to display inside a string

age = 20print (f"Age of student is {age} years")

Code 7: Displaying integer value inside string

  1. String interpolation shows a natural flow of English sentences with an integer variable inside.

Figure 3: Output of String Interpolation

  1. For string interpolation use a leading f outside the quotation marks and use {} around variable name using in it.
  2. String interpolation can be used multiple times and different data types.
name = "Ali"age = 20height = 5.9print (f"{name} age is {age} and height is {height} feet.")

Code 8: Using Different Data Types with Multiple Variables in String Interpolation

Output like follows.

Figure 4: Output of multiple String Interpolations in single statement

 

Leave a Reply

Your email address will not be published. Required fields are marked *