Saturday 22 August 2020

Lesson Deux

 In lesson one we had two different types of inputs, such as ‘Hello World’ and 3 + 5.

When you input characters to form words for example that are known as STRING. You enclose them in quotation marks ‘ ‘. You could use double quotations as well “ “. Whatever you write and enclose within quotation marks and print comes out as it is.

Another type of input is the numbers. These do not need to be enclosed within quotation marks and can be changed by the program, e.g. 3 + 5 would give 8. We would see other forms of input later on in the course. This is just a very basic explanation to get you started with programming. We might see more examples and go a bit more in-depth later in the course.

Let us try some examples to illustrate this. However, before doing that, let us explain the hashtag key that is #. Whenever you type # before any line of code, all the text to the right of it is ‘commented out’. Meaning Python would not take that into account when executing your code.

Okay, on to the example. Let us go back to your saved code, Lesson 1. Type # before each of the lines you used in the example from the previous lesson. It should look something like this:

You will notice that all the text after the # changes to green. That means they would not be executed when you press the green play button. Type this in the upper right window.

                print(Hello)

This gives you some error messages at the lower right window. What could be wrong? If you guessed that you did not enclose the string Hello in quotation marks, then you are right. The string Hello, should have been written as ‘Hello’ or “Hello”. Make the correction and run it again. It would work this time.

Try these examples below:

                print(‘To be or not to be, that is the question’)

                print(“Please enter your name: “)

                print(‘It is sunny today’)

                print(30 + 2)

                print(’30 + 2’)

You should get something like this

Notice that there is a difference between print(30 + 2) and print(’30 + 2’). Python treats the former as numbers and treats the latter as a string.

Let us try these last examples. You can comment out the previous examples using #.

                print(‘Last number is’, 20)

                print(‘Today is’, 30, ‘degrees’)

                print(50,’People signed up’)

You should get something like this

This shows you can combine strings and numbers but you have to enclose the stings in quotation marks and separate the strings from the numbers using commas.

In Lesson 3 we would discuss what variables are and how to assign values to them. We would also look at the difference between integers and floats then how to write the code to accept external input from users.


No comments:

Post a Comment