• No results found

Internal Components of Statements

In document Python (Page 31-43)

To create a statement or instruction for the computer to follow, you will need to fill a statement with some necessary components. The components needed to be added depends on what type of action you want to happen.

Just like in a sentence, programming statements also have parts. In sentences, you need noun, verb, adjectives, adverbs, and punctuation. In a statement, you need variables, operators, expressions, literals, functions, and keywords.

Literals or Data

Programs need data in order to function or to have a purpose. Literals are fixed values that you can manipulate in order to achieve a goal. For example, the numbers 1, 2, 3, and so forth are literals.

Literals are not limited to numbers alone in Python. Text or strings are also literals.

There are multiple types of literals in Python. And two of the important literals that you should learn are strings and numerical literals.

Numerical Literals

Any plain number in Python you can see is a numerical literal. Numerical literals can be shown in Python in multiple forms. You can type them as integers, plain integers, floating points, or imaginary numbers. On the other hand, you can show them in binary, octal, decimal, and hexadecimal. Also, you can use scientific notation to present them.

By the way, you should never separate one number with commas. For example:

>>> 123,456,789 error or an undesirable result may happen if you do not. For example:

>>> "This is a string"

'This is a string'

>>> This is a string File "<stdin>", line 1

This is a string

^

SyntaxError: invalid syntax

>>> _

In typing strings in Python, you might encounter some difficulties or errors when you include certain or symbols. For example, if you use double quotes in your text:

>>> print ("And Rudolph said, "Why do I have a red nose?"") File "<stdin>", line 1

print ("And Rudolph said, "Why do I have a red nose?"")

^

SyntaxError: invalid syntax

>>> _

Since the example included a double quote in the string, the interpreter thought that the example has terminated the string on the second instance of double quote — the double quote after Rudolph said. Due to that, the interpreter was confused on the following characters after that, thus it returned an invalid syntax error.

There are three ways in order to overcome that. First, you can use a different quote to prevent string termination. In the example’s case, you can use a single quote to contain the string with the double quotes. For example:

>>> print ('And Rudolph said, "Why do I have a red nose?"') And Rudolph said, "Why do I have a red nose?"

>>> _

Another method is to use triple quotes. This method can prevent you from having errors with strings with single or double quotes in them. Triple quotes are just three single quotes put together. For example:

>>> print ('''And Rudolph said, "Why do I have a red nose?"''') And Rudolph said, "Why do I have a red nose?"

>>> _

And the last method is to use escape characters to insert the double quotes. To do that, you need to use the backslash character. For example:

>>> print ("And Rudolph said, \"Why do I have a red nose?\"") And Rudolph said, "Why do I have a red nose?"

>>> _

To prevent the interpreter to ‘misinterpret’ the next double quote, the example used a backslash character to ‘escape’ the meticulous check that the parser is doing. Due to that, the double quote was just treated as a string character instead of a string terminator.

Escape Sequences

In Python, there are multiple escape sequences that can help you prevent having problems with some signs and symbols. Some of those escape sequences are:

Sequence Purpose

\\ Escape sequence for backslash

\' Escape sequence for single quote

\" Escape sequence for double quote

\n Escape sequence for Linefeed (LF)

\r Escape sequence for Carriage Return (CR)

\t Escape sequence for horizontal tab

Note: To create a line break in your string, you can insert both \r and \n in the string.

For example:

>>> print ("This is the first line. \r\nThis is the next line.") This is the first line.

This is the next line.

>>> _

Also, you do not need to place a space after the escape sequence. If you do put a space after it, it will be included on the string. For example:

>>> print ("This is the first line. \r\n This is the next line.") This is the first line.

This is the next line.

>>> _

Operators

In the example, the arithmetic operator addition (+) was used. It was placed between two operands, which are numerical literals. By the way, in the interpreter mode, you can just perform expressions like that. However, if you do place a line of code like that in programming or scripting mode, you will receive an error.

Also, operators in programming are similar with Mathematical operators. The only difference is that programming operators are greater in number and functions. Anyway, since it was entered on the interpreter, it immediately evaluated the expression. And the result of 1 + 2 was 3.

Arithmetic Operators

In programming, a lot of Math will be involved. But do not fret; you do not need to become a master of calculus to pull off a decent program. Truth be told, most common programs only require basic Mathematical operations such as addition, multiplication, division, and subtraction. Below are the arithmetic operators in Python you can use.

