Programming Tutorials | Python
When it comes to programming in any language, you will need to ensure that you have set up the language to work on your machine. In the case of Python, this means installing Python itself and also ensuring you have an IDE (Integrated Development Environment) installed that supports the language.
In order to install Python, you will need to visit the Python website and download the latest verson of Python from the downloads page. Once you have downloaded Python, simply run the file and follow the installation instructions that you see.
During the installation of Python, the Python IDE should have been installed alongside (IDLE). IDLE can be used to easily write, edit and run Python files. It is recommended that you use this if you are planning to use only Python. If you do not wish to use Python, there are plenty of other IDEs / editors which will support Python, including, but not limited to:
Whilst it is strongly recommended that you do choose to use an appropriate IDE, you can use any text editor to code in Python, as long as you ensure to keep all files to '.py' filetypes and run them with Python. Using an IDE such as IDLE makes this and testing your programs a lot easier.
One of the most basic features of Python is the ability to output content into the shell (console). Though it is basic, it is also incredibly useful for simple features of your program. We can use the ability to output to console for a huge variety of things, including: error logging, text based programs, user input requests and much more.
Printing in Python is incredibly easy using the built in functions of the language. Python uses a function called "print", which takes string parameters in order to output what you want to console. If you input multiple parameters, it will automatically concatenate (join) them and separate each one with a space, attempting to convert anything that is not already a string into one. We can also concatenate strings using "+", however if using that method, you will have to convert any non-string values into strings using the str() function.
When using the print function, there are also extra features to be aware of. If you simply use print and give it a string parameter, it will print the given string onto its own line, however you can change this. Within most languages, "\n" is treated as a command to tell it to go to a new line. You can use this within a string you want to print in order to separate lines easily, or you can use it to keep prints onto the same line. By default, Python will follow up any printed string with "\n", hence why it will go to a new line, however you can change the way that the lines are ended. As a parameter, you can add "end=''". This will define the "end" parameter of the function to be "" rather than "\n", keeping the next print onto the same line. Similarly, we can use this to change the way that the print function concatenates strings. We can define another parameter as "sep=', '". This will separate each parameter we give with ", ", and is useful if you want to print a list easily. We can use this knowledge to create some simple pieces of Python code (The # key will comment out lines, meaning they will not be seen as code and are merely there to explain what parts of the program do):
#Basic printing print("This is a string!") #Concatenating values print("This", "is", "some", "more", "strings") print("This " + "also " + "concatenates!") #Printing to the same line using end print("This will ", end="") print("print to the same line") #Printing lists using sep print("We need to buy: " + "apples", "pears", "oranges", sep=", ")
From this code, you should see the following output:
>> This is a string! >> This is some more strings >> This also concatenates! >> This will print to the same line >> We need to buy: apples, pears, oranges
At even this basic level, there is a lot that you can do with the print function to make some very simple programs which you may find useful, even if it may not seem like you can do much, output is incredibly useful to implement into other features, and integral for creating proper programs.
Another important, basic and useful feature of Python is to appropriately handle user input. Again, by being able to accept user input and handle it correctly, you can open up a lot of opportunities for your programs, especially once you develop upon it to create more complicated and advanced input structures.
Similarly to printing, Python contains a very useful function that we can utilise when it comes to user input through the shell. This function is called "input". The input function, whilst not required, can take a string parameter as the prompt for the user's input. For example, if we wanted to ask the user to input a number, we could do this with the following code:
input("Please input a number: ")
What we will see as a result of this code is:
>> Please input a number:
Following this, Python will allow the user to type in any value into the shell as prompted. On its own, you will not see any results from this, as we are not actually saving or doing anything with the user input. The "input" function has a return value of whatever the user input when prompted, so we can save the input as a variable for us to use later, or we can immediately use the value.
#Save the value input as a variable and then print it userinput = input("Please input a number: ") print("You input:", userinput)
#Immediately use the value input print(input("Please input a number: "))
This code will give you the following output, assuming the user decides to input "7":
>> Please input a number: 7 >> You input: 7 >> Please input a number: 7 >> 7
By utilizing the ability to take user input, we can allow them to choose various options in our program, such as options on a calculator, or the possible choices in a text-based game.
There are a couple of problems with the code being used above, however, as we may want to use the value input by the user in some form of calculation. Due to the fact that the "input" function returns a string, we would receive errors and crash the program if attempting to input any value, even if it was a number. We can fix this partially by "casting" out input to an integer or a float. Casting is when we convert a value into another data type, such as a string into an integer and vice versa. We can cast to a string by using the "str()" function, and similarly, we can cast to integers or floats using the "int()" and "float()" functions, and we can use this as follows:
#Ask the user to input two numbers and save them as integers userinput1 = int(input("Please input a number: ")) userinput2 = int(input("Please input another number: ")) #Print the sum of our values #Using "+" in order to demonstrate casting to a string print("The sum of your inputs is: " + str(userinput1 + userinput2 ))
Assuming that the user inputs "7" and "9", we would get the following output:
>> Please input a number: 7 >> Please input a number: 9 >> The sum of your inputs is: 16
Previously, we would not have been able to use these values in such a way, and by replacing "int" with "float", we can make this work for decimal values (floats) as well. You may notice that you receive an error if you try to input a value which is not a number, such as if you were to input "This is a number". We will be able to fix this easily in the future when we cover handling of errors and exceptions.