Sunday 23 August 2020

Lesson Tres

 Excellent work so far. Now let us try this these codes below:

                x = 3

                y = 5

                print(x)

                print(y)

Did you notice that? You should get an output like this:

Let us explain what is going on here. The letter x and the letter y here are referred to as variables. The equal sign, = is referred to as the assignment operator (Not the equal sign, the equal sign in double ==, that is for another lesson in the future). Basically, you were telling Python to assign the number 3 to the variable x and the number 5 to the variable y. They are called variables because those values can be changed or varied.

Let us attempt to add these few lines directly under the previous block of code and see what the output would be:

                x = 4

                y = 6

                print(x)

                print(y)

We used the print(x) and print(y) twice each but we got 3 and 5 initially and got 4 and 6 afterward. That is because initially we assigned 3 to x and 5 to y and after executing the print command we re-assigned 4 to x and 6 to y.

What happens when we add this to the line below the block of codes?

                print(x + y)

The command print(x + y) gave us 10 because the x and y were assigned 4 and 6 respectively as their latest values. Hence they are not strings but they represent the numbers 4 and 6 and their sum is 10.

We can also assign strings to variables, not just numbers. We can comment out the block of codes above by putting # before them and then try out these lines below:

                x = ‘Hello’

                y = ‘World’

                print(x)

                print(y)

So it is very clear now that strings can also be assigned to variables.

Variables do not have to be single letter. They can be words as well but without spaces between them. Or you could use an underscore _ to separate the words of a variable. Examples below:

                username = ‘Jay’

                user_name = ‘Ali’

                print(username)

                print(user_name)

It is clear now that you can use words as variables. As we can see writing username is not the same as user_name. They contain different strings ‘Jay’ and ‘Ali’ respectively.

Now we can assign values to variables. We can assign strings and numbers to variables.

To end this lesson, let us introduce how to make our code more interactive by asking for user input. We do that by using the Python built-in command know as input.

Let us comment out the block of codes above and try it out:

                user_name = input(‘Please enter your username: ‘)

                password = input(‘Enter your password: ‘)

                print(user_name)

                print(password)

When you press the play button you are prompted to enter a username. You can type in any name you want. I would type in Ali.

Next, you are prompted to enter the password. You can enter anything here too. I would enter in 1234.

The username and password are now printed out. Well done.

In the next class, we would explore this interactive aspect of prompting the user for an input.

Leave your questions and comments below in the comments section.



No comments:

Post a Comment