1.1. Variables & Comments
Let's try out Python for the first time by pressing the play button on the top part of the code editors.
Let's try out Python for the first time by pressing the play button on the top part of the code editors.
Let's try out Python for the first time by pressing the play button on top of the following code. You will see test as the output.
Let's try using a variable. Look at the following codes.
You should see the same output as before: Hello world!
In that code, we've added a variable named text. The value of this variable is "Hello world!". The print command print the value of the variable message.
Now, let's add an empty line and add the following codes. When you run that code, you should see the following output:
Hello world!
This is my first code!
This shows that the value of a variable can be changed anytime in your program.
There are few rules of naming variables and without following these rules, some will cause errors. (Others just help you write code that's easier to read and understand).
Let's look at an error you're likely to make and learn how to fix it. We will se the following code that will generate an error. The error comes from writing texts instead of text in the variable name.
You will see the following message. (You can always close the output by clicking the 'square with arrow' on the left side of output)
Let's analyze the error message carefully.
The error message shows that the error starts at line 2.
The output at last line shows that the error is of the type NameError. This is further explained, which says that name 'texts' is not defined.
Throughout this class, you will likely do some errors and before you ask around, let's carefully analyze the error message first. Most of the time, you can easily find the error this way.
Now, deleting the extra s from texts will not produce any error.
In Python, the hash mark (#) indicates a comment. Anything following that mark is ignored. For multiline, you can use triple quotation marks.
1. Simple Message
Assign the message "This is the first lesson." to a variable text_1, and the print that message.
2. Simple Messages
Assign the message "I'm learning Python." to a variable text_2, and the print that message. Then change the value of the variable text_2to a new message "So far, it is easy.", and print the new message.
3. Why Error?
Uncomment (delete the #) from the No.3 section. The code will generate error if you run it now. Try to fix the code in the print part so the program runs normally. The output should be:
I learned about print().
I also learned about variable!
4. No New Line
Sometimes, we don't want to make a new line when we print. To do this, we can add end='' as the second argument of the print, like this:
print("Hello,", end=' ')
print("Hello!")
>> Hello, Hello!
Uncomment text3, text4, text5, and text6 . Then, using them, print the following output:
This is the end of the first exercise.