Course Overview: We'll start with an introduction to an Interactive Development Environment and the basic concepts of programming with the Python language.
Please note: depending on the version of Python you are using, you may have to use
py in the code instead of
python. For example:
For any queries relating to this course, please contact us at email@example.com.
For info on how to download PyCharm, please visit this link: https://www.jetbrains.com/help/pycharm/installation-guide.html
PyCharm download: https://www.jetbrains.com/pycharm/download/#section=windows
Python download: https://www.python.org/downloads/windows/
Hello, for this lesson we will start with developing a simple hello world application in Python. The objectives of the hello world course are as follows. First, we want to introduce you to the Python programming language. Then we will help you recognize and explain the core Python concepts. We will then go through some of the common Python syntax that you need to know to write programs in Python. We have the exercises in this learning path available for you in the included labs, however it may be also be very useful to have a Python Interpreter installed or available to you from your computer. During this course we'll be using the PyCharm interpreter. You need to have the editor integrated with the Python programming compiler to be able to invoke a Python shell. You can access a virtual compiler like the AWS Cloud9 one if you don't have administrator access to your machine. So let us go over the terminal to show you an IDE.
Now an IDE is basically an interactive development environment and I'll show you how that's working as a compiler. Here I am using PyCharm IDE. Now with PyCharm we start by creating a new project. We will leave the default directories as they are and create a default directory called Cloud Academy. Just click on an existing interpreter, click on Create to continue. I recommend updating the libraries to ensure you have the latest runtime of Python. In my PyCharm interface here, PyCharm presents us with a series of tips which are useful if you haven't used PyCharm before. To start our project, open the Cloud Academy directory and right click on it so that you can create a new Python file. We'll name this file Hello_World. PyCharm will automatically add the .py extension to the file. The .py extension is universally known to be representative of a Python script and this is what the interpreter first looks for to recognize this as a Python script. So next we will type in the text that we need to get our program running as an executable script in Python. We define hello world with the declaration statement D-E-F, or def.
On our next line we type the declaration return followed by Hello World enclosed in speech marks. This signals to the interpreter that we want to print out this text in the console. However, we need to actually tell the interpreter to print our variable out to the screen. We do this by calling the function hello world within parenthesis. Once you have completed this input, we need to have the interpreter execute this as a program. So let's start the IDE interpreter so that we can test out this program. Click on the terminal Sub menu in PyCharm so that you can open a new terminal window to run the program. Now within the Cloud Academy directory, I should be able to run the command Python, hello_world.py. And the output should show you the words Hello World. Now, for those of you who are not able to get this running don't worry, the IDE is great for showing you any errors which is what we will look at now. Python programming or programming in general follows a certain set of rules. One of the key issues with the way rules work are that you have to follow them so that the program will be recognized by the program interpreter and it will do what you need it to do. Now, we used the word D-E-F, or def, to signal to the interpreter that the string or values following that was the name of the function. If we typed D-E-F incorrectly the interpreter would not recognize it as a function when we run the Python program. The Python interpreter will tell you that it's an invalid without quite telling you where the error was.
So in this case, we can see in our text that we ended the line with a colon. Now the error console is very useful but it does not always show you the the point at which an error has occurred. If I have missed out the brackets or have the semicolon or the colon wrong, for example as I have here, I will find myself again with a syntax error. And again, without an indicator to where actual the issue is. Now one way Python's different from other languages is that is does not use a delimiter to indicate the end of the line. Python instead uses white spaces to indicate one section from another section. So for example, if you were to take away this space in between the hard return and the front of the line the interpreter would return another syntax error. And in this case it wasn't a syntax error, but it was an indentation error which shows that Python was expecting that there would be an indentation at this point. So we can either put it a space or just use a break. Ensuring your indentation is correct is very important in Python. If you're using two spaces, stick to using two spaces if you are using four spaces, just make sure that it's always use four spaces. The safest approach is to use the Tab key to signal an end of the line. Let's now look at the print command. In this case, the print command calls a method we defined as hello world. Again, if I were to remove the brackets off the hello world Python would show you the function with a number hash. Now this number hash is the memory address of where the function hello world is stored. If we were missing one of the parenthesis of the pier, the Python interpreter would display an error for us. In this case the interpreter is expecting that the line should end somewhere because it will look for the closing bracket that forms a pair with the opening bracket we used to declare this earlier. So just to recap, we're telling the interpreter to define a function called Hello World.
The empty parenthesis following the statement tells the interpreter that this function has no variables. If we wanted to define variables for this function, then we would declare those variables within this set of brackets. This is the start of the method in this and this method returns this data type known as a string called Hello World!. Python is able to interpret and define the type of variable for you. So Python has already worked out that this is a string rather than, say, an integer. Do note that for the quotation, you must be either double quotes or single quotes. The next thing we want to do is we are going to print the contents of this function to the screen. Now remember, it's returning you the string Hello World!. The print function outputs the contents of the function to the screen console. There are, as you might imagine, many different ways we can output contents of a function from Python. We can save it to a file, send it to another function or even have Python manipulate the results for us. For now we have returned the contents to the screen. Okay, well done! What we did was create a simple Hello World program. We ran it so we could understand where the program could fail and how we can fix it. Okay, let's move on to the next lesson.
Andrew is fanatical about helping business teams gain the maximum ROI possible from adopting, using, and optimizing Public Cloud Services. Having built 70+ Cloud Academy courses, Andrew has helped over 50,000 students master cloud computing by sharing the skills and experiences he gained during 20+ years leading digital teams in code and consulting. Before joining Cloud Academy, Andrew worked for AWS and for AWS technology partners Ooyala and Adobe.