How to Use Literal Constants and Variables
Pull up your text editor and run the following:
Your output should look like this:
What happened in this program is that you assigned a literal value of 5 to the given variable i
through an assignment operator, which is the = sign. That entire line is considered a statement
because it indicated that something should be done, which is connecting the said variable to a
numerical value. Afterwards, you printed out the value of i by using the print command.
Afterwards, you added 1 to the given value that you stored in the variable i, and then you saved it.
When you use the print statement again, you get the value of 6.
At the same time, you also assigned a literal string to the variable s and then proceeded to use the
print statement.
Physical and Logical Lines
What you see when you type out a program is called a physical line. What Python gets when you
type a statement is called the logical line. With this said, this programming language assumes that
every physical line that you see corresponds to a given logical line.
While you can use more than one logical line on a physical line by using the semicolon (;)
symbol, Python encourages that programmers like you input a single statement in order to make
your codes more readable. This way, you will be able to see lines that you are working on and
avoid possible confusion when you are working on two different logical lines and get lost on
what you are supposed to work on.
Indentation
Python is one of the programming languages out there where white space, especially the space at
the beginning of each line of code is important. By using indentation, you can group together
blocks, or statements that belong together. As a rule of thumb, see to it that you are using the same
indentation when you are working on similar statements. Also remember that using the wrong
indentation can make your code prone to error. Take a look at this example:
When you run this code, you will get this result:
Python recommend that you use four spaces for your indentations. Typical good text editors will
do this for you. As long as you are consistent with the spaces that you are using, you will be able
to avoid unexpected results in your code.
Now that you know the basics, you can now start learning the more interesting stuff! So stay tuned with this series and see you soon.
0 Comments