Please, can someone explain me this block of code?

I’m new in programming, I really need a detailed explanation of the following code, in particular I need to know:

  1. What are that square brackets after the variables, why are used?
  2. What is open?
  3. What is readlines? Is it a method? If yes how does it works?
  4. what is happening here sex = int(float(l.split()[2])), I need a translation.
  5. What is close? Is it a method? If yes how does it works?
    So, in short, I need a commentary that explains what’s happening here. I will be really gratfull, thanks.

Exercise:
The file ex1_1.txt includes data for a set of patients. The data are organized into rows with the following format: x time sex, where x is an experimental measurement, time is when the measurement was taken (in seconds), and sex is 0 for male and 1 for female.

  • Read the file line by line, and print each line
  • For each line, define the values x, t, and sex equal to the values in the 1st, 2nd and 3rd column respectivelly. Define x and t as floating values, and sex as an integer value
  • Define the lists X, T, and SEX including all the values read from the file
  • Use the function average define above to calculate the mean of X
  • Compute the mean of X separately for men and women

X = #these are square brackets
T =
SEX =
fin = open(‘ex1_1.txt’,‘r’)
for l in fin.readlines():
print(l)
x = float(l.split()[0])
t = float(l.split()[1])
sex = int(float(l.split()[2]))
X.append(x)
T.append(t)
SEX.append(sex)
fin.close()
print('Average: ',average(X))
X_male =
i = 0
while i < len(X):
if SEX[i] == 1:
X_male.append(x)
i = i + 1
print('Male average: ',average(X_male))

You should read through The Python Tutorial — Python 3.9.1 documentation.

1 Like

Hi Giulio, and welcome!

If you are learning Python in school, you really should read the text
book closely. You should also do some tutorials, there are many on the
internet.

To answer your questions:

  1. Square brackets like this: x = [] create an empty list. A list is a
    group of values that are stored together in a specific order.

  2. open is a function that is used to open a file so you can read or
    write to it.

  3. Yes, readlines is a method. When you have an open file, the readlines
    method reads the entire file into a list of lines.

  4. int(float(l.split()[2])) starts with l, which I expect is
    probably a string. l.split() splits the string into words. The [2]
    part grabs the third word. (I know that is weird, but Python starts
    counting at zero.) Then float(...) converts the string into a floating
    point number (a number with a decimal point) and finally the int(...)
    converts that into a whole number, an integer.

  5. Yes, close is also a method, it tells the computer to close the file
    and ensure any data written was updated on the disk. (Sort of… it’s
    complicated.)

Think of files as being like a box on your hard drive. Before you can
see what’s in the box, you have to open(...) the box; when you are
done, you have to close() it again.

Here is an example from #4 above:

>>> l = "Some words 27.3456 go here."
>>> l.split()  # Split into words.
['Some', 'words', '27.3456', 'go', 'here.']
>>> l.split()[2]  # Third word.
'27.3456'
>>> float(l.split()[2])  # Turn into a number, instead of a string.
27.3456
>>> int(float(l.split()[2]))  # Turn into a whole number.
27

The best skill you can learn is how to try things for yourself at the
Python interactive prompt. Do you know how to run Python so you get a
>>> prompt and you can type commands and call functions and see the
result?

You should try running the Python tutorial.

https://docs.python.org/3/tutorial/

Good luck!

Thank you so much, you are too kind. I’ll follow your suggestions. Have a nice day!