Title: 4
Course: Start programming with Python in 10 steps

Figure out some details

Now that you know how Python programming works in general, it's time to start writing programs that can actually do useful things for you. For this, you need to get control over the input and output of your programs, that is I/O control (yes, programming is all about control!), start using (built-in) functions and do things with lists and strings. Since you will be working with text data a lot, now is the time to start handling text files stored on your computer. Knowing a few tricks with strings might prove very useful already too.

👉    8 things to know at this step

1. There are two main sources of input for your programs: standard input is what the user types using the keyboard when prompted by your program. Most of the time though, your program will take input from a text file.
2. In both cases, you need to put the input into a variable so that the program can use it. Python has built-in functions  (also called built-in classes for reasons that will become clear towards the end of this guide) to help you with this: input() for standard input and open() for files.
3. Like programs, functions too have an input and output. The input to a function is what we write inside of the parentheses and what we call arguments, or sometimes parameters. The output of a function is the result of the operations performed inside of the function.
4. This will sound a little odd, but it actually makes sense if you take some time to understand it: the output of the function input() is a literal. This literal has to be sent as an argument (or a parameter) to print() to see what you got from the user.
5. The output of the function open() is not a literal, it is a stream --- a new kind of a variable, similar to a list, but which you cannot print directly. You can picture a stream as a train that your input takes to go from a file to your variable. Each line of the file is a wagon in the train and each wagon is a literal. Remember this when you want to print the content of a file!
6. As a matter of fact, you can think of lists as trains too. To print their elements, you need to iterate over them, that is print the wagons one by one.
7. Strings are practically structured as lists in Python so, in some sense, they are like trains too, with each character being a wagon. This is important because it means that we can manipulate strings in the same ways as lists.
8. A string is a sequence of characters between two quotes. Any characters inside quotes are elements of a string. This means that, inside quotes, there is no difference between letters, numbers, spaces, or interpunction, they are all characters of a string, one pair of quotes, one string. Things are much more nuanced outside of quotes.

🧑‍🎓    Learn actively

▶️   In Step 3, we already used built-in functions similar to those mentioned in 2️⃣, such as print() and range(). Watch this short video to get another example of how print() works:

📹  The print() function by Martin Volk

📝   Look up the meaning of the term function on Wikipedia, especially the schematic depiction, and compare the mathematical notation with the Python syntax: what is common to both?

📝   In Python (and programming in general), built-in functions are like pre-written mini-programs: they contain a bit of code hidden behind a name that you can call. When you call a built-in function, your computer executes this code without you needing to even look at it, almost like magic.

▶️   Now, let’s make sure we get a good understanding of 3️⃣. In the video example above, the input to the print() function is the literal that we want to print (i.e. the variable num or the string ‘hello world’) and the output is the display of this literal on the screen. Open the demo from Step 2 and find all the functions in the notebook: what’s their input? And their output?
❓  Have you noticed that the range() function does not always have the same type of output? Try a few different inputs to range() and print their outputs.
Tip: The output can be an action, like displaying something on your screen, but in the case of range(), it’s information that can be used later on.
Another tip: You might want to catch this information in a variable before printing it, but you actually don’t have to!

📝   Read about lists and strings on Google for Education to get familiar with 6️⃣, 7️⃣ and 8️⃣. Here is a short video demonstrating how lists work:
📹  Lists by Martin Volk

▶️   Make sure to watch this video, too, to see how you can really manipulate strings in the same ways you manipulate lists:
📹   Index a string by Martin Volk

📝   You will see two kinds of things that are applied on lists and strings: functions and methods. For now, you can think of methods as something similar to functions and just note that their syntax is a little different. We’ll come back to methods in Step 9.

📝   Read about the input() function on w3schools. The examples are good illustrations of 4️⃣.
❗  You can easily find basic information about all built-in functions in Python on w3schools. Find the ones you already know and see how, for each of them, you get a list of all possible arguments (or parameters), with some being mandatory and some being optional.

📝  Read about file handling on w3schools. Make sure to read all four parts of the section to see examples of 5️⃣.

❓ Put the pieces together

1.  User input

Write a program which takes two numbers as user input and outputs their:
1.1  multiplication
1.2  division
1.3  the remainder of their integer division.

 Make sure that the program prompts the user to insert the input and adds to each output its description.

2. User input, operations on stings

2.1  Write a program which prints a square pattern N x N, where N is a variable that you define within your program. For example, for N=5, your program should output the following pattern:

+ + + + +
- + + + +
- - + + +
- - - + +
- - - - +

Hint: Use one for-loop and string concatenation

2.2  Write a new version of the program which takes a string as user input and N is the length of the string. Instead of the symbols "+" and "-", print two symbols taken from the user input: the first and the last symbol of the input string. For example, if the user inputs "abcd", then N = 4, the first symbol is "a", and the last symbol is "d".

3. Input from a file, operations on strings

Download this file to your computer, in the same folder where you keep your notebooks. This will be your input file. Your task here is to take this file as input and print all its content on one line (as a single paragraph).

Hint1: The last character in each line is the "new line" character, you can remove it even if you do not see it

Hint2: Use open() with a for-loop (the example under the heading "Loop through the file line by line")

💡   You can do all of this in a single notebook, but it might be a better idea to create three notebooks, one for each task.

Remember: