Hi guys, welcome to DAY 3 of our python series. Today, we are going to be talking about collecting user inputs and reading data from external files.
Table of contents
Introduction And Recap
What are user inputs?
How do we allow users give data to our program?
How do we make use of user given data in our program?
Reading and using data from external files.
In our last tuotial, i mentioned that this part will cover 'Working with user inputs and reading external files'.
Before we go on, i will like to do a quick recap of what we have learned so far.
On Day 1, it was pretty much the basics:
We installed the python interpreter, visual studio code and the python extension for vs code, We did a little introduction on python and then we set up visual studio code for python development, We talked about debugging and code execution etc.
CLICK HERE TO READ DAY 1
On Day 2, We delved into python properly and talked about:
Declaring and assiging values to variables, changing the values of variables, variable data types etc.
CLICK HERE TO READ DAY 2
Today we are going to work with user inputs and external files.
Input in computing context symply means to 'enter data into the computer while the user is anybody who uses codes or programs written by you.
That would make user inputs any information or data entered into the computer by users. How do we give users a means of entering data into our program? and how do we make use of that data? Let us give answers to The above questions.
Giving users an avenue to enter data into your program is very much easy in python. to acheive that, we make use of the inbuilt python function known as 'input.
Let use make it practical:
Lauch visual studio code and open our tutorial folder
- If you have any issues doing that, please refer back to day 1 and day 2.
Create a new file named 'externaldata.py' or you can give it whatever name you want but do not forget the .py extension.
Inside the new file, enter the code below:
input("Enter Your name:")
input("Enter Your age:")
Right click on your work area and select 'Run python file in terminal'. If everything was typed correctly, you'll first see a message saying 'Enter Your name:' and if you type your name and hit enter another message will pop up in the terminal saying 'Enter Your age:'. If you enter your age and hit enter, that is the end of the program.
Let me explain the code:
'input' is an inbuilt python fuction that allows us to collect user inputs. it expects one arguement from you which is a prompt - a message that gives your user an idea of what to enter.
Lines one and two are pretty much the same albiet that one asks for your name and the other your age.
But now you may ask : Where did the information i entered go? The answer is no where, we don't even have any record of it.
Now, how do we account for the data entered?, How do we work with it?, How do we make it something tangible? Let us see
To make use of data given by your user, you need to assign that data to a variable thereby making it tangible. if you have forgoton how to assign values to variables, please refer to day 2.
Update our code to look like this:
Username = input("Enter Your name:")
Age = input("Enter Your age:")
print(Username)
print(Age)
Run the code, enter the required data in the terminal and watch the result. If everything worked out fine, you'll see the name and age you have given printed in the terminal because they are now tangible and can be accounted for.
Lets make it much more fun. Update your code to look like this:
Username = input("Enter Your name:")
Age = input("Enter Your age:")
print("Your name is " + Username + " and you are " + Age + " Years Old.")
Run the code and observe the behavior. There is really not much new here at all except the '+' sign which is used to concatenate.
Concatenation symply means combining multiple strings to form one and that is what we did. we entered Multiple strings and combined them with our inputed data in the form of variables to print what we want.
Here is another example. comment the code above and enter this code(if you have forgotten how to comment, refer to day 1).
num1 = input("Enter your first number: ")
num2 = input("Enter your second number: ")
num3 = input("Enter your third number: ")
total =int(num1) + int(num2) + int(num3)
print(total)
Run and observe once again. you'll see that we have created a calculator that can add 3 numbers. Again the only thing new here is 'int' and it is an inbuilt function that converts our variable to an integer or number so that we can perform numerical operations like addition on it.
Thats it with collecting user inputs, making them tangible and using them. look at all the examples , add your own ideas and practice them more.
Now what if i have an external file and want to read from it or perhaps i want to add data to it using code.
To use external file, we make use of the 'open' function and it expects two arguments - a file part and the mode of opening the file. The mode depends on what you want to do with the file(do you want to just write data into it?, do you want to just read from it? or do you want to be able to do both?)
Below are some modes and how the work:
'r' Mode
Only allows you to read data from the file. You cannot write into it
'w' Mode
Only allows for data to be written into the file and also allows for the modification of already present data - you cannot read from it.
'a' Mode
This mode only allow appending data to the end of the file but does not allow modification or reading.
'r+' Mode
Gives you power to do whatever you want with the file.
Now lets get practical again. comment all our code and add this:
Create a 'students.txt' file in our tutorial folder and save some names into it.
To read from the file, enter the code below:
Students_file = open('students.txt', 'r')
print(Students_file.read())
Students_file.close()
Run it and you'll see that all the contents of the file printed out.
To write into the students file, enter the code below:
Students_file = open('students.txt', 'a')
Students_file.write("\nJohnson jay")
Students_file.close()
run the code and observe the behavior. open the 'students.txt' file and realize that a new name has been added to the list.
Thats is for reading and writing into files. i think this is all you need for now.
The next part of this series will address 'Working with operators'. STAY TUNED
I remain Isaiah Ozadhe and i write regularly on programming here on Emmason247, Do have a wonderful day and don't forget to read some of my other articles.