Operator Operator Name/Description Example

+ Addition: adds the value of the two 1 + 1 equates to 2

operands Commutative, associative, and identity laws apply to addition in Python.

1 + 2 is equal to 2 + 1

1 + (2 + 3) is equal to (1 + 2) + 3

1 + 0 is equals to 1 and 2 + 0 is equals to 2

- Subtraction: subtracts the value of the left operand to the left. Also, you can treat it as an addition operation

This is possible because Python allows and understands double negation.

* Multiplication: multiplies both

operands

2 * 3 equates to 6

Commutative, associative, distributive, and identity laws apply to addition in Python.

3 * 6 is equal to 6 * 3

3 * (6 * 9) is equal to (3 * 6) * 9 3 * (6 + 9) is equal to (3 * 6) + (3 * 9) 5 * 1 is equals to 5 and 34 * 1 is equals to 34

Note: Always put the multiplication operator when multiplying a value to an expression enclosed in parentheses.

If you try multiplying like this: with the right operand, but instead of providing the quotient, it will only

Modulus also does not allow 0 on the right operand.

** Exponent: provides exponential power to the left operand.

5 ** 2 equates to 25 3 ** 3 equates to 9 15 ** 0 equates to 1

// Floor division: divides the left operand with the right operand, but any fractional component in the quotient will be removed.

10 // 3 equates to 3 instead of 3.33 15 // 6 equates to 2 instead of 2.5

Floor division does not allow division by zero.

Take note, just like normal Mathematical expression, you can group your operands using the parentheses. Also, all the expressions within the deepest grouping of parentheses are always evaluated first. Exponents are evaluated next before multiplication and division. Addition and subtraction get processed last.

Variables

If your program needs a lot of literals and will involve a lot of evaluation of expressions, tracking and taking note of those literals become difficult processes. You will need a place where you can store them and recall them for later use.

That is the function of variables. Variables are storage locations where you can store your literals (numbers and text) and the result of your expressions or operations. You might be already familiar with the concept of variables in Mathematics. Remember the x, y, and z your Mathematics professors or teachers talked about during class? It is almost the same as the variables in computer programing.

Of course, there are differences between them. First of all, variables in programming does not only store numbers, but they can also store strings. For example:

>>> x = "this is a string"

>>> x

Instead of having a fixed or unknown value, the last value that you assign to a variable will be its value. For example:

124 operator (=). Take note of that since you might get confused to the = (equals sign) later on. Also, always make sure that when assigning variable, you need to place the variable on the left of the assignment operator and place the value on the right of the assignment operator. For example:

Here’s a tip: you can actually assign values to multiple variables in one physical line or logical statement. You just need to use the comma (,) separator. For example:

>>> x, y, z = 23, 15, 541

Identifiers

Identifiers, sometimes called as labels or lexical tokens, are names in programming that makes it easier to recall, invoke, or use a variable, function, class, object, and etcetera. Since variables are ‘intangible entities’ in programming (they are only storage location after all and calling them using their address on the memory is a tough job), you need identifiers in order to use them.

However, before you can create an identifier for a variable (or other elements), you must know the following rules or naming conventions in Python:

• No identifiers should duplicate each other. If you are going to use an identifier similar to a function’s identifier such as print, you will receive an error.

• Identifiers are case sensitive. The variable named x is different from the variable named X.

• Identifiers should be at least one letter or underscore long.

• Identifiers should start with a letter or an underscore. Do not start with a number or any other symbol.

• Identifiers can contain letters, numbers, and underscores. However, numbers without letters or underscores are invalid and are automatically considered numerical literals.

Expressions

Expressions are combinations of literal, variables, functions that return a value, and operators. They can be assigned to a variable. But do note that the expression itself will not be assigned, but instead, the result of the expression will be. For example:

>>> x = 1 + 1

>>> print (x) 2

>>> y = 2

>>> z = x + y

>>> print (z) 4

>>> a = z + 3 + (y * x)

>>> print (a) 11

>>> _

These components alone cannot provide you with enough power to create a decent program. However, they are essential building blocks you will need. In the next chapter, some functions will be discussed. Those basic functions will allow you to give your program some use.

In document Python (Page 31-43)

Related documents