The input output system, usually called I/O, is a big deal in programming. To do it, Python already ships several functions we can use directly.
Some functions, print() and input() among them, are standard functions widely used to do I/O. Let's go further.
The Output System in Python with the print() Function
First we'll cover the output system.
We actually touched on this function in an earlier piece. It should be familiar too, since we use it so often.
The print() function prints something to the terminal screen.
print("Halo Dunia👋")
print("Teks ini akan dicetak di layar terminal")
If that code is run, output like this comes out.
Halo Dunia👋
Teks ini akan dicetak di layar terminal
That's it, just like that. Simple, right. Eitss.. not yet, there are a few more things to cover about this one function.
Printing Several Values
The print() function can print several values. You do it by passing several values separated by commas.
nama = "Inva"
umur = 18
print("Halo semua, namaku", nama, "umurku", umur, "tahun")
Run it and output like this comes out
Halo semua, namaku Inva umurku 18 tahun
By default, python puts a space between every value you pass as a parameter to the print() function.
Parameter is the term used for a value given to a function.
~ We'll cover parameters further in the procedural programming with python section.
If you want to change the separator, you can add a sep parameter with the separator you want.
nama = "Inva"
umur = 18
print("Halo semua, namaku", nama, "umurku", umur, "tahun", sep="+")
Then the output changes to:
Halo semua, namaku+Inva+umurku+18+tahun
Output Formatting
The code from before doesn't look so good. It's tangled and there are far too many commas in there.
To solve that, we can do output formatting using the format() method available on the string data type.
Let's turn that tangled code into this:
nama = "Inva"
umur = 18
print("Halo semua, namaku {} umurku {} tahun".format(nama, umur))
Run it and the output stays the same as before:
Halo semua, namaku Inva umurku 18 tahun
The curly braces ({}) in that code work as placeholders, standing in for the real values. The real values go to the format method's parameters in order.
See? Cleaner already, right 😎
The Input System in Python with the input() Function
Output is handled. Now we cover the input system in Python 3.
Input, or what goes in, is the 'something' a user gives for the program to process. Input is also what lets a program run dynamically and flexibly.
For the input process, we can use the input() function python already provides.
This function takes a parameter, a string to display on the terminal screen. It also returns the input string from the user.
let's change the earlier code so it's a little more 'dynamic'
nama = input("masukkan nama: ")
umur = input("masukkan umur: ")
print("Halo semua namaku {} umurku {} tahun".format(nama, umur))
Instead of giving values directly to the variables nama and umur, that example gives the input() function as their value.
Run it and you get 2 chances to enter values. Those then go into the variables in question.
Here's the output.
masukkan nama: Udin
masukkan umur: 24
Halo semua namaku Udin umurku 24 tahun
Converting the Input Data Type
Even when you enter a number as input, the variable in question stays a string.
Example
input_angka = input("masukkan angka: ")
print(type(input_angka))
Output:
masukkan angka: 12
<class 'str'>
In that example you can see the variable input_angka is a string.
So what does that cost us?
Say you want a simple calculator that adds 2 numbers coming from user input.
bil1 = input("bilangan 1: ")
bil2 = input("bilangan 2: ")
hasil = bil1 + bil2
print("hasil dari {} + {} adalah {}".format(bil1, bil2, hasil))
That code probably looks normal enough. Now guess the result if the user enters the numbers 12 and 24.
bilangan 1: 12
bilangan 2: 24
hasil dari 12 + 24 adalah 1224
Hmm...🤔 The result should be 36. Any idea why that happens?
In that code, because variables bil1 and bil2 are strings, the operation happening on variable hasil is concatenation. So what we get is 2 strings joined together.
So what's the fix?
In this case you should be adding numbers. But addition only works on variables of type number. So you need to convert the data types.
To change a string into a float or an integer, you can use the float() or int() function.
bil1 = input("bilangan 1: ")
bil2 = input("bilangan 2: ")
hasil = int(bil1) + int(bil2)
print("hasil dari {} + {} adalah {}".format(bil1, bil2, hasil))
Output:
bilangan 1: 12
bilangan 2: 24
hasil dari 12 + 24 adalah 36
Yayy🎉🎉
Wrapping Up
That's it for this one. Hope it helps.
Next time we'll carry on into programming with conditions in Python 3
See ya...