Thursday, May 28, 2020

Shortcut Operators


Let’s say “op” is a two-argument operator & the operator is used in the following context.

variable = variable op expression

It can be simplified & shown as follows.

variable op= expression

Given below the examples.




Solve a mathematical problem in Pythagorean Theorem.


The square of the hypotenuse is equal to the sum of the squares of the other two sides.





Assume that A = 4.0 & B = 3.0

Let’s find the value of C.

Given below the program to find the value of C.








Output is 5.0

Variables


If we want to give a name to a variable, there’re some rules to follow.

1.       Variable name must be composed of upper case letters, lower case letters, digits and characters (underscore).

2.       Variable name must begin with a letter.

3.       Underscore character is also a letter.

4.       Variables with same meaning but, with different upper case & lower case letters are treated as two different variables.

Example:

THETEXT
thetext
theText

Given above three different variables.

5.       Variable name must not be any of Python’s reserved word.

NOTE

The PEP 8 – Style Guide for Python Code recommends the following naming convention for variables & functions in Python.

1.       Variable names should be lowercase with words separated by underscores to improve readability.

Example: my_variable

2.       Function names follow the same convention as variable names.

Example: my_function

3.       It’s also possible to use mixed case, but only in contexts where that’s already the prevailing style, to retain backwards compatibility with the adopted convention.

Example: myVariable


Keywords

Given below the reserved keywords in Python. They are reserved because we mustn’t use them as names, neither for the variables, nor functions. The meaning of the reserved word is predefined, and mustn’t changed any way.

['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']


A variable comes into existence as a result of assigning a value to it. Unlike in other languages, we don’t need to declare it in any special way. If you assign any value to a non-existent variable, the variable will be automatically created. We don’t need to do anything else.

The creation is very simple, just use the name of the desired variable then the equal sign (=) and the value you want to put into the variable.

Sunday, May 24, 2020

Binding of the operators


The binding of the operator determines the order of computations performed by some operators with equal priority, put side by side in one expression.

In Python, most of the operators have left-sided binding. That means the calculation of the expression is conducted from left to right.





In correct way, from left to right:

9 % 6 gives 3 and then 3 % 2 gives 1
In incorrect way, from right to left:
6 % 2 gives 0 and then 9 % 0 causes a fatal error.

In Python, modulo operator has left-sided binding.







In correct way, from right to left:

2 ** 3 gives 8 and then 2 ** 8 gives 256
In incorrect way, from left to right:
2 ** 2 gives 4 and then 4 ** 3 gives 64

In Python, exponentiation operator has right-sided binding.


Operator Priorities


Example:






Parentheses with operators


According to the arithmetic rules, sub-expressions in parentheses are always calculated first.


Arithmetic Operators – Part II


Remainder (Modulo)

Percent sign (%) is the remainder operator. In other programming languages it also known as modulo.

The result of the operator is a remainder left after the integer division.





Reasons for the above result:

14 // 4 gives 3 (integer quotient)
3 * 4 gives 12 (quotient & divisor multiplication)
14 – 12 gives 2 (remainder)

When we consider the below example,



The answer is not 3 but 3.0

Reasons for the above result:
12 // 4.5 gives 2.0
2.0 * 4.5 gives 9.0
12 – 9.0 gives 3.0

Addition

Plus (+) sign is the addition operator.








Subtraction / Unary & Binary Operators


Minus (-) sign is the subtraction operator. But we should notice this this symbol can change the sign of a number.

So, we have to say here there is an important distinction between unary & binary operators.


In subtracting scenarios, the minus operator expects two arguments.


From the above reason, the subtraction operator is considered to be one of the binary operators.

But the minus operator may be used in a different (unary) way.








There is also a unary (+) operator.





The operator preserves the sign of its only argument.


Thursday, May 21, 2020

Arithmetic Operators – Part I


An operator is a symbol of the programming language, which is able to operate on the values.

Exponentiation

Double Asterisk (**) sign is the exponentiation (power) operator.


Pure text editors don't accept 23, in Python uses ** instead.







When both arguments are integers, the result is an integer. When at least one argument is a float, the result is a float too.









Multiplication

Asterisk (*) sign is the multiplication operator.










Division

Slash (/) sign is the divisional operator.












The result produced by the division operator is always a float. Actually, this is a problem. It happens sometimes that we really need a division that provides an integer value, not a float.

But, Python has solution for this problem.


Integer Division (Floor Division)


Double slash (//) sign is the integer divisional operator. It differs from the standard operator (/) in two ways.

  1. The results are always rounded.
  2. It conforms to the integer vs. float rule.













Let’s do some experiments.








What we get is two ones - one integer and one float.

The result of integer division is always rounded to the nearest integer value that is less than the real (not rounded) result.

In Python, rounding always goes to the lesser integer.


But in here, 








This is ok.


Hence, in below codes some of the values are negative. This will too affect to the result. The real (not rounded) result is -1.5 in both cases.








The rounding goes toward the lesser integer value.

Tuesday, May 19, 2020

Boolean Values


A programmer writes a program and the program asks questions. Python executes the program and provides the answers. The program must be able to react according to the received answers.

Computers know only two kinds of answers.

  • Yes. This is true.
  • No. This is false.


So the two Boolean values have strict denotations in Python. (Including case-sensitivity)

True
False