How to do basic Input & Output operations in Python
Are you thinking of becoming a more technical tester? On my journey to become a more technical tester I decided to start learning Python and started doing a lot of other things which are slowly getting me into that direction.
I want to help other testers like me to become technical as well. So, I will be sharing all my technical learnings in a simple way so that testers can get just enough programming knowledge they would need to do automation or read code; through my blog posts.
Having said that, here is a simple explanation on how to do input and output operations in Python.
This post is focused on how to make your programs interactive which is also known as Input/Output in the programming world. I love this quote from Zed Shaw, author of “Learn Python 3 the Hard Way”:
“Programming as an intellectual activity is the only art form that allows you to create interactive art. You can create projects that other people can play with, and you can talk to them indirectly. No other art form is quite this interactive. Movies flow to the audience in one direction. Paintings do not move. Code goes both ways.”
So you can write code to talk to people (output information) and people can interact with the program (input information).
Here are all the basic input/output statements in Python you would need to know as a Tester.
OUTPUT:
There are many ways to output information from a python program. The most basic way to display information to a user is to use the print() statement like this:
print(“Hello World”)
Every character inside the quotes is printed directly on the screen.
To print a number:
print(123)
You can also print the result of a maths calculation:
print(1 + 2 + 3) # outputs the results of a calculation
When there are no quotes used the maths result is calculated and printed on the screen.
print(“What is 1 + 2 + 3?”, 1 + 2 + 3) # outputs a string and result
Every character inside the quotes is printed directly on the screen whereas the result of the maths calculation is printed for the part with no quotes.
You can use variables:
count = 4 # this is a variable named count with value 4
print(“There are”, count, “items”) # combining strings and variable
There are no quotes around count, so the value is displayed.
You can embed variables in a string by starting the string with the letter f (for “format”) and putting the variable inside a special {} sequence. This is also known as an f-string:
print(f”There are {count} items”) # an f-string containing a variable
And concatenate (join) strings when printing:
first = “Hello”
second = “ ”
third = “World”
Fourth = “!”
print(first + second + third + fourth) # joining string variables
There are no quotes in the print statement so the string values are joined together and printed to the screen:
You can also format an already-created string when printing:
interesting = False # create a variable
story_evaluation = “Isn’t that story interesting? {}”
Here in the string variable story_evaluation, the {} are a placeholder for a variable.
print(story_evaluation.format(interesting))
Here, the parameter in the format() function has no quotes, so it passes the value of the variable named interesting into the place where the {} are located in story_evaluation.
Finally, you can also print multiple lines together on the screen by using three double-quotes:
INPUT:
There are many ways to input information. Here are the basic input operations in Python:
For a user to interact with a running program: use input()
You can ask someone a question and get the answer, and put in a prompt so that the user knows what to type:
name = input(“What’s your name? “)
This prompts the user with a question and puts the result in a variable called name.
The input() value is read as a string by default. For feeding different data types into the program you need to convert the input() to the type you need:
a_string = input() # the value input by the user is read as a string
an_integer = int(input()) # you can convert to an integer
a_float = float(input()) # you can also convert to a float
This way, if you enter the wrong type of data value an error message will be displayed:
You can take the value entered by the user and assign it to a variable that can be used again later.
name = input(“What is your name? “) # create a variable called name with the value entered at the prompt
age = int(input(“How many years old are you? “) # create a variable age with the value entered at the prompt
print(f”You told me that your name is {name} and you are {age} years old.”) # use the variables in a message
You can also give inputs in Python when executing a python script, but that will be for a future blog post.
Please share your thoughts below on whether there are any more input/output operations I could add.